Using PIR + door sensor for room occupancy detection

Hey all, I’ve been fiddling with this for a good while and still haven’t come up with a robust solution. In my room, I have two Xiaomi PIR motion sensors from different angles. I also have a Xiaomi door switch sensor.

I feel like it should be possible to get fairly good detection with this, but it’s either turning off while occupied or staying on when empty (probs 90% accurate). I think a contributing factor to this might be that the Xiaomi sensors once they see motion they remain set as motion for 2 minutes, which I think is complicating matters. Is there anything glaringly obvious I’m doing poorly in the below? Thanks. (the light bit is to make a sign that it’s going to flag room so I can wave or something to stop it). This seems especially problematic when I leave and reenter a room quickly (I guess before the leave script has finished running?)

Essentially what I have is in pseudocode:

open door -> room presence set to on
door close -> Stop detection script. Start detection script.

sequence:
  - service: input_boolean.turn_on
    entity_id: input_boolean.bedroompresencescan
  - delay: 00:02:00
  - condition: state
    entity_id: binary_sensor.motion_sensor_1
    state: 'off'
  - condition: state
    entity_id: binary_sensor.motion_sensor_2
    state: 'off'
  - delay: 00:03:00
  - condition: state
    entity_id: binary_sensor.motion_sensor_1
    state: 'off'
  - condition: state
    entity_id: binary_sensor.motion_sensor_2
    state: 'off'
  - delay: 00:03:00
  - service: light.turn_on
    data_template:
      entity_id: light.ceiling
      rgb_color:  ['{{ (range(0, 255)|random|int) }}','{{ (range(0, 255)|random|int)}}','{{(range(0, 255)|random|int)}}']
  - condition: state
    entity_id: binary_sensor.motion_sensor_1
    state: 'off'
  - condition: state
    entity_id: binary_sensor.motion_sensor_2
    state: 'off'
  - delay: 00:02:00
  - service: input_boolean.turn_off
    entity_id: input_boolean.bedroompresence
  - service: input_boolean.turn_off
    entity_id: input_boolean.bedroompresencescan

And I also have this automation - where if motion is detected on any sensor while in scanning mode it sets presence to true and turns of the scripts.

 - alias: bedroomscan
   trigger:
     - platform: state
       entity_id: binary_sensor.motion_sensor_1
       to: 'on'
     - platform: state
       entity_id: binary_sensor.motion_sensor_2
       to: 'on'
     - platform: state
       entity_id: binary_sensor.motion_sensor_1
       to: 'on'
       for:
         minutes: 2
         seconds: 1
     - platform: state
       entity_id: binary_sensor.motion_sensor_2
       to: 'on'
       for:
         minutes: 2
         seconds: 1
   condition: 
     - condition: state
       entity_id: input_boolean.bedroompresencescan
       state: 'on'
   action:
     - service: input_boolean.turn_on
       entity_id: input_boolean.bedroompresence
     - service: input_boolean.turn_off
       entity_id: input_boolean.bedroompresencescan
     - service: homeassistant.turn_off
       entity_id: script.bedroompresencesrcipt

OK…so…

What I think you want is when the door is open then start detecting motion from the two sensors. If either is on then turn on some device. If both are off then turn off the device. If the door is closed then don’t detect motion from either device.

Is that about correct?

I’m confused about what you are trying to accomplish with your automation tho.

It looks like you want the motion detection to be turned off once the motion detectors first detect motion. But you have some triggers I don’t understand why they are there.