Automatic Room Occupancy - Room-based occupancy based on activity

Automatic Home Occupancy Home Assistant Blueprint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Code based on @gdeboos great start as he has indicated he is having health issues and cannot maintain. Please remove

Automatically turn on and off an occupancy input_boolean helper entity for a single room based on activity or non-activity (with timeout). Current “Activities” include:

  • Motion Detection
  • Media_Player state changes
  • Presence detection (ie ESPresence or Room-Assistant) state changes
  • Household Group/Guest state changes

------SETUP------

The following helper entities are required:

  • input_boolean.[room]_occupancy (input_boolean that will be turned on/off with occupancy)

The following entities are optional for full automation functionality:

  • group.household (A grouped set of people in your house. Logic to turn on occupancy will only run if state is “home”".)
  • input_boolean.[room]_occupancy_disabled (input_boolean that disables the automation)
  • input_boolean.guest (input_boolean that will enable the automation when household is away)

------LOGIC------

Occupied triggered when ANY of the following conditions are met (all are optional).

  • door binary_sensor (or group) state to “off”/closed (for interior rooms eg. bathrooms)
  • media player(s) state as specified (default: “playing”)
  • motion binary_sensor (or group) state to “on”
  • room presence sensor (eg. room-assistant) state as specified

Occupancy is cleared when ALL of the following conditions are met (all are optional):

  • door binary_sensor (or group) state to “on”/open for specified time (default: 0 mins)
  • any media player state to state as specified (default: “playing”)
  • any motion binary_sensor (or group) state to “off” for specified time (default: 10 mins)
  • presence sensor(s) all detected in different room or “not_home”
  • group.household changes to “not_home” state AND input_boolean.guest is off

------OPTIONAL HELPERS------

Uses the following helpers to allow for front end control of timeouts. Defaults are as specified below:

  • input_number.door_occupancy_timeout (in minutes, default: 0)
  • input_number.media_occupancy_timeout (in minutes, default: 10)
  • input_number.motion_occupancy_timeout (in minutes, default: 10)

------To-Do------

------Application------
I personally use this input_boolean for lighting control, so when the boolean goes off, I have an automation that calls service light.turn_off for that room’s lights IF the home is operating in “Home” mode (not Nap, Bedtime, Sleep, etc.). I also use it to turn on the lights immediately (transition: 0) when the boolean turns on. This can pre-emptively turn on the lights as I walk down the hallway and room-assistant detects my phone approaching via BLE, with the motion sensor as an immediate backup. Example:

alias: Master Bedroom Occupancy Lights
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.master_bedroom_occupancy
condition:
  - condition: state
    entity_id: input_select.mode
    state: Home
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.master_bedroom_occupancy
            state: 'on'
          - condition: state
            entity_id: light.master_bedroom_lights
            state: 'off'
          - condition: state
            entity_id: input_select.mode
            state: Home
        sequence:
          - service: light.turn_on
            data:
              transition: 0
            target:
              entity_id: light.master_bedroom_lights
      - conditions:
          - condition: state
            entity_id: input_boolean.master_bedroom_occupancy
            state: 'off'
          - condition: state
            entity_id: light.master_bedroom_lights
            state: 'on'
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.master_bedroom_lights
            data:
              transition: 5
    default: []
mode: single
8 Likes

Great blueprint !!

I have one issue with media player. When I turn of my mediaplayer in a room the occopancy state goed to off immediatelly. Even if I specify a input_number.media_occupancy_timeout helper and set it to 10

Any help is welcome

Thanks for reporting back your issue. I’ll look into it and tag you if I find anything.

What media_player integration are you using for your device and what state change is it reporting when you pause/stop the music (from “playing” to “idle”?).

Edit: Also can you confirm that your media_player state attributes has one that is “last_changed”?

Hey @RienduPre can you make the following changes in your config and see if it resolves your issue?:

I used code from someone else and it resolved my issue since I am using the alexa_media HACS integration.

Same result ! Or do I have to create the automation from scratch after a blueprint update?

The media device is a (Philips) TV, the states are on or off

/Rien

And you put “on” in the state field to match? Please post a screenshot of the state attributes of the TV if you would, while playing media and while NOT playing media.

If it’s easier, you can open an issue here: Issues · kreene1987/automatic-room-occupancy · GitHub

Here you go


Schermafbeelding 2022-05-10 om 14.25.40

1 Like

So this is seeming to act differently than most media_player integrations I am seeing. What I am going to recommend FOR NOW is to GROUP this with your motion sensor and make sure you de-select all entities when setting up the group. This way if either the TV or the motion is on, you will be occupied and the motion timeout will also still apply.

For the longer term, I’ll try to implement a looser media_player timeout system. Right now I don’t see a way to track the timeout since there is no timestamp when it was last updated in the state attributes.

Edit: Just for additional background, the current implementation looks for a field of last_changed for the media_player entity state attribute, and if there is none then it times out immediately. It seems like there is a wide variety of media_player state attributes that represent last_changed (my alexa_media players use “media_position_updated_at”) so what I think I need to find is a way to grab the time when the media player last changed state to NOT “playing” and work from that, but I need to do some research on how to get that information (thinking event-based instead of state-based).

Hi @kreene1987

I’ve been able to use this as one of the core components of my system, after migrating from homeseer, so firstly thanks for your work its made things a lot easier for me! The one challenge I have is that I need different timeouts for different rooms - eg for transient areas like bathrooms I set a very low timeout, but in others where there is likely to be more presence without movement I use higher ones. Can you suggest a way of doing this without having to create several copies of the blueprint with the timeout hard coded? I am new to Home Assistant so not great at YAML yet, but I wondered if there was a way to create some sort of array in the config, where you could map an input boolean entity name (representing the room) to a timeout value? For any other homeseer migrants, this would then pretty much replicate the capabilities provided by a plugin called BLRadar.

Thanks

Hi,
Thanks for picking up the mantle on this, sorry to see that @gdeboos isn’t well.

I’m trying to set this up to have a go with it, but I’ve stumbled at the first block. When I’m trying to choose my contact sensor, it’s stating that I there are no matching entities, no matter what I enter in that box.

Unlike with the motion sensor dropdown, the door sensor dropdown never displays any of the contact sensor entities in my home . :confused:


Any ideas why it won’t pick them up?

Hi,

I am trying to use this, but obviously motion triggers the occupancy to go off.

My config:

id: '1656673715441'
alias: Occupancy - Flur
description: ''
use_blueprint:
  path: kreene1987/automatic-room-occupancy.yaml
  input:
    motion_sensor: binary_sensor.motion_sensor_home_security_motion_detection
    occupancy_switch: input_boolean.flur_occupancy

Shouldn’t the second case from the left be triggered (picture above)?
Instead the not at home case is triggered.

This leads to OFF on the occupancy entity if I set it to ON manually beforehand.

I got it:
The Household group is required - otherwise the Occupancy will not be set.
The documentation says it’s optional though.

Anyway: I won’t complain as this automation is very helpful :wink:
Thanks for your efforts!

1 Like

All I am so sorry for being away, I have been on vacation and will be through the 5th. Upon my return I’ll respond to all. I am working to optimize the code and improve flexibility while also incorporate multiple devices per setting (I got multiple:true working by deleting defaults if anyone wants to get ahead of me!).

Thanks for your patience and sorry for the lack of communication!

1 Like

I am incorporating a number in the script itself that will override the input_number if it is set. Stand by and I’ll PR for testing with you. Might be a week or so.

1 Like

Hey, can you see if your device class is “opening”? This is the blueprint code portion:

Perhaps it isn’t finding the device due to the opening device filter, you could comment out line 48 and see if that helps!

Edit: Although based on the “closed” state it seems like it is an opening device and should be found… Can you post the attributes of the device under developer tools → states?

Will take a look and update docs or make a change to these conditions to ensure if occupancy is not set up it won’t choose this option.

Thanks for reporting!

Ah, that will explain it! My contact sensor’s device class is door.

Updated the .yaml (oddly line 45 for me :confused:) from opening to door and they show up!


I’ll have a play with this today now. :slight_smile:

And please don’t apologise for taking a break, this is meant to be a hobby. Enjoy the rest of your holiday.

Thanks for the reply… I am new to YAML but trying to learn. Would a simple solution be to just enable the user to set “Room Name”. This name is then substituted as a part of each variable for a helper in the script, when timeouts are enabled (it could also auto-populate the name of the input_boolean for the occupancy switch). The user would then just need to create helpers with the matching room name, eg if I set “Room Name” to “familyroom”, and I enable motion and media timeouts, then the script would be looking for:
input_number.familyroom_motion_timeout
input_number.familyroom_media_timeout
input_boolean.familyroom_occupancy
Not sure if this is the most elegant way but could work?

By the way, I have also been unable to get occupancy via presence detection working using an espresense sensor. I think this is because the script looks for a change from “NULL” to one of the values specified in the list. In reality, the state is likely to change from another room value (or not_home) to the target state. Is there a modification that will allow for presence state to change from ANY previous state to the target state?

Thanks

Thank you for your contribution!
There is no need to be sorry being on vacation :wink:

For those that want to test alongside me, I’ve uploaded a PR with some new concepts (untested) that allow multiple entity selectors for entries and updated handling to ensure when multiple devices are selected it updates.

I’ll be continuing to test in this branch for a bit: https://github.com/kreene1987/automatic-room-occupancy/tree/kreene1987-multiple-true

Only the .yaml file is valid, the import blueprint will still link to the published version above.