Template trigger help

Hi
I need some help with a trigger I need to make.
I have these 2 temperatures

"{{ states('sensor.living_room_temperature_2')}}"
"{{ state_attr("climate.living_room_ac", "temperature")}}"

I need to make a trigger like this

trigger:
    platform: numeric_state
    value_template: "{{states('sensor.living_room_temperature_2'-state_attr("climate.living_room_ac", "temperature")}}"
    above: 1.5

but I haven’t manage to make it work (always get error in the developers tool - template

You get an error, because value template is not a valid option for numeric state triggers. Just convert it to a template trigger and include the > 1.5 in the template.

Something like this(tyoed on the phone, so may have errors):

platform: template
value_template: "{{ (states('sensor.living_room_temperature_2') | int - state_attr('climate.living_room_ac', 'temperature') | float ) > 1.5}}"

I see. but my first problem is that doesn’t work in templates at all. Something is wrong with the syntax and I can not solve it

value_template: "{{states('sensor.living_room_temperature_2'-state_attr("climate.living_room_ac", "temperature")}}"

Your quotes are incorrect. Either single quotes outside the curly brackets and double quotes inside or vice versa.

do you mean like this because this is not working too

"{{states.sensor.living_room_temperature_2.state-state_attr('climate.living_room_ac', 'temperature')}}"
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Try my example I posted

It works, thank you.
( I hate templates)

1 Like

Hi
I have a problem sometimes with this template and I don’t know why.
I have change the template to

"{{ state_attr('climate.living_room_ac', 'temperature') | float  - states('sensor.living_room_temperature_2') | int < 1.5}}"

right now the climate temp is set to 23 and the sensor temp is 21.9 which is smaller than 1.5
however the the following automation haven’t triggered yet.

alias: Living AirCon Low Speed
  trigger:
    platform: template
    value_template: "{{ state_attr('climate.living_room_ac', 'temperature') | float  - states('sensor.living_room_temperature_2') | int <= 1.5}}"
  action:
    - service: climate.set_fan_mode
      data:
        entity_id: climate.living_room_ac
        fan_mode: low

I think it will work later when for example the sensor temperature goes up to 22.3 (a little higher) because it did worked the previous days. So I guess that it doesn’t reading the right time the living room sensor.
Should I add a time pattern trigger to check the values every 15 minutes?
Do you see any mistakes in the logic that I don’t?

(I don’t find the time pattern so efficient that’s why I am looking for a more elegant solution)

Try changing the int filter to a float filter as well.

If you put the template under Developer Tools -> Templates, does it show ‘True’?

The trigger should be evaluated every time either of the two temperature changes.

probably you are right!
I tested the template and showed false. When I changed the int to float it showing true.
I hope that this will do the trick, fortunately I asked.

1 Like