Simple motion sensor automation. Best approach?

I’m sure this is an easy one but would like to understand the best approach.

I have a motion sensor in our hall which turns lights off if there hasn’t been any movement for 10 minutes and it’s after 22:00.

But… if the last movement of the day is at 21:00 then ‘no movement for 10 minutes’ will trigger at 21:10 and the condition of ‘after 22:00’ has not been met. So the light will stay on all night.

Have I understood that correctly? If so, what’s the best fix? A separate automation that triggers at 10pm and checks to see that there hasn’t been any motion in the last 10 minutes?

I will give you my example. It will turn the lights off if left on after the time, but will not turn them on.
I utilize two separate automatons:

alias: Kid Bathroom Light Motion On
description: Turn on light with motion
trigger:
  - entity_id: binary_sensor.bathroom_kid_motion
    from: 'off'
    platform: state
    to: 'on'
condition:
  - condition: time
    after: '05:00:00'
    before: '21:30:00'
action:
  - type: turn_on
    device_id: xxxxxxxxxxxxxxxx
    entity_id: switch.shelly_shsw_pm_fxxxx
    domain: switch
mode: single


alias: Kid Bathroom Light Auto Off
description: Turn off light after no motion
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_kid_motion
    from: 'on'
    to: 'off'
    for: '00:10:00'
condition: []
action:
  - type: turn_off
    device_id: xxxxxxxxxxx
    entity_id: switch.shelly_shsw_pm_fxxxx
    domain: switch
  - type: turn_off
    device_id: xxxxxxxxxxxx
    entity_id: switch.shelly_shsw_pm_fxxxxx
    domain: switch
mode: single

Just add a second trigger to the same automation, and a second condition. This is quite a common pattern here for doing something when 2 things are true

trigger:
  - platform: state
    entity_id: MOTION SENSOR
    to: 'off' 
    for:
      minutes: 10
  - platform: time
    at: '22:00:00'
condition:
  - condition: state
    entity_id: MOTION SENSOR
    state: 'off' 
    for:
      minutes: 10
  - condition: time
    after: '21:59:59'
action:
  service: light.turn_off
  entity_id: LIGHT
1 Like

Oh! I didn’t know there could be more than one trigger for an automation. That makes perfect sense. Thank you.

1 Like

Sorry to resurrect this old thread but I have a similar question. I’m trying to turn a light off when there’s no movement in the hall AND no movement at the front door (from a security cam). I can trigger it okay, but I don’t know how I can establish that there hasn’t been any movement from the camera in the condition section. Do you know?

Here’s how I have it right no.

alias: Front Door. Motion stopped.
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.front_door_motion_detected
    to: 'off'
    for: '00:05:00'
  - type: no_motion
    platform: device
    device_id: 5f90816e6055cd94bd14ff88c33d53c6
    entity_id: binary_sensor.ewelink_ms01_e9ce6522_ias_zone
    domain: binary_sensor
    for:
      hours: 0
      minutes: 4
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.front_door_motion_detected
    state: 'off'
  - type: is_no_motion
    condition: device
    device_id: 5f90816e6055cd94bd14ff88c33d53c6
    entity_id: binary_sensor.ewelink_ms01_e9ce6522_ias_zone
    domain: binary_sensor
    for:
      hours: 0
      minutes: 4
      seconds: 0
action:
  - type: turn_off
    device_id: 76d7b61a3bb3d7317a5d9e82d875afa7
    entity_id: light.novastellabulb3
    domain: light
mode: single

alias: Front Door. Motion stopped.
trigger:
  - platform: state
    entity_id: binary_sensor.front_door_motion_detected
    to: 'off'
    for:
      minutes: 5
  - platform: state
    entity_id: binary_sensor.ewelink_ms01_e9ce6522_ias_zone
    for:
      minutes: 4
condition:
  - condition: state
    entity_id: binary_sensor.front_door_motion_detected
    state: 'off'
    for:
      minutes: 5
  - condition: state
    entity_id: binary_sensor.ewelink_ms01_e9ce6522_ias_zone
    state: 'off' 
    for:
      minutes: 4
action:
  service: light.turn_off
  entity_id: light.novastellabulb3
1 Like

Thanks for the quick reply. So

    state: 'off'
    for:
      minutes: 5

Tests to see if it’s been in the off state for five minutes?

Exactly that.

1 Like

Perfect. Thanks. I think I might abandon the Automations UI and start writing in YAML, which seems easier for me to follow.

1 Like