I have an automation that fires on various events, one of them is when one entity values becomes greater-or-equal to another entity:
- trigger: template
value_template: |-
{{ states('sensor.my_sensor')|int >= states('input_number.my_threshold')|int }}
But because this automation also fires on various other events that all perform the same job (when main switch is turned off, when HA starts, etc), I have to run the if statement in the actions block, checking again if {{ states('sensor.my_sensor')|int >= states('input_number.my_threshold')|int }}
How, and is there a way (trigger variables, variables maybe?) that would allow me to define the variable in the automation, like so:
# Create global variable
my_var: {{ states('sensor.my_sensor')|int >= states('input_number.my_threshold')|int }}
# Use it in the trigger
triggers:
- trigger: template
value_template: '{{ my_var }}'
# other triggers
actions:
- if:
- condition: template
value_template: '{{ my_var }}'
then:
....
The objective is to not repeat myself with the same code blocks.