I have three generic thermostats set up to control 3 zones in an mqtt relay system. The happy path works very well and i’m very happy with it. However, sometimes the mqtt relays can get out of sync with the home assistant generic thermostat. For instance, if the relay was on and home assistant is rebooted, Home assistant would come up thinking the generic thermostat was off but the relay would still be on as the retained message in the mqtt queue is on. Another example is when I inadvertently turn on the mqtt relay by doing turn on group.all_switches.
To mitigate the above issues i configured a keep_alive setting to 3 minutes in the generic thermostat. I’ve looked at the documentation for this configuration and read the generic_thermostat.py code and from what I can see, this should to the trick. In other words, the generic thermostat module updates the mqtt topic to off or on depending on what is required every 3 minutes . But this does not seem to be the case and I have had instances where the i accidentally turned the mqtt relay on but the generic thermostat which was turned off didn’t send any mqtt command to the mqtt relay to turn it off. So the heating remained on for hours when it should have been off.
Is this is a bug in home assistant? Or is the keep_alive functionality disabled when the generic thermostat is set to off. I could put in some automations to turn the relay off if generic_thermostat is turned off, but this doesn’t seem like a nice solution to me.
I believe you’ve identified the problem. You are publishing command topics with retain: true.
For command topics, namely topics that control a device’s state, it’s best to publish them with retain: false. You don’t want the MQTT Broker to retain a command. It’s Home Assistant’s responsibility to be the controller of devices, not the broker’s.
For state topics, namely a topic representing a device’s current state, it’s best for the device to publish them using retain: true.
Thanks for the reply 123, however I’m not sure that will solve the problem. Lets say I turn the retain flag to false, what will instruct the relay to turn off? Home assistant has been rebooted, the relay remains on and the mqtt topic does not have any information in regards to the correct state.
Normally I enjoy thought experiments but in this case I think you should simply try it. Be sure to first purge the retained topic (while retain is true, publish an empty-string to the topic). Then set retain to false and start the experiment.
It’s the physical switch is in the in correct state. When the system comes back up, from home assistants perspective the thermostat defaults to off. And it assumes it’s off even though the actual physical switch controlling the boiler remains on.
If you need HA to receive the correct state when it restarts, then your thermostat code should publish its state message with the retain flag set.
This will cause the broker to keep the state message in its database, and when HA subscribes to the topic (after it restarts), the broker will send the last message for that topic, which will update the state of the HA switch.
I’ve been put off by MQTT because it seems overly complex.
You’ve literally made the whole thing a lot easier by just clearing up the difference between state values and retain values.
Nothing to do with this problem directly, but thanks for putting it into plain english!