Resend MQTT command to nodemcu(or ESP 8266) after power failure

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…

Create a System started rule with:

MyMqttRelayItem.sendCommand(MyMqttRelayItem.state)

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.

3 Likes

Thank you very much…

Now it works really well for all the items…

Great help, really appreciated…

@rlkoshak Rich,

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.

Thanks! Danny

Good eye. Yes, count should be a var.

THanks just making sure I didn’t miss something. I was initially getting an error with val. It forces me to rtfm though!! Lol