Turn light off after X minutes, no matter how it was turned on

I want to make an automation to turn a light off, after X minutes. No matter how the light was turned On.

I have tried all kind of combinations, and tried all the tips I found googling. But the automation never seems to get triggered. I have changed the trigger to check from ‘Off’ to ‘On’, also added the delay to the action instead of the trigger. But it just seems that the automation never triggers.
(just using 10 in the code below to check the automation in 10 seconds)

When I click execute on the automation, it does turn off the light.

alias: Slaapkamer uit na 30 minuten
description: ''
trigger:
  - platform: state
    entity_id: light.54802417d8f15bcdfb49
    to: 'On'
    for: '10'
condition: []
action:
  - type: turn_off
    device_id: 9c6bd3a09a47421fb7dd732cf6a0db1f
    entity_id: light.54802417d8f15bcdfb49
    domain: light
mode: single
2 Likes

your for condition is wrong. Also i changed the action to call service. Try this. It will turn off the light after 10 minutes.

alias: Slaapkamer uit na 30 minuten
description: ''
trigger:
  - platform: state
    entity_id: light.54802417d8f15bcdfb49
    to: 'on'
    for: '00:10:00'
condition: []
action:
  - service: light.turn_off
    data: {}
    entity_id: light.54802417d8f15bcdfb49
mode: single
8 Likes

Thanks!
The main thing that I was doing wrong was using ‘On’ instead of ‘on’

So a silly Case senstive mistake, I feel stupid now.

1 Like

I just wanted to say thank you for this post. It really helped me!

I made a automation that turns off the lights I have on the outside. Usaually they are turned on via a physical Ikea on/off switch, but they are rarely needed for more than 10 minutes.

I really like Home Assistant and the whole smart home thingy, but I do wish it wasn’t so dam-ed complicated in some ways…

Anyways, thank you again!

1 Like

Thanks guys,

I accidentally stumbled upon this post and thought, Hey I can use that.
Now My bathroom light turns off after 10 minutes.

Thanks for the inspiration.

Thanks for posting this. For those of you who aren’t sure what to do with the code above, I’ve created it as a blueprint: Turn a switch off after a defined set of time (simple switch automation) enjoy :slight_smile:

2 Likes

it only for switch… if possible to create this for the light?

thx

As requested: Turn a switch off after a defined set of time (simple switch automation) - #2 by JonTheNiceGuy

nice!
thanks

I was just attempting to use the switch version of the blueprint, the time input is a little confusing. Wouldn’t a “duration” selector instead of a “time” selector be more appropriate? In it’s current state it’s looking for a time of day instead of length of time.

1 Like

Hey @drodegeb thanks for that! I’d completely missed that there was a duration selector! I’ve fixed that - it’s at a new gist location!

Hi all,

Currently I have a motion sensor and upon triggering it will turn on.
I want it to turn on for 15mins and if I re-trigger again, I don’t mind that the counter is resetted and then turn on for 15mins.

Thanks

You can do that with a standard automation like this. It will turn the lights on for 10 minutes. If it detects movement within that 10 minutes, it will just extend it to 10 minutes again. I use it in my bathroom and also have a ventilation automation connected to it. So when I leave the bathroom, the lights (and ventilation) stay on for max 10 more minutes and then turn off.

alias: Badkamer - Lampen aan bij beweging
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 6f53cec33b35905bd452816ac6a4d3d6
    entity_id: binary_sensor.tradfri_motion_sensor_badkamer
    domain: binary_sensor
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.badkamer
    data:
      brightness_pct: 100
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.badkamer
    data: {}
mode: restart
max: 10
2 Likes

I just wanted to thank you for posting this. I’m new to HA and this helped me.

2 Likes

Thank you for this, I’m still new to this and had spent an age trying to get this to work. This after already pulling my hair out because my light was showing up as an entity rather than a device when turning it on.

1 Like

Hope someone can help me, unfortunately I’m not experienced with creating rules.

Always get error “Message malformed: extra keys not allowed @ data[‘0’]”

  • alias: Turn off via motion detector
    description: “test”
    fashion: single
    triggers:
    platform: state
    entity_id: binary_sensor.lumi_lumi_motion_ac02_occupancy
    to: ‘off’
    for:
    minutes: 10
    action:
    service: light.turn_off
    entity_id: light.sofa

Thanks in advance

Hi Angelo, please format your code properly: from the How to help us help you - or How to ask a good question => 1.1 Format it properly

Have a look in this result: Search results for 'Message malformed: extra keys not allowed @ data[‘0’]' - Home Assistant Community

Hi fellow Dutchman.
Does this work when you switch the light on bij putting the power on it with a normal switch?
I can’t get that to work.
My kids have a tendency of not switching off the light on the overloop. No idea what overloop is in English.
Works when i run the automation manually.
I use Zigbee lights from Lidl.

alias: Zet lamp Overloop uit
description: ""
trigger:
  - platform: state
    entity_id:
      - light.lamp_overloop_light_4
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition: []
action:
  - type: turn_off
    device_id: a8993e58f2802892ff41a715d5620f04
    entity_id: light.lamp_overloop_light_4
    domain: light
mode: single

Hi,
I don’t know if this would work by using the normal power button. I have disabled all my normal powerbuttons in my house (by adding a bit of tape while its in the OFF position). HA will probably not get the right event here, as the device is not available when it is completely turned off. Perhaps there is a way to detect that the light is connected to HA again and then do this check, but not sure if that can be detected.

1 Like

Thanks,
I have a Shelly 1 available.
Will try that.