Occupancy Blueprint

Hey there,

I created a blueprint for the “wasp in the box” principle. This project is inspired by the App Daemon script wernerhp/ha.appdaemon.wasp.

Why?

I wanted to solve the problem: “The lights in my bathroom should stay on as long as I am in the room. No matter if I move or not.”

For that, the “wasp in the box” principle can be applied. It’s works as the following:

Imagine a wasp that is flying. As long as it flies, there is movement. Now put this wasp inside a box. As long as the box is closed, you know that the wasp cannot escape. It is only required to register movement of the wasp inside the box once. When you open the box, the wasp can escape.

This principle can be applied for e.g. a bathroom. As long as the door is closed and movement was detected once, the light can stay on. The only way for you to leave the room, is to open the door (or escape through the window, if you’re adventurous :upside_down_face:)

I use this blueprint as my daily driver in my bathroom. And it works pretty great. Also, special cases like directly closing the bathroom door behind you after leaving does not result in a false occupancy detection.

Requirements

The blueprint requires some helpers to work:

  1. Occupancy Helper (input_boolean) – This helper saves the occupancy state and can be used as a trigger of e.g. your light automation.
  2. Last Motion Helper (datetime, set with “Date and time”) – This helper saves when the last movement was detected by the motion sensor or motion sensor group. This is necessary to prevent a false occupancy when you directly close the door behind you.

How to set up

  1. If you have multiple motion sensors, please create a group for them. The same also applies if you have multiple door sensors.
  2. Create two helpers (input_boolean, datetime) in the helpers section
  3. Import the blueprint Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.
  4. Select the helpers and groups/sensors.
  5. Set a name
  6. Profit.

You can find the code here: https://community.home-assistant.io/t/occupancy-blueprint/477772 · GitHub

29 Likes

Hey Alex - talk about great timing… this is a problem I’m trying to solve right now but not a fan of having a custom component for it - so firstly thank you!

Before I implement it - I have a couple questions you might be able to help with:

  • The room in question is a small en suite - so this means that the motion sensor will detect movement as I’m exiting… will this be a problem?
  • I’m using an Aqara P1, with the occupancy timeout currently set to 30 secs - do you advise reducing this to 0 secs for your blueprint?

Thanks! - Jay.

1 Like

Hey Jay-uk,

movement during exiting should not be a problem. I specially wrote this to fix this issue. The mentioned AppDaemon script did not account for that and therefore the light stayed on in the room when I closed the door behind me. If you don’t close the door it also should work, but it will detect you in the other room and sets the occupancy helper to on. So the light would turn on (or whatever you want to automate).

The occupancy stays in the on state as long as the motion sensor is not in the clear state. That means if your timeout is set to 30 seconds, the helper will be set to off after the 30 seconds.

1 Like

Thanks Alex!
After struggling with bathroom and toilet lights turning off when occupied, I stumbled across this as I was about to install wasp. Seems to be working so far, and does exactly what I was trying to achieve with automations. This probably saved me months of frustration.
Thanks again. :smiley:

2 Likes

I have a similar thing set up and working for some time.
I use door sensor and a microwave “occupancy” detector (aqara fp1), which is actually just a more sensitive motion detector.

I have separated occupancy algorithm from lights, as those are quite complex on their own.

How I did it so far:
motion sensor is set for 10 second cooldown. So when it’s state changes to “no movement”, I check if the door was opened or closed during the last 12 seconds. If true, I set the room as not occupied.
However this is not perfect, as there are at least two corner cases:

  1. I go IN to the room, close door and immediately sit down to do my business. If I sit really still, even the microwave detector will stop registering movement, and this algorithm will think that someone has actually left the room when door was opened. I have tried to decrease the timeout, however the cooldown of motion sensor is not very precise and I can’t get this working well: If I decrease the timeout too much, it will not detect someone leaving.

  2. The case, where door is not being used/closed. Like a working room, living room etc. If I go in and sit down by the desk and read a book, it will lose the movement, and, since the door is open, there is no way so see if someone has left the room.

My idea to tackle this is to also check nearby rooms. For example, if the door is/was open, the motion has stopped, but there is no movement in adjacent room, we can assume no one has left and is just sitting still. Same for case #1 and #2.

There are still two issues remaining though:

  1. Reliable motion detection needs to be installed in every room.
  2. This is still not reliable with more than one person at home.

Any ideas?

For your 1st “corner case” I’m afraid its just the limitation of your hardware and there is nothing you are doing wrong. There is no way around it, you need a motion sensor with a cooldown of less than a few seconds. (you can use my solution for problem number two, but it feels a bit invasive for a toilet or bathroom) For this I just bought the aqara p1 sensor as it had an adjustable cooldown period of 1-300 seconds or something like that. I set it to 2 seconds, more than enough time for the sensor to catch me inside the room moving just after closing the door.

And for your second issue I use a PIR to turn on the lights and Frigate to turn off the lights, and while I’m at it everything else in the Livingroom.
A small $50-$100 rtsp camera, and a HA server powerful enough to run frigate for object detection.
Any recent desktop or laptop should be enough… not sure about a pie though.

Thank you very much @alexbabel !

Will try to run i through it’s paces tomorrow instead of the AppDaemon version

Looking at the blueprint it in the visual editor, I think it might help new users if for each helper field you provided the same short description of the type of helper needed as you have in your Requirements section here.

Thank you Alex, will try your blueprint later and update you .

Hi Alex,
thanks a lot for your blueprint. I am quite unexperienced, yet, when it comes to HA automations.
so far, I was able to get the script running and my boolean helper (“bathroom_wasp”) works… what now?
could you please give me some guidance: I want to set a light scene depending on bathroom occupancy, respectively switch off all bathroom lights when the room is empty… how can I achieve that?

Until now I used another blueprint (“toggle scene”) that relies on a motion sensor… however, I cannot simply put my occupancy-helper as trigger there :frowning:
thanks a lot!

Hey @lensherm,

thank you for your feedback. I just updated the description for both helpers.

Hey @docNeptun ,

you can use the wasp helper as a trigger for further automations. Just choose as a trigger type State and search for the helper (bathroom_wasp), then you can choose the state changes (offon for movement and onoff for no movement [as two separate triggers]).

There are also some blueprints that can help you to achieve the light dimming behaviour. Currently, I use a modified version of the default motion blueprint. I also plan to publish my version as soon as I have the time to polish it. Presently, it’s not in a state to be published.

Update: It was not the default motion blueprint. I used the following blueprint as a template: Turn on light, switch, scene, script or group based on motion, illuminance, sun (+ more conditions)

1 Like

Thank you. Apologies for the confusion, I should have been more clear. What I meant was giving the user the type of a helper they have to create - one of them needs to be of the type “toggle” and the other has to be the “date and time” helper.

Oh ok. I will add that too. Thanks again

I’m getting an error adding the blueprint:

mapping values are not allowed here in "<unicode string>", line 31, column 35: name: Occupancy Helper (type: input_boolean) ^

Hey @nitaybz,

thanks for the hint. It should work again now.

I was hoping this would solve my bathroom lighting automation troubles which so far I’ve been battling with a custom automation but it seems I have an issue with the blueprint that hasn’t been mentioned above;

I have an Aqara Human Presence Sensor in the bathroom and it sometimes triggers false positives (wind moving curtains etc.) - when this happens it triggers your blueprint but doesn’t seem to recover (i.e. the input_boolean remains on until the next event).

Hi I’m would you consider implementing a QOL change and expose the ability to add conditions through the UI this would be really helpful, Thanks.

@alexbabel

Hello there,

I found a bit problem your automation.

So i create a automation, that turn on the light, when door is open and turn off it, when input_boolean from this blueprint switch off.

My problem is that i open the door and sit more time in the bathroom (more that about 2 min) the light turn off. When i do some movement, it turn on again and it still on when i’m here in the room. (so works as expected)

it possible to somehow fix that first time not switch off the light and not need to move it?

Edit:

I think solve the problem:

binary_sensor:
  - platform: template
    sensors:
      wc_motion_template:
        value_template: "{{ is_state('binary_sensor.0x00158d0007e2d214_occupancy', 'on') }}"
        delay_on: 0:00:15
        device_class: motion

Create a sensor template, and delay the “on” state 15 sec.

How did you make the automation? With Node-Red or with Automations?
Can you share your automation with me?

Hello, here:

- id: '1673703971400'
  alias: Földszint WC lámpa kapcsolás
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_boolean.wc_jelenlet_foldszint
  - platform: state
    entity_id:
    - binary_sensor.xxxxxxx_contact
    from: 'off'
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.xxxxxxx_contact
        state: 'on'
      sequence:
      - service: light.turn_on
        data: {}
        target:
          device_id: xxxxxxx
    - conditions:
      - condition: state
        entity_id: input_boolean.wc_jelenlet_foldszint
        state: 'off'
      sequence:
      - service: light.turn_off
        data: {}
        target:
          device_id: xxxxxxx
  mode: single
1 Like