Question about MQTT Switch


I have a Generic Thermostat that triggers a MQTT Switch to open/close the valve of a water heater:

- platform: mqtt
  name: "heater_bedroom"
  state_topic: "bedroom/relay/state"
  command_topic: "bedroom/relay/set"
  payload_on: 1
  payload_off: 0
  state_on: "1"
  state_off: "0"
  retain: true

- platform: generic_thermostat
  name: thermostat_bedroom
  heater: switch.heater_bedroom
  target_sensor: sensor.temperature_xiaomi_air_1
  min_temp: 16
  max_temp: 26
  ac_mode: False
  target_temp: 20.5
  cold_tolerance: 0.1
  hot_tolerance: 0.1
    seconds: 5
    minutes: 3
  initial_operation_mode: "auto"
  away_temp: 16

I also have an automation that triggers the central heating boiler whenever 1 or more heater switches are open:

- id: enable_boiler
  alias: Enable Boiler
    - platform: state
        - group.all_heaters
      to: 'on'
      from: 'off'
    - service: switch.turn_on
          - switch.boiler

This configuration works pretty well (I’ve been using it for the whole season without glitches) until, for whatever the reason, one of the heater’s MQTT Switches disconnects.

If one of the heater’s MQTT Switches disconnects while the State was “ON”, it remains “ON”, and my Boiler keeps heating even though the heater’s MQTT Switch now is closed, because those are Normally Closed valves.

So basically, the ideal solution for me would be a MQTT Switch that changes state to “OFF” when the connection to the client is lost.

What could be a solution for my problem?


Ok, if your switch loses connection to the MQTT server, it can’t send an ‘OFF’ state. Create a binary_sensor that pings your device, and if it is disconnected for x minutes, publish an ‘OFF’ message.

Or write another automation that turns off the boiler. Use state triggers for each heater when they become ‘unavailable’.


- id: disable_boiler
  alias: Disable Boiler
    - platform: state
      entity_id: switch.heater_bedroom
      to: 'unavailable'
    - platform: state
      entity_id: switch.other_heater
      to: 'unavailable'
    - service: switch.turn_off
          - switch.boiler

I’d also be making an effort to discover why these switches are becoming unavailable.

Well everything is here, if the client is lost, you have two sides, ha+Mqtt with automation consequence, and device (boiler) consequence.
I just set up a Shelly 2.5 yesterday, you can choose your reboot default setting in case of power loss, that should answer your question from my understanding.

Yes, but he’s not talking about power loss, but loosing connection (not sure loosing connection to Wifi or loosing connection to MQTT, OP did not specify)

The NC default state of the device make me think of power loss, power return make me worry about boiler (should have built in security anyway).
But agree with you, that don’t mean return on WiFi nor mqtt, device could also stay unavailable in HA.
When it happen to me the device (sonoff basic/Tasmota) reboot on it’s own ssid without saying why.

The way I read his question :

he has heaters ( “heater_bedroom”) that lose connection and turn their self off, but don’t send a message that they are off
as a result, the boiler ( switch.boiler ) does not turn off, while it should.

This sentence is quite confusing, one could understand both ways about boiler switch losing connection also, (does the implyed switch regain connection ?) But if only thermostat switch (1 or many) lose connection the problem is the same, you should find a switch offering choice of NC or NO.

Sorry for being so vague on my description - The problem is abrupt lose of power on the device that opens/closes the valve on each heater. Under such circumstance, the device is not able to send any message to the mqtt broker, so this device remains “ON” for HA, even though it is now closed, as it is now a not powered Normally Closed valve. I hope this clarifies it.

Thanks for all the answers so far

Somebody disconnected them :slight_smile: I’m going to play with the solution you proposed. I didn’t know mqtt switches trigger an ‘unavailable’ state event when they disconnect…

Sounds like a solution - but I’d need to build that ‘ping’ into my device firmware (which I wrote) and reflash all the devices… I was wondering if there’s something more OOTB

The scenario is not total lose of power - Actually my HA server has power backup. This is just about power loss on the mqtt client that controls the heater valve.

Have you heard about LWT? I believe it’s built into MQTT switch as well so all you need to do is provide availability topic to it on HA side and publish LWT when you connect to MQTT broker.
Much better than the ping solution imho.

Well, I was going on the fact that you said they were unavailable. Which they should be if you have LWT enabled.

HA binary_sensor

  - platform: ping
    name: binarysensorrflink
    count: 2

Building in your device makes no sense, if it has no power it will still not send messages.

1 Like

This is exactly what I needed :slight_smile: it solves my problem very elegantly. Now I just need to reflash all my heater switches :pensive:

1 Like