Generic Thermostat Failovers with Tasmota

I am trying to find a way to turn off a heater when the temperature sensor being used by the heaters generic thermostat fails to update

I can do this with an automation to look at the last state change and if it exceeds a set time then I can turn off the switch and it will resume when the generic thermostat gets a temperature reading again - this all works

My temperature sensor is a DHT22 connected to the GPIO of a sonoff running Tasmota firmware

The problem is that if there is a fault with the DHT22, I have simulated this by removing the signal wire, instead of a number, the tasmota firmware sends the string “nan” for the temperature value and home assistant registers this as a sensor update even though the sensor and thermostat both show the last received numeric value.

There is an error in the HA log

Error parsing value: ‘value_json’ is undefined (value: {“Time”:“2018-10-19T20:47:51”,“SI7021”:{“Temperature”:nan,“Humidity”:nan},“TempUnit”:“C”}, template: {{ value_json[‘SI7021’].Temperature }})

In this situation, the thermostat would keep the heater on indefinetely

Does anyone have a solution please?

I have my generic ‘thermostat’ which is just a collection of automations to turn on/off my not-smart heatpumps. The off automation is triggered by my ecobee temperature sensor reading and a timer that is the failover. The timer is set to run for a fix period (2 hours) and is started/restarted every time heatpumps are turn on. So I don’t have to depend on just the temperature sensor.

Thanks, time may have to be an option but it is not my preferred option

I am trying to define the sensor to show unknown if the sensor fails but without success

  - platform: mqtt
    name: "Lounge Temperature"
    state_topic: "tele/sonoff/SENSOR"
    expire_after: 120
    force_update: true
    value_template: "{% if value_json['SI7021'].Temperature is defined %}{{value_json['SI7021'].Temperature}}{% else %} unknown {% endif %}"
    unit_of_measurement: "°C"

Another idea is a Tasmota rule to replace ‘nan’ with something else but also cant get that to work

Have you tried the payload_not_available option for mqtt sensor in this case?

The payload is available, it keeps updating the last_changed date, it just has the value of nan instead of the numeric temperature value

I have now managed to get a solution working, this sensor value template detects the temperature of nan that is sent by tasmota when the temp sensor fails as not defined and replaces it with unknown. This sensor also has a expiry time that will cause home assistant to change the value to unknown after 120 seconds if no value is received. A single automation can detect both failures and turn off the heating. As soon as the temp values are available again, the thermostat will turn the switch back on if required.

  - platform: mqtt
    name: "Lounge Temperature"
    state_topic: "tele/sonoff/SENSOR"
    expire_after: 120
    value_template: "{% if value_json is defined %}{{value_json['SI7021'].Temperature}}{% else %}unknown{% endif %}"
    unit_of_measurement: "°C"

- alias: "Boiler Failsafe"
    platform: template
    value_template: "{% if is_state('sensor.lounge_temperature','unknown') %}true{% endif %}"
    service: homeassistant.turn_off 
      entity_id: group.heating

Something has changed here, the dht22 when disconnected from the sonoff now shows as “None” in the states page. On sensors where I have applied an offset, eg. Float -1, it now shows the temperature as -1 C in a fault condition of a disconnected sensor. I will need to rewrite my rules for this.

I tried to apply the method that is explained here but with no success with tasmota 7.20 and homeassistant 0.103.5.
Here is what worked for me:

  - platform: mqtt
    name: "HTU21 Temp."
    state_topic: "tele/tasmota/SENSOR"
    value_template: "{% if as_timestamp(now()) - as_timestamp(states.sensor.htu21_temp.last_changed) | int < 60 %}{{value_json['HTU21'].Temperature}}{% else %}unavailable{% endif %}"
%}unavailable{% endif %}"
    unit_of_measurement: "°C"
    icon: mdi:thermometer
    availability_topic: "tele/tasmota/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Not sure if this topic is the right place given it is quite old.
I am trying to setup a Thermostat using Tasmota, SOnOff SV and a couple of sensors.
What I want is to try and create a failsafe if Home Assistant crashes or becomes unavailable.
If HA crashes, and I am not home at the time, it could potentially crash with the thermostat relay stuck in the on position, meaning the Sonoff really is on and nothing to send a signal to turn it off.
Any advice from a HA or Tasmota guru would be appreciated.

Look up rules for Tasmota, I have the following set on some of my sonoff basics so they turn off the relay if the mqtt server is disconnected. I once had ha crash but the mqtt was still connected so it’s not completely safe but the best I have found so far.

{“Rule1”:“ON”,“Once”:“OFF”,“StopOnError”:“OFF”,“Free”:471,“Rules”:“on Mqtt#Disconnected do power1 off endon”}

1 Like

I was trying to make sense of the Tasmota rules a bit, I had trouble with the syntax.
I’ll try the rule you listed as that looks like what I need. I am using the onboard relay for the heat/furnace component and a secondary relay for fan only, so that should work well.
I have a couple of spare DHT11’s I might use as an absolute cut-off and see if I can work out the syntax enough to have a rule that cuts off inside Tasmota firmware if the temperature exceeds an absolute value.

Great thing about the Sonoff SV, it has GPIO4, GPIO5, and GPIO14 exposed as pins as well as Tx and Rx headers. A little sidetracked there :slight_smile:

I used the rule and it works well. I also added a ON WiFi#Disconnected DO Power1 off EndOn as well.
Now I have the format right, I am also adding a On DHT11-14#Temperature>35 DO Power1 off EndOn as well as a second one that measures the temperature of the air in the ductwork as On DHT11-5#Temperature>60 DO Power1 off EndOn . I have two DHT11’s , one on GPIO14 and the other on GPIO5.

Thanks so much for helping with the rule syntax.

Edit: My temperatures are in Celsius. I just thought I should add that :slight_smile:

1 Like

I like your over temp rules, they won’t work for me as most of my dht22 sensors that control my heating are not on the same sonoffs.

Have you noticed restart reason ‘hardware watchdog’ on your sonoff Tasmota information page when using dht22 sensors? All mine suffer from this but the ones with no dht22 don’t restart for months.

I had a couple of spare DHT11 sensors so I added them specifically for over temp.

I have not noticed any restart events to be honest, but I am using DHT11’s not 22’s. Might be a bug with the DHT22 implementation… I might have to get a DHT22 and run some tests.