Motion activated light wait time automation

How can I add a wait time to turn the light off to this automation?

- id: '1671648472715'
  alias: 'Lights: Bathroom Automation'
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.p1_motion_ias_zone
  condition:
  - condition: time
    before: 07:00:00
    after: '17:00:00'
    weekday:
    - sat
    - fri
    - thu
    - wed
    - tue
    - mon
    - sun
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.bathroom
  mode: single

When do you want to turn off the light?

Are you trying to create a motion activated light that automatically turns off when there’s no motion anymore?

Correct. I mean I would prefer an expiration time, like hey after 90 seconds no motion turn off.

Try this:

alias: "Lights: Bathroom Automation"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.p1_motion_ias_zone
    to: "on"
condition:
  - condition: time
    before: "07:00:00"
    after: "17:00:00"
    weekday:
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
      - sun
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.bathroom
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.p1_motion_ias_zone
        to: "off"
  - delay:
      hours: 0
      minutes: 1
      seconds: 30
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.bathroom
mode: restart

By the way, you will find lots of blueprints already tested to do exactly what you want… :wink:
This one from @Blacky is probably a good option for you: đź’ˇ Sensor Light - Motion Sensor - Door Sensor - Sun Elevation - LUX Value - Scenes - Time

The sequence of actions will be:

  1. Turn on the lights:
  1. Wait until motion sensor goes off:
  1. Wait 90sec:
  1. Turn off the lights:

=> Please note that I’ve change the mode to “restart”, so if a new motion happens again during those 90s the automation will restart and wait again for the motion to stop and then count the time again.

By the way, on your trigger the automation will run at any change on the motion sensor (even a sensor becoming unavailable), which your probably don’t want.

Maybe you should have something like this instead:

  - platform: state
    entity_id:
    - binary_sensor.p1_motion_ias_zone
    to: "on"
1 Like

Thank you for taking the time to explain this in detail to me. I very much appreciate it. Will try it soon.

1 Like

It’s as simple as this:

- id: '1671648472715'
  alias: 'Lights: Bathroom Automation'
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.p1_motion_ias_zone
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.p1_motion_ias_zone
    from: 'on'
    to: 'off'
    for:
      seconds: 90
  condition:
  - condition: time
    before: '07:00:00'
    after: '17:00:00'
  action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.bathroom
  mode: single
1 Like

@123 you are a master at light automation. I am learning a lot. Thanks.

One last challenge, I’ll ask here since it still about motion sensor and lights.

I have a contact sensor on the front door, and planning to install a person presence sensor (Aqara FP1), to turn on the light (hue lights) of the entry room when the door is open (contact sensor), no one is detected in the living room (aqara fp1), and the persons status is away (I have just 2 persons tracked).

I have gone here so far:

alias: 'Lights turn on when someone is home.'
trigger: 
  platform: zone
  entity_id: device_tracker.iphone
#entity_id: person.hatarez
  zone: zone.home
  event: enter
action:
  service: light.turn_on
  entity_id: light.livingroom
condition:
  condition: state
  entity_id: sun.sun
  state: 'below_horizon'

Let me understand it better…

You have the following:

  1. A door sensor
  2. A presence sensor
  3. Device trackers for 2 people
  4. A light in the living room

My questions are:
a. You want the light (4) to turn on only when the door is opened AND no one is at home, is that correct? If someone is at home then the light is NOT turning on, right?
b. What if no one is at home, the door still closed and then the presence sensor start detecting someone? Do nothing?
c. What if no one is at home, the door still closed and the presence sensor in NOT detecting anyone, but then the device tracker detects someone arriving at home? What if the door then opens? It could happen that your device tracker detects you are at home even before you open the door.
d. Will this light ever turn off automatically? After some time? After the last person leaves home?

Try to think in other combinations and share your expected results.

Also,

The light must go on only in case no one is home and the door gets opened when is dark outside. Now we can use the tracker or maybe not (what if I have the maid or someone dropping off something , do I let them navigate in the dark?).