Comparing two Temperatures and set a binary value if the difference is larger then 2

Hi,

I am controlling my Roof Window with Home Assistant. If the outside temperature is 2 degrees higher then the inside temperature, then I am closing the Roof (to keep the heat out).

This worked the last summers but now I noticed that my binary sensor is not working anymore.

template:
- sensor:
  - unit_of_measurement: °C
    default_entity_id: sensor.temp_vergleich_vorraum_dach_3
    state: '{{ states.sensor.vorraum_temperature_template.state | float - states.sensor.weather_weewx_outtemp_template.state
      | float > 2 }}'
    name: temp_vergleich_vorraum_dach_3

What I am doing wrong, or better said, what changed? Home Assistant is not showing any error.

Best Regards,
Johannes

Basically, you’ve included 3 failure points…

  1. Using the state object method instead of the, recommended, states() function. The following warning has been in the docs for over 5 years:

  1. Not providing defaults for the float filter.
  2. Not providing an availability.

Some situations do not require both defaults and availability, but most situations will require at least one of them to be used to get reliable results.

While it’s not a technical error, you are also not using a binary sensor for binary output.

template:
  - binary_sensor:
      - default_entity_id: binary_sensor.temp_vergleich_vorraum_dach_3
        state: |
          {{ states('sensor.vorraum_temperature_template') | float - 
          states('sensor.weather_weewx_outtemp_template') | float > 2 }}
        name: temp_vergleich_vorraum_dach_3
        availability: |
          {{ has_value('sensor.vorraum_temperature_template') and
          has_value('sensor.weather_weewx_outtemp_template') }}

Remove this:

Your template reports true or false which is not a temperature value in degrees Celsius.

1 Like

Thanks,

I reworked it following:

template:
- binary_sensor:
  - name: "temp_vergleich_vorraum_dach_3"
    unique_id: "temp_vergleich_vorraum_dach_3"
    state: >
      {% set diff = (states('sensor.vorraum_temperature_template') | float(0)) - (states('sensor.weather_weewx_outtemp_template') | float(0)) %}
      {{ diff | abs < 2 }}

Now It is working again.

Would still add availibility template

1 Like