No Motion Light Switch Off

Hi all

I have a light in the hallway that has a few triggers. It can be triggered by opening the front door, the porch door, or by a motion sensor in the hall.

Using a combination of toggles I have got this system to work for me 99% of the time. There is an edge case that I can’t quite figure out however.

Usually, when you open the front door and/or porch door you will then enter the hallway, thus triggering the motion sensor. I then have a trigger configured to switch off the light if the motion sensor is “Clear” for 2minutes. I use “triggered-by” for this and it works great.

However, if you open the door, thus turning on the light, but do not enter the hallway, the light will remain on indefinitely. It never receives the “triggered-by” as the sensor has not toggled from Clear to Detect and back to Clear.

So my question is, is there a way to sample a sensor and determine how long it has been in a state? As it stands, I need the Detect → Clear state change to trigger the automation. I would like a “if the sensor has been clear for at least 2 minutes then trigger”.

Does that make sense? Probably not… Sorry!

alias: Hall Motion Toggle
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 4a2100b162f41bae919ef1d45dd8e666
    entity_id: binary_sensor.porch_door
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 80c14596b45440d960f19f2048d0510d
    entity_id: binary_sensor.front_door
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: 117718ad42487823f9e6366289700cac
    entity_id: binary_sensor.hall_motion
    domain: binary_sensor
  - type: no_motion
    platform: device
    device_id: 117718ad42487823f9e6366289700cac
    entity_id: binary_sensor.hall_motion
    domain: binary_sensor
    id: Hall Motion Stopped Detecting
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: or
    conditions:
      - type: is_illuminance
        condition: device
        device_id: 117718ad42487823f9e6366289700cac
        entity_id: sensor.hall_luminance
        domain: sensor
        below: 10
      - condition: sun
        after: sunset
        after_offset: "-00:30:00"
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.hall_motion
  - if:
      - condition: trigger
        id: Hall Motion Stopped Detecting
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.hall_motion
    else:
      - stop: ""
mode: restart