How to trigger an automation based on temperature hysteresis

Hello,

I am trying to configure the following automation:

  • when temperature drops below 19°C, turn heater on
  • when temperature raises above 19.5°C, turn heater off

I defined a “cold” threshold binary sensor, with lower limit set to 19.25°C, and hysteresis set to 0.25°C. I also defined two automations, “turn heater on” that is triggered when “cold” changes to on, and “turn heater off” that is triggered when “cold” changes to off. The heater is stateless, the commands are sent to an IR-enabled tasmota device through MQTT.

The problem is, sometimes the bluetooth thermometer goes unavailable, and the “cold” sensor goes unavailable as well.

Let’s say “cold” is on (heater on), and temperature at 19.1°C. Thermometer goes unavailable, as well as “cold”. When thermometer goes available again, “cold” changes to off, turning heater off, breaking the expected hysteresis.

Is there any way to keep the “cold” sensor to its last value when the thermometer is not available?

Create a Template Sensor that reports the current value of the “cold” sensor except when the value is unavailable (or unknown). In that case, the Template Sensor reports its existing value (which represents the “cold” sensor’s previous value).

template:
  - sensor:
      - name: Enhanced Cold Sensor
        state: >
          {% set t = states('sensor.your_cold_sensor') %}
          {{ t if t | is_number else this.state }}
        device_class: temperature
        unit_of_measurement: '°C'

Thanks for pointing out the template sensor.