"last_changed"

Hi all,

I hope you can help, I’ve been trying to use “last_changed” as a trigger to turn a light off & I’ve been messing about with it for the last few days, just can’t seem to get it to work.

I want this Lifx light to turn off (but only after 5 mins of NO motion) and I can’t seem to get it to turn off = the trigger that turns it on works fine.

I started with one automation, that would turn on, then turn off & I couldn’t get it working so now I’ve added an Input Boolean and tried splitting them into two in the hope that would work better.

I’ll post the YAML below, but I think I’ve made that many changes that now I’m a bit lost (if you know that feeling & would appreciate if someone could point me in the right direction). `- id: ‘123’
alias: Hallway Motion to turn Light OFF AFTER NO Motion
trigger:

  • entity_id: input_boolean.hallway_motion
    from: ‘on’
    platform: state
    to: ‘off’
    condition:
  • condition: template
    value_template: “{% if as_timestamp(now()) - as_timestamp(states.input_boolean.hallway_motion.last_changed) > 2 %}true{% endif %}”
    action:
  • data:
    entity_id: bedroom_main_light
    transition: 3
    service: light.turn_off
  • id: ‘888’
    alias: Hallway Motion to Turn on Light after dark
    trigger:
    • entity_id: input_boolean.hallway_motion
      from: ‘off’
      platform: state
      to: ‘on’
      condition:
    • condition: state
      entity_id: light.bedroom_main_light
      state: ‘off’
    • condition: state
      entity_id: switch.hallway_lamp
      state: ‘off’
    • condition: state
      entity_id: sun.sun
      state: below_horizon
      action:
    • data:
      brightness_pct: 8
      color_temp: 400
      entity_id: light.bedroom_main_light
      transition: 1.5
      service: light.turn_on`

I think I have tried that many variations of above that I just don’t know what else to try, I thought Value Templates would do the job. but I just can’t get them working, am I missing something? If someone/anyone can point me in the right direction, much appreciated.

Please follow the directions at the top of the page when posting YAML code.

I’m not sure I understand everything you want to do, but if you want an automation to trigger when an entity is in a particular state for at least a certain amount of time, then use the for: parameter with a state trigger:

  trigger:
    platform: state
    entity_id: ENTITY_ID
    to: SOME_STATE
    for:
      minutes: 5

See State Trigger.

1 Like

My apology’s, I did use that little button to post the Yaml. Not too sure why it’s turnds out like that.

What I’d like is for a motion sensor to turn on my light in the hallway (I’ve got that not working just fine). And to only turn it off after 5 minutes of no motion in that area.

I tried your suggestion this morning and it didn’t manage to turn the light off.

Many thanks

You need to format right after you copy and paste the code into the post.

If you try something and it still doesn’t work, it helps to post the code as it is now so we can see if there’s something wrong. Just saying it doesn’t work isn’t very helpful.

Have you tried something like this:

- alias: Turn light off
  trigger:
    platform: state
    entity_id: input_boolean.hallway_motion
    to: 'off'
    for:
      minutes: 5
  action:
    service: light.turn_off
    entity_id: light.bedroom_main_light
    data:
      transition: 3

BTW, in your original post, you didn’t enter the full entity_id of the light when calling the light.turn_off service. You just had bedroom_main_light instead of light.bedroom_main_light. That would definitely prevent it from working, regardless if you had the triggers and conditions correct.

Hi @pnbruckner,

Many thanks for all your help, the light now turns off. You were right in your last post in that it was missing the full format light.bedroom_main_light.

I’ve been trying & trying to get this working on my own, making so many changes and I think I got a bit lost.

Many thanks for your help.

1 Like

I’ve been using these two automation for the last few weeks to turn the hallway light on/off after sunset, but I’ve noticed that after midnight the trigger stops working.

I tried to add a “before sunrise” condition but it caused to trigger to fail completely.

Is there a way to get this automation to work while the sun is below the horizon?

- id: ''
  alias: Hallway Motion to Turn on Light after dark
  trigger:
  - entity_id: input_boolean.hallway_motion
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.hallway_main_light
    state: 'off'
  - condition: state
    entity_id: switch.hallway_lamp
    state: 'off'
  - after: sunset
    after_offset: -01:00:00
    condition: sun
  action:
  - data:
      brightness_pct: 8
      color_temp: 400
      entity_id: light.hallway_main_light
      transition: 1.5
    service: light.turn_on


- id: ''
  alias: Hallway motion to turn LIGHT OFF
  trigger:
  - entity_id: input_boolean.hallway_motion
    for:
      minutes: 5
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: light.hallway_main_light
    state: 'on'
  action:
  - data:
      entity_id: light.hallway_main_light
      transition: 5
    service: light.turn_off

Many Thanks

Yes. Change your condition to:

  condition:
  - condition: state
    entity_id: light.hallway_main_light
    state: 'off'
  - condition: state
    entity_id: switch.hallway_lamp
    state: 'off'
  - condition: or
    conditions:
    - after: sunset
      after_offset: -01:00:00
      condition: sun
    - before: sunrise
      before_offset: 01:00:00
      condition: sun

Of course, adjust the before_offset as needed.

1 Like

Many thanks