How to Turn Light on only for 2 minutes?

Hi. How do you code it when you only want the light to turn on for a certain amount of time then turn off automatically?

For example, I want to do the following automation: If the front gate door (binary_sensor.front_gate_door) opens and it is night, then open flood light (light.back_yard_flood) only for 2 minutes.

Thanks for your help!

1 Like

something like this?

- alias: Turn Light on only for 2 minutes
  trigger:
    - platform: state
      entity_id: binary_sensor.front_gate_door
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
  - service: light.turn_on
    data:
      entity_id: light.back_yard_flood
  - delay: 00:02:00
  - service: light.turn_off
    data:
      entity_id: light.back_yard_flood
5 Likes

Awesome. Thanks a million man!

1 Like

FWIW, @lolouk44’s solution will probably suit your needs just fine. However, there is one issue with this - if the front gate door is closed and opened again before the two minutes is up, the light will probably immediately go off. This is because triggering an automation while it’s actions are still running from a previous trigger event usually doesn’t work very well.

A more robust solution would be to turn the light on and start a two minute timer. If the front gate door is closed and opened again before the timer expires, the timer will just be restarted. Then, when the timer finally expires, trigger another automation that turns the light off. Like I said, the simpler solution will probably be fine. But if you’d rather account for the scenario I described, and you need help writing a corresponding implementation, just let me know.

Thanks @pnbruckner! I am so new to this I need all the help I can get. I would love to see then how you would write the “code” with the two minute timer. Appreciate it!

Thanks for your input, though I don’t see how opening and closing the door quickly will turn the light off immediately?
There doesn’t appear to be an automation (or a request for one) to turn the light off when the door is closed. It’s purely “turn on the light FOR 2 min WHEN I open the door IF it’s night”
Or have I missed something?

So, this is based on observation, not documentation. I’ve found (through helping other people and my own experience) that if you trigger an automation with a wait or delay in the actions, while it’s still running from a previous trigger event, that the wait/delay effectively gets skipped. (I don’t know why, and I haven’t taken the time to try and figure it out. It’s just what I’ve observed.) So in this case, the light would be turned on (which it already is), then the delay gets skipped, and then the last step will run which turns the light off. Hence, the light goes off immediately.

In general I try to avoid automation action sequences that include waits or delays. Either I put them in a script which I cancel (in case it’s already running), then start, from the automation action, or I do something like I described above.

I don’t know for sure that your solution would “fail” in this way in the described scenario, I just think it probably will.

Thanks for the insights… I have only a small number of these so will run some tests and see if I can reproduce (though they’re usually short like 1 sec to allow for a light transition to take place)

FWIW, I think I found why this happens - i.e., if an automation’s actions are running, and “sitting” in a delay or wait_template, and the automation gets triggered again, the delay/wait effectively gets skipped past.

Note that the actions of an automation are basically a script, or at least, they are run as if they are a script. Now look at this block of code in the script implementation: https://github.com/home-assistant/home-assistant/blob/9a786e449b2a4ad598ad80da57b3f664d14b4204/homeassistant/helpers/script.py#L82-L84

If the script is “turned on” again while it’s already “on” (i.e., run again while it’s already running), and it’s in a wait for a delay or wait_template, the delay/wait simply gets canceled, and the script picks up with the next step!

Thanks for the details as always :slight_smile:

I know this is an old thread by maybe somebody can help me. I am trying to do the same thing as OP but can’t seem to get it to work, the light will turn on but I can’t get it to turn off after 2 minutes. I have tried:

  alias: turn on kitchen light on motion
  description: ''
  trigger:
  -entity_id: binary_sensor.aarlo_motion_living_room
  platform: state  
  condition: []
  action:
  - service: light.turn_on
    data:
      entity_id: light.kitchen_main
  - delay: 00:02:00
  - service: light.turn_off
    data:
      entity_id: light.kitche_main

I have tried using two different automations

- id: '1588114015350'
  alias: Test
  description: ''
  trigger:
  - entity_id: binary_sensor.aarlo_motion_living_room
    platform: state
  condition: []
  action:
  - data: {}
    entity_id: light.kitchen_main
    service: light.turn_on
- id: '1588114917286'
  alias: Test step 2
  description: ''
  trigger:
  - entity_id: automation.livingroom_motion_trigger
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - delay: '00:02:00'
  - data: {}
    entity_id: light.kitchen_main
    service: light.turn_off

Yet neither of them seem to work

Unfortunately there are a LOT of topics describing this same issue and solutions, but this one doesn’t seem to have a solution posted.

The basic idea is to move the actions into a script, and then in the automation stop the script (by turning it off) in case it is running, then start the script. So the basic outline is:

automation:
- trigger:
  - platform: state
    entity_id: binary_sensor.some_motion_sensor
    to: 'on'
  action:
  - service: script.turn_off
    entity_id: script.my_script
  - service: script.my_script
script:
  my_script:
    sequence:
    - service: light.turn_on
      entity_id: light.some_light
    - delay:
        minutes: 2
    - service: light.turn_off
      entity_id: light.some_light
5 Likes

Thank you pnbruckner that worked perfectly.

1 Like

I did mine with an if-then using pnbruckner’s approach to call it in a Script, but the same approach works directly in an Automation.

script:
    alias: Welcome Home Light
    sequence:
      - if:
          - condition: device
            type: is_off
            entity_id: light.some_light
        then:
          - type: turn_on
            entity_id: light.some_light
          - delay:
              minutes: 3
          - type: turn_off
            entity_id: light.some_light
    mode: single
    icon: mdi:lightbulb-auto-outline
1 Like

I don’t know when the mode was added, I didn’t know you could use it in a script. Per:

I think you want restart rather than single so that it just kills the original one rather than not running a new instance - so for example if motion was detected again within 3 minutes the light will stay on another 3 minutes.

I just set up something like this and used restart but haven’t fully tested it.

Then I will add my two cents :slight_smile:
I’ve created a helper switch to determine if the lights were turned on by motion.
In combination with hue lights this is working good for my situation.

When motion detected and it is between certain times → check the trigger ID and if the lights are off. Then turn lights on, and set helper switch to on.

If helper switch is on for 10 minutes, turn lights off.

I did not had the situation yet that there is a need to override the helper switch yet.

alias: Beweging beneden
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_woonkamer_motion
    to: "on"
    id: Beweging_Gedetecteerd
  - platform: state
    entity_id:
      - input_boolean.ingeschakeld_door_beweging_woonkamer
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: 7 minuten na motion
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: person.x
        state: home
      - condition: state
        entity_id: person.y
        state: home
  - condition: or
    conditions:
      - condition: time
        after: "00:00:00"
        before: "08:00:00"
      - condition: time
        after: "23:00:00"
        before: "23:59:58"
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - if:
      - condition: trigger
        id:
          - Beweging_Gedetecteerd
      - condition: state
        entity_id: light.beneden
        state: "off"
    then:
      - parallel:
          - service: scene.turn_on
            data:
              transition: 20
            target:
              entity_id: scene.beneden_dagelijks
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
  - if:
      - condition: trigger
        id:
          - 7 minuten na motion
    then:
      - parallel:
          - service: light.turn_off
            data:
              transition: 30
            target:
              entity_id: light.beneden
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
mode: single

Thanks to the details analysis of @pnbruckner I understand why my script was not doing what I wanted it to do.
But in Homeassistant 2024.1.5 you now need to add an additional target element, to define which script to be stopped:

alias: LedFlurLight
trigger:
  - platform: state
    entity_id:
      - input_number.led_state
condition: []
action:
  - service: script.turn_off
    target:
      entity_id: script.ledflurlight
    data: {}
  - service: script.turn_on
    target:
      entity_id: script.ledflurlight
    data: {}