Not sure if this is possible. I know when you set up an automation you can set for: and it will trigger based on being in the new state from the previous state. Is it possible to set for: for the previous state though? I want an automation to trigger if it goes from on to off but only if it was on for more than 20 seconds, else I don’t want it to trigger and I can’t seem to figure it out. I considered templates, but I couldn’t figure out how to incorporate something similar to for: into a template either.
Thanks in advance! Bonus if we can set it for the previous state having been active between x amount of time.
Usage:
I am using this for automating the lights based on the state of my firetv. I don’t want it to keep changing the lights between every episode if I’ve changed them and only want it to trigger the lights if it goes from idle for 20 seconds to playing, but any way I set the automation triggers 20 seconds after going from idle to playing, even if it was only idle for 5 seconds.
NOT WORKING:
platform: state
entity_id:
- media_player.firetv_media_room
not_from:
- playing
for:
hours: 0
minutes: 0
seconds: 20
to: playing
So, I just tried this, and it seems really like what is supposed to solve it, but I am noticing that it is showing triggered every couple of seconds when I pause the show, even though the state isn’t changing? So for some reason it is updating and triggering even though it hasn’t changed making the automation fail since it hasn’t been 20 seconds in it’s eyes.
That might very well be the underlying AndroidTV integration. I’m experiencing these very fast state changes as well, but couldn’t find any reasons besides the no longer “fully supported” integration. And I have some more problems with the integration, so I thought, this is one of them.
The developer for the AndroidTV integration is short on time atm, so development has come to a halt or is very slow. Please don’t misunderstand, he’s doing this in his free and non-paid time, so things like this could happen.
Maybe you should check the history for the FTV with and without enabling this automation. You could at least see, if it is from the FTV (and the integration) or if something else is going on. Sorry, can’t be of much help here, but what I can say, the above automation should work!
I was able to make it work with this. I had to change it just a bit though. The greater than and less than was backwards according to my dev tools which makes 0 sense to me.
Trigger:
platform: state
entity_id:
- media_player.firetv_media_room
to: playing
Condition:
condition: template
value_template: >-
{{ trigger.from_state.state != 'playing' and trigger.to_state.state ==
'playing' and trigger.from_state.last_changed < utcnow() -
timedelta(seconds=20) }}
Thanks for the assistance!
Another question related to the last_changed attribute. Any way to get it to ignore restarts on template entities? I’m using it on a sensor I made to see when my cats ate last, but if HA restarts, it shows the last restart instead.