Missing values from MQTT sensor breaking binary_sensor based on it

Hi, I have multiple MQTT sensors (Tasmota with automatic discovery). I defined a few binary sensors on top of it as input triggers/conditions for automation, e.g.

binary_sensor:
  - platform: template
    sensors:
      return_warmer:
        friendly_name: "Return water from panel is warmer than input"
         value_template: "{{ (states('sensor.panel_input_temperature') | float) < (states('sensor.panel_return_temperature') | float) }}"

Some of my sensors don’t send data intermittently (honestly I don’t know why but they are never missing data for very long, maybe 2-3 minutes or so). If there is a missing value this will randomly become true or false depending on which value is missing (I think).

So I tried to make things consistent by adding a condition for unavailability, like this:

value_template: "{{ false if (states('sensor.panel_input_temperature') == 'unavailable' or states('sensor.panel_return_temperature') == 'unavailable') else (states('sensor.panel_input_temperature') | float) < (states('sensor.panel_return_temperature') | float) }}"

But now whenever one of the sensors goes offline it will just turn to false.

What I actually want is that the sensors retain the previous value for longer before becoming unavailable. Is this configurable? I’d love to just set it to 5-10 minutes. The temperatures in mys solar system don’t change that quickly.

Tasmota has a configuration option : teleperiod. Default is 300 seconds, so 5 minutes. That is the period it will send new sensor data. Teleperiod can be set as low as 10 seconds.

Yeah, I’ve set that to the minimum. But sometimes the sensors don’t post on time. The weird thing I noticed is that even if a sensor didn’t post for a minute home assistant assumes that is unavailable. I’m looking at the graph in grafana and the value of the binary sensor goes up and down like crazy if there is ever even a short period of no new values.

Seems like sensors can reference their own prior value. I came up with the following:

value_template: "{{ states('binary_sensor.return_warmer') == 'on' if (states('sensor.panel_input_temperature') == 'unavailable' or states('sensor.panel_return_temperature') == 'unavailable') else (states('sensor.panel_input_temperature') | float) < (states('sensor.panel_return_temperature') | float) }}"