Turn off lights if on for a certain time

Hey all,

I’d like to turn off a few lights (individually) in the case that someone leaves them on for a certain amount of time (say 30min)

I found an example of using timed switches here Example: Timed Switches but I can’t figure out how I can modify it so that I don’t have to create a separate script for each light I want it to apply to (each light must have its own countdown).

Is this possible or would I need a script per light?

If you don’t want to keep adjusting the time in the front end, you don’t need those scripts at all and can just use a simple automation for each light, this turns my shed light off 5 minutes after it’s turned on…

- id: shed_light_timer
  alias: Shed light timer
  hide_entity: False
  trigger:
    platform: state
    entity_id: light.shed
    from: 'off'
    to: 'on'
    for:
      minutes: 5
  action:
    service: homeassistant.turn_off
    entity_id: light.shed

You could use if else statements to create a does all automation if you really want to but cut/paste/amend is probably quicker.

It’s possible to use a group but then that would mean that every light would be turned off in that group after ‘x’ amount of time.

THis will not work. If the light was turned off before 5 mins and then turned back on it will switch off the light. Need to check if the light was actually continuously On for 5 mins before switching off.

This does not make sense… have you actually tried this? I don’t see any reason why this would not work unless there is another automation on your system doing this.

for:
  minutes: 5

Does mean it has to be on continuously for 5 minutes.

Of course! That is what he wanted.
If you manually switch off then on again, it’s retriggered for another 5 minutes.
I’m not seeing a problem with this.

Yep. I was agreeing with you.

1 Like

Sorry i was using ‘delay’ under action and it was not working. I am trying this now.

Which is in fact the only part necessary for the trigger?

to: 'on'
    for:
      minutes: 5

Just refreshing all my automations to be as economical as possible, and was told the “from: off” here could be left out (and all other comparable automations of course…)
Havent run into trouble yet :wink: What do you think?

For boolean triggers you can indeed leave out the from: without problem.

Is there any way to only automatically turn off the switch if it was manually turned on (ie. not via an automation)?

Yes but it is messy at the moment. For each automation you use to change the state of the switch you will also have to record that it was done by the automation using an input_boolean or mqtt binary sensor. You will also have to clear this when the switch is turned off.

I think I read somewhere that the ability to determine this is on the roadmap to be included in HA.