Bathroom occupied boolean logic

I have a moment sensor (xiaomi) triggered maximum once per minute. The movement triggers the bathroom light to turn on. Once the sensor is off for 3 minutes the light goes off.

I would like to add an open/close door sensor to the bathroom door and combine the PIR and Open/Close states to decide if the bathroom is occupied.

As a general rule I think if the door is closed and there has been movement in the bathroom after the door was closed, the state should be ‘occupied’. If the door is open and the PIR sensor is ‘off’ I the bathroom state should be ‘free’

Does anyone else have a similar setup they can share their config for? My idea is to have a light that that goes green when the bathroom is free and red when occupied :slight_smile:

I don’t think that this will work.
An example, you open the batroom door, the motion sensor detects motion and goes to “on”, you close the door → bathroom door closed and motion = “on” → bathroom occupied.
Now you are finished with your business, the motion sensor detects motion and goes to “on” again, you open the bathroom door and close it. → bathroom door closed and motion =“on” → bathroom occupied, but actually the batroom is free.

I think this should be possible. You didn’t specify the entities, so I’ll write this example with binary_sensor.motion to represent the motion sensor ('on' = motion), and binary_sensor.door to represent the door ('on' = open.) input_boolean.occupied will indicate if the bathroom is occupied ('on' = occupied.)

- trigger:
    # Motion detected ...
    platform: state
    entity_id: binary_sensor.motion
    to: 'on'
  condition:
    # ... while door is closed ...
    condition: state
    entity_id: binary_sensor.door
    state: 'off'
  action:
    # ... bathroom is occupied
    service: input_boolean.turn_on
    entity_id: input_boolean.occupied
- trigger:
    # Door is open and no motion ...
    platform: template
    value_template: >
      {{ is_state('binary_sensor.door', 'on') and
         is_state('binary_sensor.motion', 'off') }}
  action:
    # ... bathroom is free
    service: input_boolean.turn_off
    entity_id: input_boolean.occupied

The first automation does what I think you asked for. I.e., if the door is closed when motion is detected, it will turn on the occupancy indicator. However, what you might really want is to replace the first automation with:

- trigger:
    # Door is closed and motion has been recently detected ...
    platform: template
    value_template: >
      {{ is_state('binary_sensor.door', 'off') and
         is_state('binary_sensor.motion', 'on') }}
  action:
    # ... bathroom is occupied
    service: input_boolean.turn_on
    entity_id: input_boolean.occupied

This will turn on the occupancy indicator once both the door is closed and the motion sensor is on. So, e.g., if the motion sensor trips as someone is entering the bathroom, but before the door is closed, the first automation would not turn the occupancy indicator on, whereas this last one would. (I assume the motion detector entity stays on for a while after motion is first detected. If not then you might need something else.)

I don’t think this will work, but it depends on where the motion sensor is positioned.

- trigger:
    # Motion detected ...
    platform: state
    entity_id: binary_sensor.motion
    to: 'on'
  condition:
    # ... while door is closed ...
    condition: state
    entity_id: binary_sensor.door
    state: 'off'
  action:
    # ... bathroom is occupied
    service: input_boolean.turn_on
    entity_id: input_boolean.occupied

You open the door and enter the room, then motion is detected while the door is still open, so this automation will not trigger.

- trigger:
    # Door is closed and motion has been recently detected ...
    platform: template
    value_template: >
      {{ is_state('binary_sensor.door', 'off') and
         is_state('binary_sensor.motion', 'on') }}
  action:
    # ... bathroom is occupied
    service: input_boolean.turn_on
    entity_id: input_boolean.occupied

This suggestion will also not work. You leave the bathroom and close the door but the motion sensor is still “on” (assuming the motion sensor stays on for some time), so this automation would trigger even though the batroom is not occupied.

Maybe I have an error in my logic, but I just can’t see how this can work.

Yes, I said that. Hence the second suggestion.

But that was not the requirement as stated in the OP:

If the door is open and the PIR sensor is ‘off’ I the bathroom state should be ‘free’

The assumption is when the bathroom is free the door will remain open.

Now, if the requirements change… :wink:

I overread this very important assumption. Man I feel stupid now :joy:

Hi Both,

Thanks a lot for your help with this. I understand its a little tricky and might require some trial and error.

The Xiaomi PIR sensors, only trigger max 1 time per minute. If a PIR sensor is triggered, it will clear (state change to off) after 60 seconds assuming there is no movement right at the 60 second threshold.

Our bathroom door is usually open when the bathroom is not in use. (assuming we don’t need to stop the oder permeating the rest of the house :slight_smile:

Maybe you could use something like this to turn the occupancy indicator off:

- trigger:
    # Door is open and no motion ...
    - platform: template
      value_template: >
        {{ is_state('binary_sensor.door', 'on') and
           is_state('binary_sensor.motion', 'off') }}
    # ... or door is closed and no motion for 3 minutes ...
    - platform: template
      value_template: >
        {{ is_state('binary_sensor.door', 'off') and
           is_state('binary_sensor.motion', 'off') }}
      for:
        minutes: 3
  action:
    # ... bathroom is free
    service: input_boolean.turn_off
    entity_id: input_boolean.occupied

Or, I suppose, you could just turn the occupancy indicator off when there hasn’t been motion for a while, regardless of the door. But then, is the door sensor actually needed?

This is an interesting discussion and something I have been working with for a while. So as another possibility to “turn the occupancy indicator off:” in the bathroom, what about if the sensor down the hall just detected movement and a few seconds later the sensor in the loungeroom detected movement?

With no other movement detected in the bathroom…logic would reasonably tell you the bathroom was vacant right?

Another possibility (at my house anyway) is movement was detected in one of two bedrooms adjacent the bathroom…With no other movement detected in the hallway or the bathroom…logic would reasonably tell you the bathroom was vacant right?

My point is that a reliance on just deciding the bathroom is free needs to be looked at more of a change of where occupation has moved to.

My problem here? How to implement this across the whole house when 3 or more people are doing different things within the house.

Occupation can get very complicated very fast.

Then there is movement inside to outside to handle…yikes, did anyone even open a door to go out? Like I said this can all get very complicated very quickly.

Any further thoughts from you guys on a whole house approach?

Hi again :slight_smile:

In my situation I am in a house with usually 3 occupants which as you have pointed out can get tricky with complicated solutions.

I think I have a simplistic solution that may work. We have a small door bolt that we use when ever the bathroom is occupied.

My plan is to put a small but powerful magnet on on the back of the bolt in such a way that when its open its next to one half of an open/close sensor.

I am waiting for some small magnets to arrive, to test my idea :slight_smile:

1 Like

Sounds like a reasonable solution…post again after you have put it in place if you can?

The only downside with your problem is that the door stays open when unoccupied. If it was closed, you could get away with only the door sensor.
You could go with 433tomqtt. That way you can get PIR sensors (and door sensors) from alarms etc that work all the time instead of eg. once per minute.
Downside of the 433tomqtt is battery consumption, but can’t say for sure how long they will last since I’m just getting started on it!