I am controlling Relays and dimmer LED by openhab through nodemcu using mqtt, I have tried using mapdb Persistence which stores the last state of the devices in openhab. When power goes off and gets back on again it restores the switch status in sitemap file but my relay doesn’t get ON after power failure…
Is there any way I can resend last state of the switches by mqtt to my nodemcu on openhab startup…
This will send what ever state was restoreOnStartup to the device.
Note, you may have to add in a Thread::sleep() for a little bit if you are on a particularly fast machine as your System started rule may start executing before the old state was loaded from MapDB.
You could do something like:
var count = 0
while (MyMqttRelayItem.state == NULL && count < 20) {
Thread::sleep(1000)
count = count + 1
}
if(MyMqttRelayItem.state == NULL) logError("logname", "MyMqttRelayItem never received a restoreOnStartup!")
else MyMqttRelayItem.sendCommand(MyMqttRelayItem.state)
This will wait up to ten seconds for the Item to get its restored state and write an error log if it never does.
Looks like I got this working, but in the example above I found one thing in question. Should it not be var vs val? val is for constants I believe according to docs.