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
1 Like

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).