I just want to make sure I’m thinking about this correctly. I want to have an automation run if I turn off a light, but only if it had been previously on for more than 30 seconds. So a quick flip would NOT trigger the automation. Am I correct in thinking that I can do this:
Will the from_state contain the length of time that it remained in the previous state? I’m assuming that since the trigger fired, that now() is the time of the state change.
I don’t want to know that it’s been ‘off’ for 30 seconds. I want to know if it’s been ‘off’ after having been ‘on’ for 30 seconds. I thought I could do that as well, and I tried that, but then I had to figure out why it didn’t work…
Your template should do the trick but you’ll need to revise it a bit because as_timestamp is available as a function and not a filter. Alternately, you can write it like this:
trigger:
- platform: state
entity_id: light.your_light
to: 'off'
condition:
- condition: state
entity_id: light.your_light
state: 'on'
for: '00:00:30'
I’ve never used for: in a state condition, so maybe I’m just misunderstanding how that’s supposed to work.
Edit: I’m dum. I’m thinking of the trigger being something like a button press event even though I literally wrote out the light turning off as the trigger
Thanks for being my sounding board here. It’s difficult to test things that use trigger because the template editor is useless…you have to do it in a real automation situation.
{% set trigger = {'from_state': {'last_changed': now()}} %}
Ok, I’ll grant you “difficult”, especially in this case when you’re dealing with a Python datetime object, but you can test templates that use the trigger variable in the template editor. You just have to create the trigger variable by hand. In fact, it might be easier to test this way than trying to cause all values the trigger variable might take on during normal run time.
Yep. That’s a good way to test things, and I have done that sort of thing before, but my question in this case wasn’t really about whether or not the template would work, but rather if the trigger.from_state.last_updated field would have the value I thought it would.
Clever strategy to allow for testing the template’s basic functionality. If the template then fails to work in the automation, you know the real world has supplied something different than what one predicted.
So I am in the process of converting my home system from Domoticz to Home Assistant. So far, after getting used the way things are setup, things are going well. I was searching for a way to duplicate one of my Rule sets in Domoticz when I came across your post.
I have a 3 gang switch in my hall. One switch does the hall light, one the dining room, and the last doesn’t have a physical light to control. I used it as a bedtime/panic switch before. Turn it on and leave it that way, it turns its self off after 3 seconds and turns off the inside lights after a set time and then the outside lights. Turn it on and back off in under 3 seconds and it turns on every light in the system right away.
So my question is could I modify your "as_timestamp) > 30}} to be >4 for one automation and <3 for another to duplicate what I did on my other system? Or… is there a better way to set it up?