Turn light for x minutes by motion trigger and extend time before shutting off again

Hi guys,

I’m still a newbie with HA and probably my question is a basic one, but bear with me, please.

I want to turn on a light by when motion (or any other event, for all that matters) is triggered on a sensor and turn it off after x minutes (let’s say 5), but only if the light was originally off or extend the 5 minutes before turning it off if the light was turned on by the motion sensor only.
I’m want to avoid turning the light off after 5 minutes, when there’s motion, if the light was already on for another reason (another switch, scheduled, or whatever).

It would be something like this:

  • Event is triggered (motion)
    – Light is on (for another reason): do nothing
    – Light is off: turn it on and schedule turning it off after 5 minutes
    – Light is on (was turned on by the event): keep it on and reset the off timer and shut it down after 5 minutes from now

I could probably create an input boolean to keep the “turned by event” status on the light, but that makes me create another entity which I think is overkill for this (or isn’t it).
It would be cool if we could just create attributes on entities and set values to it directly, on the fly, like “turned by motion: true or false”.

1 Like
## Lights office ON / OFF

- alias: sensor.office_pir turn_on desktop_light if Night
  trigger:
    platform: state
    entity_id: sensor.office_pir
    to: 'ON'
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service: homeassistant.turn_on
    entity_id: switch.desktop_pc_light

- alias: sensor.office_pir turn_off desktop_light no_motion_detected 5min
  trigger:
    platform: state
    entity_id: sensor.office_pir
    to: 'OFF'
    for:
      minutes: 5
  action:
    service: homeassistant.turn_off
    entity_id: switch.desktop_pc_light

it just turns off , if no movement for the last 5 min

What if you turned the light manually (because you want it on) and then you trigger the motion sensor by passing in front of it. Will the light turn off after 5 minutes or will it stay on (as I mentioned it’s a requisite on my post)?

it turns off after 5 min.
understand your requirement, and is ok , but I prefer to think in the energy efficiency and if no movement for 5 min, it turns off .
to keep things as you want, it would imply to have a condition on the turn off automation, if it was turn_on by automation.sensor.office_pir turn_on desktop_light_if_Night

nevertheless, you could have another autmotation like mine, but having a 30 min - then off . it would turn off 30 min after no movement .

if it was turn_on by automation.sensor.office_pir

And that’s exactly my question. How do I do this check?

understand your requirement, and is ok, but I prefer to think in the energy efficiency and if no movement for 5 min, it turns off

Thanks, but not what I asked on the original post

I think I did it, as I mentioned in the original post, by creating an input boolean (called outdoor_light_turned_by_motion) to store if the light was turned by the motion or not.

Anyone sees any flaw here?

- id: '1622154421481'
  alias: TEST Motion Light
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.outdoor_motion
    to: 'on'
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: switch.sonoff_s26_outdoor_light
      state: 'off'
    - condition: state
      entity_id: input_boolean.outdoor_light_turned_by_motion
      state: 'on'
  action:
  - service: homeassistant.turn_on
    target:
      entity_id: switch.sonoff_s26_outdoor_light
  - service: homeassistant.turn_on
    target:
      entity_id: input_boolean.outdoor_light_turned_by_motion
  - wait_template: ''
    timeout: 00:05:00
    continue_on_timeout: true
  - service: homeassistant.turn_off
    target:
      entity_id: switch.sonoff_s26_outdoor_light
  - service: homeassistant.turn_off
    target:
      entity_id: input_boolean.outdoor_light_turned_by_motion
  mode: restart

How has this been working for you? I’m looking to do something very similar.

My setup is like this

alias: 'Sensor: Motion, light, Hall; <2.5 lux@Trapp'
description: Turns on Garderob when motion in Hall or Trapp, and below 2.5 lx at Trapp
trigger:
  - type: motion
    platform: device
    device_id: 518774bb5a0e532a83d4eba30cb2f43c
    entity_id: binary_sensor.hall_motion
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 1104adb2ceeb7216346a4845f30200d1
    entity_id: binary_sensor.trapp_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.trapp_motion_light_level
        below: '2.5'
      - condition: device
        type: is_off
        device_id: 698f00a14d15ddcf7a7449d3e66f7ad9
        entity_id: light.garderob
        domain: light
action:
  - service: light.turn_on
    target:
      entity_id: light.garderob
    data:
      brightness_pct: 10
      kelvin: 2000
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: 518774bb5a0e532a83d4eba30cb2f43c
        entity_id: binary_sensor.hall_motion
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.garderob
    data:
      transition: 5
mode: restart

Light will restart if there is motion detected during the time the light is on.

OMG another thing that is a nightmare in Homeassistant and a bliss in Hubitat… anyone got this working?

1 Like

Yes, it’s really easy.
All you need to do is have a delay in the automation to turn the light off.
Set the automation to restart, instead of single mode.
Then every time the automation is triggered, it restarts which means the delay is restarted.

I use a timer instead of a delay in the script, just to decouple from the automation - but it’s the same basic idea.

alias: 'Light: Hall'
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 45a10883d530fe0171a2af8f2f25fe5a
    entity_id: binary_sensor.hall_motion_home_security_motion_detection
    domain: binary_sensor
    id: motion
  - platform: device
    type: turned_on
    device_id: 32f11a148ef066b9bc8c729e8996ad5e
    entity_id: light.hall_light
    domain: light
    id: light_on_timer_check
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: state
    entity_id: input_number.hall_brightness
    id: set_level
  - platform: device
    type: turned_on
    device_id: 32f11a148ef066b9bc8c729e8996ad5e
    entity_id: light.hall_light
    domain: light
    id: set_level_on
    for:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finished
    event_data:
      entity_id: timer.hall
  - platform: device
    type: turned_off
    device_id: 32f11a148ef066b9bc8c729e8996ad5e
    entity_id: light.hall_light
    domain: light
    id: light_off
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: motion
              - condition: or
                conditions:
                  - condition: state
                    entity_id: input_boolean.lighting_night_mode
                    state: 'on'
                  - condition: numeric_state
                    entity_id: sensor.sma_lux
                    below: '1500'
        sequence:
          - type: turn_on
            device_id: 32f11a148ef066b9bc8c729e8996ad5e
            entity_id: light.hall_light
            domain: light
          - service: timer.cancel
            target:
              entity_id: timer.hall
            data: {}
          - service: timer.start
            data:
              duration: '00:15:00'
            target:
              entity_id: timer.hall
      - conditions:
          - condition: trigger
            id: light_on_timer_check
        sequence:
          - choose:
              - conditions:
                  - condition: not
                    conditions:
                      - condition: state
                        entity_id: timer.hall
                        state: active
                sequence:
                  - service: timer.start
                    data:
                      duration: '00:15:00'
                    target:
                      entity_id: timer.hall
            default: []
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: timer_finished
              - condition: device
                type: is_on
                device_id: 32f11a148ef066b9bc8c729e8996ad5e
                entity_id: light.hall_light
                domain: light
        sequence:
          - type: turn_off
            device_id: 32f11a148ef066b9bc8c729e8996ad5e
            entity_id: light.hall_light
            domain: light
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: set_level
              - condition: device
                type: is_on
                device_id: 32f11a148ef066b9bc8c729e8996ad5e
                entity_id: light.hall_light
                domain: light
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.hall_light
            data_template:
              brightness_pct: '{{ trigger.to_state.state | int }}'
              transition: 8
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: set_level_on
              - condition: device
                type: is_on
                device_id: 32f11a148ef066b9bc8c729e8996ad5e
                entity_id: light.hall_light
                domain: light
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.hall_light
            data_template:
              transition: 8
              entity_id: light.hall_light
              brightness_pct: '{{ states(''input_number.hall_brightness'')|int }}'
      - conditions:
          - condition: trigger
            id: light_off
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.hall
            data: {}
    default: []
mode: restart

There is extra stuff in there, coping with setting the light to a certain level when it is turned on.

1 Like

Greetings… great thread with loads of helpful information. I tried to create my own but alas it doesn’t trigger the switch? Here’s my code…

alias: Studio Motion Sensor Light
description: Turns on Studio Light when motion is detected in Studio
trigger:
  - type: motion
    platform: device
    device_id: 7d367d16898139d72601e53990f841f8
    entity_id: binary_sensor.studio_monitor_motion_3
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: device
    type: is_off
    device_id: a1f0699259ad6513b255853c1a6835fe
    entity_id: switch.studio_light_socket_1
    domain: switch
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.studio_light_socket_1
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: 7d367d16898139d72601e53990f841f8
        entity_id: binary_sensor.studio_monitor_motion_3
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.studio_light_socket_1
mode: restart

Not sure what’s happening to cause the switch to not trigger? The screen shot below shows the motion detector is triggering. And when I click on Run Actions the switch activates. So not sure why the switch is not triggering via the automation?

Any ideas/help appreciated.

UPDATE:

I tweaked things a bit and it now triggers the switch… only, it turns off while I am sitting in the room. So the PIR is not seeing me moving, so turns off the switch, which turns off the light.

alias: Studio Motion Sensor Light
description: Turns on Studio Light when motion is detected in Studio
trigger:
  - type: motion
    platform: device
    device_id: 7d367d16898139d72601e53990f841f8
    entity_id: binary_sensor.studio_monitor_motion_3
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition:
  - condition: device
    type: is_off
    device_id: a1f0699259ad6513b255853c1a6835fe
    entity_id: switch.studio_light_socket_1
    domain: switch
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.studio_light_socket_1
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: 7d367d16898139d72601e53990f841f8
        entity_id: binary_sensor.studio_monitor_motion_3
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 30
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.studio_light_socket_1
mode: restart

Looks like I need to find a better movement detector, that isn’t a PIR I guess. I’ve heard there are some new movement sensors around, so will look into it.

What I would do is put a wait time since last time motion was detected. Not quite as compact as your code, but this works flawlessly in my house. I have a seperate automation to turn on the lights based on the motion group going off, so this runs after motion is detected… starts at 6pm my time until sunrise –
Maybe a little bit of this code could be added to yours around the wait for trigger.

- id: "1631764775708"
  alias: Kitchen Light OFF 7 mins after motion 
  description: Kitchen Light OFF 7 mins after motion 
  trigger:
    - platform: state
      entity_id: group.kitchenmotion
      to: "off"
      from: "on"
      for:
        hours: 0
        minutes: 7
        seconds: 0
        milliseconds: 0
  condition:
    - condition: or
      conditions:
        - condition: time
          after: "18:00:00"
        - condition: sun
          before: sunrise
  action:
    - type: turn_off
      device_id: 4856f51b9d4f30xxxxxxxxxxxxx
      entity_id: switch.kitchen_light
      domain: switch
  mode: single

For anyone looking to do this in 2024 via the GUI, it’s really easy now with the Scripts section under Automations. For my use, I wanted a virtual button I could press to turn the garage lights on for ten minutes then turn them back off again. Start a new script then just add things in the order you want them to happen. Delay is inserted by clicking “Add Building Block”. Mine looks like this: