restoreOnStartup doesn't transmit restored value to mqtt

I have a basic test environment, mysql persistance. Switch1 is set to resoreOnStartup.

Item Switch1 <–> mqtt (smarthome/room1/led) <–> espDevice <–> led

All works fine, Switch controls led and value is seen posting to the mysql database, if I shutdown and restart Openhab2, the Switch1 item restores in the Openhab2 UI from null to the last state in the database, but no MQTT message is sent to the broker, is this the expected behaviour or do I need something else to restore the external device?

–log extract
2018-11-04 22:11:33.210 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Location set to ‘53.825613999999995,-1.8625439’.

2018-11-04 22:11:43.751 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘test.items’

2018-11-04 22:11:43.839 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘test1.items’

2018-11-04 22:11:44.415 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘mysql.persist’

2018-11-04 22:11:47.213 [INFO ] [thome.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007

2018-11-04 22:11:48.363 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘test.sitemap’

2018-11-04 22:11:50.150 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at

2018-11-04 22:11:50.156 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at

2018-11-04 22:11:50.655 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app

2018-11-04 22:11:51.498 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui

2018-11-04 22:11:51.709 [INFO ] [] - MQTT Service initialization completed.

2018-11-04 22:11:51.718 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection ‘broker’

==> /var/log/openhab2/events.log <==

2018-11-04 22:11:52.828 [vent.ItemStateChangedEvent] - MQTT_Test changed from NULL to OFF

2018-11-04 22:11:52.845 [vent.ItemStateChangedEvent] - MQTT_Out changed from NULL to ON

==> /var/log/openhab2/openhab.log <==

2018-11-04 22:11:53.228 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel

Yes. Persistence restores OH item states, nothing more nothing less. If you want whatever communication to take place, create a rule to trigger upon when System started.

Yes, the MQTT binding will react on a command not an internal state update.
Bindings is general need a command to send the instruction to a device.

You can use the MQTT binding to send a payload with a state update but it is a bit dangerous as you can end up with a MQTT loop.

What is your item definition.

On another note. Using MySQL to restoreOnStartup is slow and inneficient. There is a dedicated databse for this: MAPDB. It only stores the last state so doesn’t take any space on the drive.

Thank you Markus for the fast response,. I’ll look into how to implement rules.



Hi Vincent,. I intend to use the data stored in mysql as input to state-space systems analysis that can then be used to feedback into the sensor control model. Slow or slower recovery after a restart is not an issue for me.

As explained by Markus and Vincent, that isn’t how restoreOnStartup is supposed to work.

One way to get this behavior is below. Beware of the infinite loops Vincent mentions though.

Create a System started Rule that creates a Timer to wait a bit to ensure that restoreOnStartup has done it’s job. Then loop though the Group of all the Items and sendCommand the current state.

rule "Send command restored states"
    System started
    createTimer(now.plusSeconds(10), [ |
        MyGroup.members.filter[ d | d.state != NULL ].forEach[ d | d.sendCommand(d.state) ]

As an aside, please How to use code fences for code and logs posted to the forum so they are easier to read.

1 Like