Automation state trigger with condition on time spent in previous state

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:

   {{ (now() | as _timestamp) - (trigger.from_state.last_changed | as_timestamp) > 30 }}

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.

1 Like

You can use for: in a state condition to accomplish what you want.

No, I cannot :slight_smile:

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…

condition:
- condition: state
  entity_id: light.your_light
  state: 'on'
  for: '00:00:30'

Maybe I’m not understanding you though.

That’s not what I want :slight_smile: It’s a subtle difference.

I want to trigger the automation when the light turns off.

trigger:
  - platform: state
    entity_id: light.my_light
    state: 'off'
condition:
  # light WAS on for 30 seconds.
  - condition: template
    value_template: >
      {{ (now() | as_timestamp) - (trigger.from_state.last_changed | as_timestamp) > 30 }}
action:
  - Do stuff.

Will the trigger.from_state.last_changed contain the time when it was switched ‘on’ (not ‘off’, because that should be right now)?

1 Like

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:

   {{ (now().timestamp() - trigger.from_state.last_changed.timestamp() > 30 }}

Is there really a difference?
image

So would this really not work?

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 :man_facepalming:

Ah! Well, there you go. That’ll teach me to test it using version 0.89. :slight_smile:

Works as advertised in 0.102 so you should be good to go.


Be sure to remove the blank space after the as in the first as_timestamp.

Screenshot from 2019-12-11 10-45-53

Definitely not. It’s triggering becuase the light is currently off, so the light cannot possibly have been on for 30 seconds.

Oh duh… I’m thinking of a button press event or something as the trigger lmao :man_facepalming:

I’ll show myself out now…

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.

Yeah, there’s no stand-in for the Trigger State object within the Template Editor so you’re obliged to test it live in an automation.

Maybe I should feel bad for picking my own post as the solution, but hey – I had it right! :slight_smile:

Why useless?

{% 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.

1 Like

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.

Ah, gotcha. I can tell you the answer is yes. :slightly_smiling_face:

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?