Turn on light on motion, but after delay, then turn off

Hey guys, I’ve been struggling with an automation and have not been able to figure this out or find a good example. What I want is to: On motion, wait 2 minutes, then turn on a light, keep the light on while there is motion and another minute after the last motion, then turn it off.

As you can see in my code below, I made it work for the part where the light comes on right away and stays on 1 minute after the last motion. But I’m not sure how to make it wait the initial 2 minutes. I’ve considered inserting a second timer for the delay which comes on with motion as well, but I would need some boolean variable to only make it run the first time it sense motion, or maybe a counter, either inserted as part of an if/then block…but I’m not sure how to make that work. Any ideas? Thank you so much!

alias: Test Light
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_timer
    id: Timer Finished
  - type: no_motion
    platform: device
    device_id: dcad774a57d8eb164e21c4c727a9b111
    entity_id: binary_sensor.q_sensor_motion_detection
    domain: binary_sensor
    id: Motion Stopped
  - type: motion
    platform: device
    device_id: dcad774a57d8eb164e21c4c727a9b111
    entity_id: binary_sensor.q_sensor_motion_detection
    domain: binary_sensor
    id: Motion Detected
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Motion Stopped
        sequence:
          - service: timer.start
            data:
              duration: "00:01:00"
            target:
              entity_id: timer.light_timer
      - conditions:
          - condition: trigger
            id: Motion Detected
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.light_timer
          - type: turn_on
            device_id: 7656cc037c76b1a2615fdd70b0d15f22
            entity_id: light.z_wave_plus_700_series_s2_dimmer_switch_10
            domain: light
      - conditions:
          - condition: trigger
            id: Timer Finished
        sequence:
          - type: turn_off
            device_id: 7656cc037c76b1a2615fdd70b0d15f22
            entity_id: light.z_wave_plus_700_series_s2_dimmer_switch_10
            domain: light
mode: restart

Do you want it to trigger after detecting at least 2 continuous minutes of motion?

Or 2 minutes after any motion is detected, regardless of how brief it was?

The former…if there’s no more movement during those 2 minutes, there’s no reason to turn it on.

alias: example 
trigger:
  - platform: state
    entity_id: binary_sensor.q_sensor_motion_detection
    to: 'on'
    from: 'off'
    for:
      minutes: 2
  - platform: state
    entity_id: binary_sensor.q_sensor_motion_detection
    to: 'off'
    from: 'on'
    for:
      minutes: 1
condition: []
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.z_wave_plus_700_series_s2_dimmer_switch_10
mode: single

NOTE

The usual way this is done is that the first State Trigger (detect motion) doesn’t employ the for clause and simply triggers for any motion no matter how brief.

In the posted example, the light won’t be turned on until the automation detects 2 solid minutes of motion.

So you would need continuous motion for this? What if there is a break in motion, there’s motion at t1=0s and then more motion at t2=60 or 90s or 115s but nothing in between?

Yes. The binary_sensor representing your physical motion detector would need to report on for a continuous 2 minutes in order to trigger that State Trigger.

Because that’s what you agreed to when you replied “the former” to my previous question.

If that not what you actually want, then you’ll need to clarify your requirements.

Thank you for walking through this with me, @123. Here’s the exact use case, I was trying to avoid the details :slight_smile:

This is for a small toilet room part of a larger bathroom. If a person walks in there for #1 and they’re in and out in 2 minutes, don’t turn on the fan, no need. But if they go in and start playing Candy Crush in anticipation of #2 (so they’re still in the bathroom generating some motion), turn on the fan after 2 minutes and leave it run for 5 minutes after the last time motion is triggered. I have set the sensor timeout to 30 seconds and sensitivity high so if you’re in there on your phone or reading a book, it will pick you up.

This is really what I’m working through. The part that I’m struggling with is that initial delay. Does this make more sense now? I know that it’d be easier to just turn on the fan right away on motion, it’s how it works now…but let’s just say that I received some push back.

The way is:

alias: example 
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_motion
    to: 'on'
    from: 'off'
    for:
      minutes: 2
condition: []
action:
  - service: 'switch.turn_on'
    target:
      entity_id:  switch.bathroom_fan
  - wait_for_trigger:
      - platform: state
        entity_id:  binary_sensor.bathroom_motion
        to: "off"
        for:
          minutes: 2
  - service: 'switch.turn_off'
    target:
      entity_id:  switch.bathroom_fan

mode: single

Wait 2 minutes active movement, turn on, wait 2 minutes inactive, turn off.

It all depends on your motion detector’s ability to detect their presence, even when they’re virtually motionless.

For example, if their activity causes your motion detector to report motion for several seconds, followed by no-motion, followed by motion, etc, all within that 2 minute window, it voids the use of a State Trigger with a for option. Every time the motion detector reports off it serves to reset the for option’s countdown.

1 Like

You would have to be using mmWave for this to work, and it seems unlikely you have a powerpoint in a useful spot for it.

As Taras says, you come into the room and it triggers motion. Then you either leave 30 seconds later triggering no-motion after the cooldown period; or you sit down which also triggers no-motion after the cooldown period. Either way, after 2 minutes there’s never going to be motion (except the one going down the toilet :rofl:).

And while the cooldown for my Hue motion sensor is 10 seconds, for my Aqara sensors it is 2 minutes (although I think this can be adjusted at the expense of battery life).

You may be able to achieve what you want by combining with a door sensor. Door open then motion then door closed is somebody coming in; whereas motion then door open is somebody leaving. If somebody came in 2 minutes ago and haven’t left, then start the fan. Unless you like to do #2 with the door open :grimacing:

I wonder, if it is the need for turning on the fan that we are talking about: would this smell #2 time? :rofl:

I actually started looking into some TVOC sensors for this purpose but they don’t seem to be sensitive enough. It would have made things so much simpler. Maybe a seat pressure sensor if I can find a compact one.