Another take on room occupancy

Hi everyone!
since a few people have asked about my room occupancy setup, I decided to share it.
I’m was searching for a propper way to do occupancy tracking for almost three years now, and (like a lot of people) tried a lot of options like motion sensors, room-assistant, ultrasonic sensors, cameras, jseidl’s hass-magic-area (great project, can recommend, but wasn’t propperly working with room-assistant), but all of those options have downsides like motion not being picked up when you dont move, jitter between rooms with bluetooth tracking, having multiple people inside the home, having internet connected cameras everywhere in my home. So I came up with a little python script that combines a lot of those techniques for a more solid solution.
In my case, I use motion sensors in each room and room-assistant to track our watches.
The script was written in a way that can be easily extendable. Once the system picks up that the room is occupied a timer starts, which is then used as a trigger for whatever automation you can think of.
I hope someone has a use for this, if you have any questions, feel free to ask!
Github Repo

5 Likes

Hi. I am looking over the sample files and script, but am confuse how will the script knows what the timer and input boolean entities are. I see the timer.livingroom_timer in your automation file, but how does the script knows when to change the state if it is not referencing the entity within the script?

Thanks for sharing

1 Like

Hi!
the configuration happens in the Livingroom Occupancy Automation, lines 15-21. It is mandatory to name the timers timer.{roomname}_timer, inside the script I get the roomname in line 3, which then gets used in multiple places (see line 31 for example) to build the full entity name of the timers. I just realized I forgot to add the input_boolean to the config (I found that I didnt even use the input boolean inside this script, still have to fix that lol). I’d suggest to add overwrite input booleans under “entities_toggle” (like line 17) and probably as a trigger as well, so if you just toggle the input boolean the room gets instantly marked as occupied. I’ve just edited the automation file, it should work like this.
Hope this clears things up! :slight_smile:

It’s a pleasure, if you have any problems feel free to ask :slight_smile:

Hi Yea. I had a go with your script today and I have figured how the script works for the most part. I did noticed the input boolean was missing from your example. Thanks for adding that in in your example. I am waiting for my esp32 d1 mini to arrive so I can setup ESPresense and use it for a more accurate occupancy.

1 Like

I have been observing how the script works close to a week and notice one thing I would like the script to do. That is to turn the lights on again when the lights are manually turned off while the timer is still counting down to zero if the trigger entity has been triggered. Currently, the script just resets the timeout.

You can just work around that by changing your light off toggle to mark the timer as finished instead of just turning off the light, is that feasible for you? Since I also use it to mute/unmute my media players that’s the way I went. If not I can help you edit the script accordingly

If not I can help you edit the script accordingly

That be great if you can show me what I need to edit within the script. Thanks.

Just had time to think about it, you’ll probably need something like the following:

light_data = {"entity_id": YOURLIGHT}
hass.services.call("light", "turn_on", light_data, False)

just before or after the timer starts (after 67 or after 75, shouldn’t matter)
check out the python script documentation, there is an example for lights under “calling services”

If you have any questions, feel free to ask :slight_smile: