Say I change an entity, like a input_number slider in my front end. What is the order of things that happen? I mean like are template sensors’ values changed before state-based automations? I’m trying to debug an issue with one of my automations, and I’m trying to rule this out. Here’s the gist of it:
input_number:
my_number:
name: My Number
sensor:
platform: template
sensors:
my_number_sensor:
entity_id: input_number.my_number
value_template: "{{ states('input_number.my_number') | int + 2 }}"
automation:
- alias: my_number_changed
trigger:
- platform: state
entity_id: input_number.my_number
action:
- service: whatever
- alias: sensor_changed
trigger:
- platform: state
entity_id: sensor.my_number_sensor
action:
- service: foo
Can we know in advance, the order of what’s going to get executed? If I change input_number.my_number, will both automations see the updated value of sensor.my_number_sensor? Will only the sensor automation have the new value? Are these operations happening concurrently or linearly?
When an entity is changed that will cause a state_changed event. Automations generally watch for events (such as state_changed events) and will process its triggers and conditions when it sees one of the events it cares about. Template sensors also watch for events and will update when they see one of the events they care about.
So when you change the input_number, that will cause the template sensor to update, and it will cause the first automation to process its triggers and conditions to determine if its action(s) should run. The timing, however, can’t be guaranteed. All you will be able to count on is those things won’t happen until the event occurs. Which one starts first, whether they overlap in time to some extent, etc. you can’t know.
Now once the template sensor updates it will also cause another state_changed event, which will get the second automation going.
So, bottom line, only the second automation’s conditions and actions can be guaranteed to “see” the new value of the template sensor after the input_number changes. If you want the first automation’s conditions and actions to be guaranteed to see the same, then it should be triggered by the template sensor, not the input_number.
BTW, you don’t need entity_id: input_number.my_number in your template sensor configuration. It will be able to figure that out on its own.
Thanks for that. I was afraid that it would be non-deterministic. I guess Ill have to work around my issues.
I know that you don’t thave to put the entity_id row in template sensors, I just like seeing them listed there explicitly. I have had the case to put in sensor.time in that section, despite using now() in the template itself.
Home Assistant can monitor entities. It searches the template, identifies all entities, and monitors them.
sensor.time is an entity.
now() is a function.
It doesn’t monitor functions. So if your template contains now() and the expectation is Home Assistant will periodically evaluate it … it won’t (only on startup). By adding entity_id: sensor.time you’re instructing Home Assistant to monitor this entity. It triggers every minute (sensor_date triggers once a day) and evaluates the template containing now().