Template Binary Sensor is often 'on' or 'unavailable'

I have a template binary sensor that determines whether my HVAC fan should be on. This is in my templates.yaml file:

- name: 'Main Floor Fan Should Be On'
    state: >
      {% if is_state('sensor.basement_floor_heating_activity', 'heating') or (is_state('binary_sensor.somebody_is_home', 'on') and (is_state('binary_sensor.living_room_occupancy', 'on') or states('sensor.main_floor_temperature_differential') | float > 3 | float))%}
        on
      {% else %}
        off
      {% endif %}

As you can see, the sensor goes ‘unavailable’ quite a bit. In the history it seems this is triggered by either the basement floor heating mode being changed or the living room occupancy turning off. The state histories for these entities don’t have any ‘unavailable’ state in them:

image
image

I’m a bit puzzled as to why it works sometimes and goes unavailable other times. Any ideas?

what’s the history for sensor.main_floor_temperature_differential look like?

1 Like

image

I think I may have fixed it? I think I needed to put a default value for my float. I changed states(sensor.main_floor_temperature_differential) | float to states(sensor.main_floor_temperature_differential) | float(0) and it seems to work now!

1 Like

Found the actual problem… my entity ID for the temperature differential was incorrect, so it was returning unknown. D’oh!