Turn a light off after a period of time if turned on between certain hours?

I tried to search for something like what I’m looking to do, but not sure I can come up with a good way to search for it, as nothing looked like what I’m looking for.

My daughter has a new trick - she has been getting up in the middle of the night and turning her light on, then going back to sleep, so I’d like to see about setting something up to turn it back off after 30 minutes or so, but only if it’s turned on within a certain timeframe, say between 9pm and 6am. I’m sure this can be done, just not sure how to accomplish it.

Any suggestions?

You can use a trigger based on state change:

A condition based on a time frame:

And a delay in the action to turn off the light:

1 Like

It’s a pretty simple automation. Something like this:

  automation:
    alias: 'turn light off at night'
      trigger:
        platform: state
        entity_id: light.bedroom
        to: 'on'
        for:
          hours: 0
          minutes: 30
      condition:
        condition: time
        after: '21:00:00'
        before: '06:00:00'
      action:
        service: light.turn_off
        entity_id: light.bedroom
1 Like

Unless she turns it on at 5:31 :wink: I’m sure your answer would suffice for his use case, but being strict to the question it would fail at the extreme.

You would need to look at last_changed and do some funny work in a template for it to be exactly as the question asked. I’m just nitpicking :slight_smile:

It’s true. You’d have to adjust the time to like 6:30am. The other way, which is more complicated, is not use the “for 30 minutes”, but instead call a script that has a 30 minute delay. But I think it may be overly complicated and unnecessary for what you’re looking for.

Haha yep, or a third was like I was alluding to would be to look at the last changed state in a template and compare it to datetime now. This would prevent the delay floating out there.

Fun for thought.

That’s a good option too!

I used following in my scenario where my kids forget to turn off deck light off when our dog comes back inside. I got this idea from someone who posted on this forum for a different scenario.

This automation turns off deck light (zwave switch) after 10 minutes.

input_slider:
   deck_light_time:
    name: Deck light Timer
    initial: 10
    min: 1
    max: 60
    step: 5

script:
  decklight_timer:
    alias: "Deck light Timer"
    sequence:
      - delay: '00:{{ states.input_slider.deck_light_time.state | int }}:00'
      - service: homeassistant.turn_off
        data:
          entity_id: switch.ge_12722_onoff_relay_switch_switch

automation:

  # turn off deck light  if that light is on for more than 10 minutes (or specified via slider)
  - alias: "Decklight: Automatically turn off"
    trigger:
      - platform: state
        entity_id: switch.ge_12722_onoff_relay_switch_switch
        to: 'on'
    action:
      - service: script.turn_on
        entity_id: script.decklight_timer
      - service: script.notify_me
        data_template: {"value1":"Deck light was turned off after", "value2":"{{ states.input_slider.deck_light_time.state | int }} minutes"}

  - alias: "Decklight: reset timer"
    trigger:
      - platform: state
        entity_id: switch.ge_12722_onoff_relay_switch_switch
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.decklight_timer
      - service: script.notify_me
        data: {"value1":"Deck light timer was reset!", "value2":""}


group:
  Timers:
    entities:
      - input_slider.deck_light_time