Set room occupancy based on manual light press

I’m looking for a way to turn on a binary sensor of ‘occupied’ based on multiple domains turning on. My guess is that the best way to go about this would be to create an automation with the trigger as an event type of state_changed, and then look at the domains such as light, fan, sensor, etc.

What I then want to do, is turn on the binary sensor called {{area_name}}_occupied

I would rather not make multiple helper groups for each domain, and just look at any light or any fan or any sensor that has changed state in that room.

Any advice would be appreciated!

The resulting automation will trigger frequently because there’s always something in your system that’s changing state, especially sensors.

You can check this by going to Developer Tools → Events and listening to the state_changed event.

Do you have temperature, humidity, or illuminance sensors?

How does occupancy correlate with the changing values of those sensors?

That would be the most efficient strategy because it constrains occupancy detection to specific (and most relevant) entities.

I’m mainly looking at lights, fans, and motion sensors. If either of those domains changes state, then I would consider the area to be occupied. A manual light change versus the light coming on with my presence detection. or even a manual fan change.

What I don’t want to do is create a single automation for each room that looks at the light helper group, fan, and presence to determine occupancy. I would rather use the existing area that is applied to each domain to see if a state changed for any of the three domains, and then turn on that area’s occupancy boolean.

This would easily allow the room to continue to determine occupancy if “guest” mode is turned on for our guest room, and other automations are disabled, but you could still determine that the room is occupied by the guest manually turning on switches in that room.

Your plan to listen for the state_changed event will cause the automation’s Event Trigger to detect state-changes for all entities (light, fan, binary_sensor, sensor, lock, update, scene, etc).

I actually figured out a more elegant solution.

I have two trigger IDs, one for ‘on’ and one for ‘off’ on state changes, with each light entity listed in each trigger ID.

Then I have the following condition, seeing if this was triggered manually by a press. This means that the context was not triggered by an automation.

{{ trigger.to_state.context.parent_id is none }}

Each one of my rooms has an input boolean for ‘on’ or ‘off’ for occupied. The input booleans start with the same naming convention as my areas. The automations for turning on lights, etc are based on these input booleans rather than individual automations.

action: input_boolean.turn_on
metadata: {}
data: {}
target:
  entity_id: input_boolean.{{area_name(trigger.entity_id)}}_occupied