I’ve been setting up some lights via MQTT and I wanted to make an inquiry.
The system works fine but I have problems when the system restarts because the state of the switch is unknown. I thought about creating a rule that changes the state of the switch to OFF when it starts but this would not be correct in the scenario when the lights are on and the system is restarted.
Any help on how the rule should be? or how it should be solved?
The general recommendation is to have your device send retained mqtt messages. That way openhab always get the most recent state the devices reported on startup, even if the device changed state after oh was shut down.
Unless the switch changed in the mean time then openhab will get it wrong.
Having the device publish a retained message ensures that when openHAB reconnects to the broker it will get the latest state from the broker.
That only changes the messages that is sent from openhab, you need to configure that on your device. If you tell us what it is someone might be able to point you in the right direction.
Maybe I am doing something wrong but for my setup I have 0 retained messages. I also only have powered mqtt devices so if they lose power my whole house is off.
Since they boot to last known state and openHAB is the last thing up it all just works.
The only time I would have an issue is if a manual change occurred while openHAB was shutdown. This problem exists for every device in my system and the assumption is openHAB is always online.
Seeing this thread shows many people have various methods of how they manage their devices, what works for one may not work for another. The key takeaway is find what works for you and implement your system that way. There is not one correct answer.
As a beginner we have to ask for feedback and try different methods until we find the one that works for us.
I’m not really tracking it from 2.5M1 anyway good to hear that it’s now not undefiing values as before
As before it was not working I’ve build my system in way that reboot of OH is not really an issue and everything will repair itself in like 2-3minutes anyways.
No need of retains and/or persistence at all.
Add to your code periodic state update of your device, every minute or so. You would not need retained messages.
Also consider using tasmota or other well known esp software. It has this out of the box.
After reading your answers, I changed the items and things files from OH and the Arduino code.
For “prevention” or for testing, create an extra item that tells me the status you read from MQTT. I got that after rebooting the system the status of both items is kept updated.