Turning off lights after period of time

I want to ensure that lights are turned off if they have been left on for a period of time. I am using an automation with a hold state trigger which under normal circumstances works ok, however if Home Assistant is restarted or if I reload automations the automation gets cancelled and the light is left on.

I can get around Home Assistant restarts by creating an automation triggered by the Home Assistant Start event which turns off the light:-

alias: test_light_off
id:  01872a30-b179-11eb-8529-0242ac130003
trigger:
  - platform: state
    entity_id: light.lounge_table
    to: 'on'
    for: 
      minutes: 10
action:
  - service: light.turn_off
    entity_id: light.lounge_table

However this obviously does not handle automation reloads.

How is anyone else handling this?

1 Like

Have you tried an input_boolean flag to indicate whether the lights are on or off?

OK, I think I have come up with two possible solutions. Both involve adding an additional trigger to the automation:-

  - platform: event
    event_type: 'automation_reloaded'

or

  - platform: state
    entity_id: automation.test_light_off
    to: 'on'
    from: 'unavailable'
    for:
      minutes: 1

Can‘t this be done using initial_state: on ??

You could set an input_datetime helper to the current time + the duration you want the lights to remain on when you turn the lights on, and have another automation that triggers on that input_datetime to turn them off. That way, your lights don’t go off when you reload automations.

Set up an input_datetime called lights_off_check (under Configuration / Helpers), then on the end of your automation that turns the lights on (where 3600 is the duration in seconds), add this:

- service: input_datetime.set_datetime
  target:
    entity_id: input_datetime.lights_off_check
  data:
    timestamp: "{{ now().timestamp() + 3600 }}"

Then your light_off automation can simply trigger on the input_datetime:

trigger:
  - platform: time
    at: input_datetime.lights_off_check

As far as I am aware initial_state: on just enables the automation to be triggered, It does not actually trigger the automation.

As you have already discovered, anything in Home Assistant that performs a countdown (for, delay, timer, etc) is reset by a restart or reload.

The suggested triggers are a good ‘fail-safe’, and ensure the light is not left on indefinitely but, as you know, don’t take into consideration any remaining time the for may have had prior to being reset (the remaining time is lost so it’s not possible to restore it).


FWIW, if you use timers that must be restored after a restart, I created a means of doing that here:

I am trying the method suggested by Troon. So far it seems to be working a treat and a lot easier to debug if things go wrong.

Thanks

That’s a good suggestion as long as one understands there remains a narrow window of opportunity for the lights to remain on. If the restart/reload occurs moments before the Time Trigger is scheduled to occur, it will not trigger and the lights will remain on.

I think an easy way to seal this tiny opening is to add a second trigger to the “turn off” automation:

  - platform: event
    event_type: 'automation_reloaded'

(Possibly even check for a restart event as well.)

Add a condition to check if the current time is at or later than input_datetime.lights_off_check and it will ensure the light gets turned off, even it it missed its Time Trigger.

2 Likes

Good idea, I will add that as well.