For no other reason than just being curious (no specific task at hand) how is for:
implemented? How does the code know that a state (or attribute) has been continuously in the specific state (or attribute value) to trigger after the for:
time?
I tried to look at some of the code and an older post, but got a bit lost.
Take a simple automation:
alias: "Test: for"
description: ""
triggers:
- trigger: state
entity_id:
- light.living_room
to: "on"
for:
hours: 0
minutes: 0
seconds: 30
conditions: []
actions: []
mode: parallel
max: 10
My simple assumption is when the light-turned-on-event happens the code then sets another listener to fire at the for:
time (in 30 seconds in this example).
When that timeout/event happens at 30 seconds what test is done to determine that the light has been on continuously for that entire 30 seconds?
Initially, I imagined that when the 30 second event fires that the code then checks if the state is still on
AND that the last_changed time <= now() - 30 seconds.
But that approach doesn’t work for attributes because last_updated will change with any attribute update.
For example:
triggers:
- trigger: numeric_state
entity_id:
- light.living_room
attribute: brightness
above: 200
for:
hours: 0
minutes: 0
seconds: 30
Works as expected even if I update the friendly_name, which updates the last_updated timestamp in the middle of the 30 second delay.
Or does the code work by canceling the waiting for:
if the state (or attribute value) changes away from the expected state?
Again, everything works as expected. I’m just curious how it works.
Thanks.