Template Binary Sensor to compare 2 temperatures ist not working as expected

Hello ,

making my first steps with template binary sensor. I have to Zigbee Thermometers, and I want to show “heat”, when one of the temperatures is more then 2 degrees above the other.

Here the template:

 name: "Heizkörper Garderobe"
      unique_id: "HK-Garderobe"
      device_class: "heat"
      state: >
        {% set t1 = states('sensor.lumi_lumi_sens_e3e98705_temperature') | int  %}
        {% set t2 = states('sensor.lumi_lumi_sens_temperature')  | int %}
        {{ t1 - (t2 + 2) }}

But this shows almost always “heat”, but this is not what I expect.

Screenshot 2021-06-04 163451

It seems that it schows “cold” only when the Difference is exactly 0.0 .

What do I wrong here?
BTW: If I modify the sensor to:

state: >
        {% if 2.0 | float < states('Temp-Diff-HK-Luft-Garderobe')  %}
          True
        {% else %}
          False
        {% endif %} 

I get “unavailable” most of the time.

clueless, muellthos

      state: >
        {% set t1 = states('sensor.lumi_lumi_sens_e3e98705_temperature') | float %}
        {% set t2 = states('sensor.lumi_lumi_sens_temperature')  | float %}
        {{ t1 - t2 > 2 }}

Assuming you want it to be on when t1 is 2° larger than t2. Otherwise swap t1 and t2.

Yes, exactly so, t2 should be at least 2 degrees higher than t1.

then it is:

{{ t2 - t1 > 2 }}

Oh boy, am I stupid :hot_face: t1 should be the higher temperature .

But this is not my problem. Look at the picture in the initial post:

I’d expect, that I see “normal” when the red curve ist below the blue one (at least).

Binary sensor templates are required to resolve to true or false. Your fist template resolves to a number, when this number is 0 it is interpreted as false.

Your second attempt has so much wrong with it that I gave up listing the issues.

As I said above, use this.

      state: >
        {% set t1 = states('sensor.lumi_lumi_sens_e3e98705_temperature') | float %}
        {% set t2 = states('sensor.lumi_lumi_sens_temperature')  | float %}
        {{ t1 - t2 > 2 }}

The “greater-than” test resolves to true or false as required.

Sorry for being annoying. I had checked that. Nevertheless, the display is not correct. It should display “normal” between 0:30 and 6:30 and from 13:30 (approximately), but not “heat”. Maybe this is also due to the display in Lovelace, or the way Home Assistant works internally. In any case, the two thermometers send updates at different times.

The new sensor template is not going to change past history of the old one. Keep watching what happens from now on…