Sensor template producing strange result

Can anyone explain why this sensor template is getting set to the value of gw2000b_indoor_temperature?

    - name: Temp Difference
      unique_id: temp_difference
      device_class: temperature
      state_class: measurement
      unit_of_measurement: °F
      state: "{{ states('sensor.gw2000b_indoor_temperature')|float(0)|round(2) - states('sensor.outdoor_temperature_sensor')|float(0)|round(2) }

I thought it might have to do with there being a current difference of -2.2 degrees with outdoor_temperature_sensor being greater, but I don’t know what to do to fix this. It accurrately showed a value of -0.2 degrees F when the difference was small.

First, it’s probably just a copy/paste error, but double check that the real sensor template isn’t missing }" like what you have posted.

Is sensor.outdoor_temperature_sensor unavailable or unknown? That would be the most likely cause. The fix to avoid this issue would be to set an availability to require both sensors to have values in order for the difference to be calculated.

    - name: Temp Difference
      unique_id: temp_difference
      device_class: temperature
      state_class: measurement
      unit_of_measurement: °F
      state: |
        {{ (states('sensor.gw2000b_indoor_temperature') | float(0) -
        states('sensor.outdoor_temperature_sensor') | float(0)) | round(2) }}
      availability:  |
        {{ has_value('sensor.gw2000b_indoor_temperature') and
        has_value('sensor.outdoor_temperature_sensor') }}

2 Likes

I checked the sensor values individually in Developer Tools, which is how I know the difference was -2.2 (gw2000 was 66.2 and outdoor sensor was 68.4).Now that I think about it I don’t think I checked temp_difference in Developer Tools, but I noticed the 66.2 value for temp_difference on an Entities card in Lovelace, so maybe it was a case of the card got updated at a time one was unavailable. Thanks for the explanation. Anyway, it’s working fine now with no change to anything.