Template for comparing thermostat values

I want to create an automation that turns on a fan when the air temperature in the room becomes greater (or two degrees greater) than the set point of the thermostat in the room. What is the best way to do this?

The two entities I have from my thermostat for this are climate.t6_pro_z_wave_programmable_thermostat (set point) and sensor.t6_pro_z_wave_programmable_thermostat_air_temperature (air temp).

The Numeric State trigger seems like one possible way to achieve this but appears to be limited to just x is greater than or less than y. If I want the fan to turn on when the air temp is 2 or more degrees greater than the thermostat set point, it seems like I will need to employ a template. Can anyone provide an example of what the template would look like for this scenario? Thanks!

Sure:

- trigger:
    - platform: template
      value_template: "{{ states('sensor.t6_pro_z_wave_programmable_thermostat_air_temperature')|float(0) + 2 > state_attr('climate.t6_pro_z_wave_programmable_thermostat,'temperature')|float(0) }}"

Thanks! Plugging that into the Template Developer Tool returned TemplateSyntaxError: expected token ',', got 'temperature'

I reformatted the template as

"{{ states('sensor.t6_pro_z_wave_programmable_thermostat_air_temperature')|float(0) + 2 > states('climate.t6_pro_z_wave_programmable_thermostat,')|float(0) }}"

and the Template Tool returned “True”
Currently the thermo sensor reads 64 and the climate is set to 60. So this makes sense. But if I change the 2 in the template to 10, it still returns True. Shouldn’t the +2 (or whatever number) be on the other side of the > ?

So I tried moving the + 2 to the climate side of the > and it still returns True. Makes sense.

"{{ states('sensor.t6_pro_z_wave_programmable_thermostat_air_temperature')|float(0) > states('climate.t6_pro_z_wave_programmable_thermostat,')|float(0) + 2 }}"

I increased the 2 to 10, still true. Increased it to 64 (current air temp) and get “False.” So it seems like the climate value is being ignored if I add “+ x” after it.

There was a missing quote in @tom_l’s template:

      value_template: "{{ states('sensor.t6_pro_z_wave_programmable_thermostat_air_temperature')|float(0) + 2 > state_attr('climate.t6_pro_z_wave_programmable_thermostat','temperature')|float(0) }}"
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

Your “reformatting” looks like guesswork as opposed to trying to understand the original, and introduces a different error. The trouble is that people helping on here are doing it “blind” without access to your entities and system, so much of what we suggest is untested.

Your attempts return true, because the RH side of the equality is the state of a non-existent entity with 0 as a default value:

states('climate.t6_pro_z_wave_programmable_thermostat,')|float(0)
-----------------------------------------------------^

So your tests reduced to 64 > 2, 64 > 10 and finally 64 > 64.

Much better to very carefully inspect the provided code against the documentation (in this case, for state_attr) to see where the problem is. Even better, the error message pointed you towards the problem: expecting a quote but got temperature.

Thanks. Yeah it was complete guesswork as I have no prior experience with the template syntax. Just starting to learn. Your template worked, except that I think there is still the issue of adding the “+ 2” on the left side of the >. Isn’t that like saying “air temp + 2 is greater than thermostat setting?” What I want to know is when “air temp > thermostat setting + 2.” So would that be correctly coded as below?

"{{ states('sensor.t6_pro_z_wave_programmable_thermostat_air_temperature')|float(0) > state_attr('climate.t6_pro_z_wave_programmable_thermostat','temperature')|float(0) + 2}}"
1 Like

Yes, that’s right.