Move template to trigger - not working

Before i had a rule which checked every time for a numeric state of a sensor and then in conditions i had the following

- condition: template
  value_template: '{{ states.sensor.mys_kitchen_hood_72_4.state | int - states.sensor.mys_kitchen_hum_avg_mean.state |int > 5}}'

And everything was working fine. But i need to move it to a trigger so i did it like below and it’s not working. So i can only use trigger:template for this?

  trigger:
    - platform: numeric_state
      entity_id: sensor.mys_kitchen_hood_72_4
      value_template: '{{ states.sensor.mys_kitchen_hood_72_4.state | int - states.sensor.mys_kitchen_hum_avg_mean.state |int }}'
      above: 5

I would create a new sensor using the template to calculate the result then hide it and use it in the trigger.

1 Like

You can use the template trigger with the same template from the condition.

automation:
  trigger:
    platform: template
    value_template: '{{ states.sensor.mys_kitchen_hood_72_4.state | int - states.sensor.mys_kitchen_hum_avg_mean.state |int > 5}}'
2 Likes

Thanks. That’s an idea. Just thought that templates are supported in all kinds of trigger. Seems they are supported only in templates

By the way does it support negative numbers?

  value_template: '{{ states.sensor.mys_kitchen_hood_72_4.state | int - states.sensor.mys_kitchen_hum_avg_mean.state |int < 15}}'

This seems have triggered when the difference was less AVG 80 Trigger 75

It should handle negative numbers just fine, but I don’t quite understand what you mean by “the difference was less AVG 80 Trigger 75.”

You can test your template by copying it into the “template” field on the templates page of home assistant, under developer tools. Just replace the Paulus / Anne Therese example. It’s super helpful, and I would have never been able to get a template to work without it!

You can also spoof sensor values using the “states” developer tool, but I often find it easier to just tweak my template to test it (e.g. change the 15 in your template to a different number, just temporarily).