How to turn off lights when all PIR sensors are not occupied OR unavailable, for 5 minutes

I need an automation that turns off the bathroom lights when there is no one there. Im using 3 PIR sensors on it.

The triggers are when they have no movement, BUT, I want the automation to run when they ALL are not occupied for 5 minutes. I also need to account for the case where one or more becomes unavailable, so I cannot just put the “and if” as “not occupied”.

I tried modeling them as “AND(NOT (not occupied for 5 minutes), …)” but the 5 minutes is not working because the negation should be NOT(not occupied) for 6 minutes, which is a little bit different.

Is there a way to solve it? Here’s how it is right now:

alias: sem movimento banheiro
description: ""
triggers:
  - type: not_occupied
    device_id: 8a8d0f2c37e0eb0a4acfcc783431cb98
    entity_id: dc2f2e958c71ab094e69f6e06cd36ee2
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - type: not_occupied
    device_id: 7128ea10089bd2becf45f3d0e3dc4e42
    entity_id: ddb61c02e913c99e3364c7496d37738e
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - type: not_occupied
    device_id: 79688030f254a6b0e2be1bea48b88c9b
    entity_id: cdc2270997413bcde57b93412cdb540b
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - trigger: time_pattern
    minutes: /15
conditions:
  - condition: and
    conditions:
      - condition: not
        conditions:
          - type: is_occupied
            condition: device
            device_id: 7128ea10089bd2becf45f3d0e3dc4e42
            entity_id: ddb61c02e913c99e3364c7496d37738e
            domain: binary_sensor
            for:
              hours: 0
              minutes: 8
              seconds: 0
      - condition: not
        conditions:
          - type: is_occupied
            condition: device
            device_id: 79688030f254a6b0e2be1bea48b88c9b
            entity_id: cdc2270997413bcde57b93412cdb540b
            domain: binary_sensor
            for:
              hours: 0
              minutes: 8
              seconds: 0
      - condition: not
        conditions:
          - type: is_occupied
            condition: device
            device_id: 8a8d0f2c37e0eb0a4acfcc783431cb98
            entity_id: dc2f2e958c71ab094e69f6e06cd36ee2
            domain: binary_sensor
            for:
              hours: 0
              minutes: 8
              seconds: 0
actions:
  - type: turn_off
    device_id: 805e72203db25cea03161d3e11584689
    entity_id: 728c4861be456ab63844ad5a812e644b
    domain: switch
  - type: turn_off
    device_id: 805e72203db25cea03161d3e11584689
    entity_id: 1d41f9b210e2c070fe31ae179ea16e99
    domain: switch
mode: single

This sort of thing is much easier if you use entity IDs rather than device IDs. There’s an example here which you may be able to build on:

I don’t think this answers the same question as mine.

I know how to turn off when both the PIRs are (not occupied for x mins), but this is different than both PIRS (not (occupied)) for x mins.

I read that sentence several times, but still don’t get it.

Create a group with your PIRS (binary_sensors)

if your group is off for x min, turn_off your switches

My point was that you’re making life very difficult by using device IDs. :grin: