Need help getting door sensors to work as motion sensors

I’m trying to get an insight into my overall home activity. What I count as activity is the opening and closing of doors. So the title says it all, I want the doors to act as motion sensor. When a state change is detected, a sensors will turn on for about 1,5 minutes and then turn off again. Sounds pretty simple but I’m running into a problem with some input_booleans getting stuck in the on state forever (until I turn them off manually of course).

Automation:

- alias: Doors Changed
  trigger:
  - platform: state
    entity_id: 
    - binary_sensor.front_door 
    - binary_sensor.back_door
    - binary_sensor.garage_door
    - binary_sensor.living_room_stairway_door
  condition:
  - condition: template
    value_template: '{{ is_state("input_boolean." + trigger.entity_id[14:] + "_changed", "off") }}'
  action:
    - service: input_boolean.turn_on
      data_template:
        entity_id: input_boolean.{{ trigger.entity_id[14:] }}_changed
    - delay:
        seconds: 90
    - service: input_boolean.turn_off
      data_template:
        entity_id: input_boolean.{{ trigger.entity_id[14:] }}_changed

Inputs:

front_door_changed:
  name: Front Door Changed
  icon: mdi:door
  initial: false
back_door_changed:
  name: Back Door Changed
  icon: mdi:door
  initial: false
garage_door_changed:
  name: Garage Door Changed
  icon: mdi:garage
  initial: false
living_room_stairway_door_changed:
  name: Living Room Stairway Door Changed
  icon: mdi:door
  initial: false

I don’t understand why an input boolean would stay stuck in the on state. I tried splitting the automation for each entity but that did not help as one of the booleans still would get stuck.

Classic problem. If an automation is triggered while it’s still in a delay or wait_template from a previous trigger, it will abort the delay/wait_template and continue with the next step. So what’s probably happening is one of the sensors is changing less than 90 seconds after another sensor changed. So one of the sensors changes and turns on its associated input_boolean, then before the 90 seconds is up, another sensor changes, which causes the delay to abort and then its associated input_boolean is turned off, causing the first input_boolean never to get turned off (until you turn it off manually, or its associated sensor changes again without another sensor changing for more than 90 seconds.)

What I would recommend is to use timers instead of input_booleans. When a sensor changes have the automation start its associated timer. Then instead of testing for an input_boolean being on, you can test for a timer being active.

And if you want to display the timer in Lovelace I suggest you do a timer.pause first.

  #==================================================
  #=== Start Motion Timers
  #===
  #=== The timers are paused before starting because
  #=== the time remaining is not exposed after a
  #=== start event if the timer is already running
  #=== but it is if it is paused first.
  #===
  #==================================================
  #=== Hall
  - alias: Access Monitoring - Start Hall Motion Timer
    trigger: 
      - platform: state
        entity_id: binary_sensor.pir_hall
        to: 'on'
  
    action:
      - service: timer.pause
        entity_id: timer.hall_motion

      - service: timer.start
        data_template:
          entity_id: timer.hall_motion
          duration: 90

Alright, thanks guys! Gonna implement it right away and see for the coming days how it’s working.