If motion is detected X times in X Minutes - extend the time the lights are on. How is that possible?

Dear community,

i am facing the problem, that my lights turn off too fast in some occasion.
So I thought about extending the time the light is on, based on how often the motion sensor had detected motion in a certain time frame.

Is that possible that way?

Just extending the time in general is not a real solution for me, because I don’t want to have lights on for such a long time.

Would be great to get some input.

Thanks in advance.

Yes it is. If you aren’t extending the time but rather restarting the timer.
I have all my lights set to around 15 minutes, and every time motion is detected it simply restarts the timer. That way the lights will always switch off 15 minutes after the last motion. There is nothing to stop you reducing that to 5 minutes or less though, as long as you don’t make it less than the cooldown period of the motion detector.

Here is an example of the motion lighting in the hall:

alias: 'Light: Hall'
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 16942ffea955cab30431d0ef8be0e53a
    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
          - 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
    default: []
mode: restart

This code cancels and restarts the timer when motion is detected. Catches the timer finished event to turn the light off, and starts a timer if the light was turned on manually - so it will still switch off when the timer expires.

Thanks for that. I might adapt to that.

It’s not exactly what I was looking for though.
Right now my lights turn off, if there is no motion for 20s.
For most occasions that is enough, but at times it turns off when I was standing still for too long.

So I thought I could expand the 20s to like 1min, when there is motion detected more than x times.
Does that make sense?

Potentially if you used something like this:

You could detect how many times the motion sensor turned on in the last X minutes. And then use choose when the motion sensor is triggered to decide if the lights should turn off after X seconds or X minutes based on how many times the history_stats sensor says the detector was on in the last X minutes.

eg: turn off after 20 seconds if the count is <2 and after 1 minute if the count is > 1

1 Like

Perfect. Seems like what I have been looking for!
Thanks :slight_smile:

What about keeping the light on and switch it off until there is no motion anymore?

Correction: no more motion during a certain time-frame.

as Nick stated turns on the lights and keeps them on for 150 seconds (60 for the shelly and 90 for the automation) after motion has stopped.

- id: '1630948539368'
  alias: Bar Lights - Turn on and off based on dual motion sensors.
  description: Turn on and off all the lights in the bar area
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    - conditions:
        - "{{ trigger.id == 'bar_off1' }}"
        - "{{ states('binary_sensor.bar_main_motion_sensor_motion') == 'off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: single

That’s what I am doing right now. The time span is too short though and I don’t want to extend it, as i don’t want the lights on for too long.

Wanna keep it efficient.

I am using a ratio history stats sensor to solve for this. Just turn the lights off when the ratio falls to 0 and make the delta long enough that someone typically triggers it before it hits 0 (this takes some analysis to find the sweet spot).

- platform: history_stats
  name: Guest Bathroom Motion Ratio Last 10m
  entity_id: binary_sensor.guest_bathroom_motion_occupancy
  state: "on"
  type: ratio
  start: "{{ (now() + timedelta(minutes=-10)) }}"
  end: "{{ now() }}"
3 Likes

I liked the idea. Will play a bit with this.
Thanks for sharing!

Using a plain and simple automation here for avoiding the lighting for our patio is getting switched off if we are still sitting outside there after the scheduled switch-off time.

The lighting should switch to off daily at 22:00 but only if there has been no movement/occupancy detected for 10 minutes. If we are still sitting outside after above scheduled time the lights wont switch to off for as long as the occupancy sensor detects movement. Only after there has been no movement detected at the patio for at least 10 minutes the lights will be switched to off:

  trigger:
  - platform: time
    at: '22:00:00'
  - platform: state
    entity_id: binary_sensor.xiaomi_aqara_motion_sensor_inner_patio_occupancy
    to: 'off'
    for:
      minutes: 10
   condition:
   - condition: state
     entity_id: binary_sensor.xiaomi_aqara_motion_sensor_inner_patio_occupancy
     state: 'off'
     for:
       minutes: 10
   - condition: time
     after: '21:59:59'
     before: '05:00:00'
  action:
    - service: switch.turn_off
      target:
        entity_id: switch.lichtschalter_innenterasse_4_fach
  mode: single

The above works pretty well since quite some time now never left us sitting in the dark all of a sudden nor didn’t it switch the lights to off after it finally got quite at the patio. It’s basically a set-and-forget automation.

1 Like

I’m using an approach with 2 automations:
The first one switches the lights on upon motion (trigger: motion detected).
The second one switches the lights off, but with an delay of x minutes (trigger: no motion detected, delay x minutes).
This way the lights stay on as long as there is motion and for x minutes after the last motion was detected.