Generic Thermostat: Using the keep_alive setting to keep MQTT Relays in correct state

Hi,

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.

climate:
  - platform: generic_thermostat
    name: Downstairs Heating
    heater: switch.downstairs_heating
    target_sensor: sensor.kitchen_temperature
    min_temp: 12
    max_temp: 22
    target_temp: 20.5
    cold_tolerance: 0.2
    hot_tolerance: 0
    min_cycle_duration:
      minutes: 1
    keep_alive:
      minutes: 3
    initial_operation_mode: "off"
    away_temp: 12

Any takers?

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.

1 Like

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.


EDIT
I invite you to read this thread starting with my post explaining why only state topics ought to be retained, not command topics.

If you are using Tasmota for the relays, The Hook Up has an informative video explaining the correct use of retain.

Does the state of the heater switch change to match the actual state correctly?

I can’t really tell from your description if it is a problem with the switch in the incorrect state, or the thermostat not reacting to it.

I’ll try to test this over the next few days to see does it work.

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.

1 Like

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!