I can make an automation trigger because an entity had been at a state for some time, but I’d like to have a trigger for a state change after that entity has been static for some time.
Practical example: my proximity sensor regularly switches between ‘stationary’, and either ‘towards’ or ‘away_from’ everytime my device tracker sees me moving. I’d like to be able to have an automation trigger only if that state changes after having been stationary for 20 minutes, so it wouldn’t trigger after I was stopped at a traffic light, for example, but it would if I had legit stopped somewhere.
This will still fire when the state changes, get ignored by the condition, and then never fire again. It won’t actually get triggered when the state of the sensor has remained unchanged for 60 seconds. It’ll only get triggered when it gets changed after being static for more than 60 seconds.
I think I actually need a timer, and a second automation to fire when the timer goes off, and do the action after verifying that the sensor hasn’t changed.
First, this won’t actually do what you think. When the state changes the last_changed field will be updated to basically now, so this automation will never run its action.
What you need to look at instead is trigger.from_state.last_changed. That will be the last time its state changed (i.e., before this recent change.)
Next, have you changed your requirements? It was pretty clear that originally you were asking how to run an action when a sensor changed, but only if it hadn’t changed for a specified amount of time before that. Now you seem to be asking for something different – i.e., run an action some amount of time after a sensor changes state. So which is it?
Ok, fair enough. And, yes, unfortunately you are correct. To use the for option you need to specify to. I think you’re also correct in that the best way to implement this is probably with a timer. Maybe something like:
The first automation will start, or restart, the timer. The second automation will run the action when the timer finishes.
Note that the condition of the first automation makes sure that only real state changes will (re)start the timer. I.e., attribute only changes will not affect the timer. If the sensor doesn’t have any attributes (that change), then you can probably do without the condition.
I had a case for this too. I ended up using a timer and splitting my automation into two: one that started the timer when the state moved from the one I was tracking, and the other when the timer finished and did whatever the single automation was supposed to do
Yeah, I see that as a definite need. I would often want a sensor automation to fire if it was “not off” for more time than a short blip to ‘unavailable’ and back.
Unfortunately, the for option is only available on the state trigger, not template. I suppose you could define a template (binary) sensor and then use the state trigger for that though.