Looking for template sensor help - Occupancy puzzle

I think a template sensor is the way to go, but I’m open to other solutions.

I want to know if a room is occupied. I have 4 sensors to work with. 3 are motion sensors, and one is an open close sensor. So, all binary sensors

Two of the motion sensors are in the room.
One motion sensor is triggered if you enter the room, but otherwise not able to be triggered from the room.
The open close sensor is for door entry into the Room.
The only way to get into the room is through the door or by passing the motion sensor outside the room (it’s in a stairwell to the room)

I’m thinking that basically if the last changed of either of the two motion sensors in the room is newer that the other two sensors, then someone is in the room and has not left.

Only thing is in the past you end up running into possible false positives if one sensor takes a few seconds longer to “turn off”, so I think I need some sort of buffer or timer. Also It’s not uncommon for there to be no motion detected in the room even though someone is in the room. I think that will still be ok as long as the sensors in the room were the last active.

Anyone have any thoughts, or suggestions.


Adding some extra details. I’ve got the general idea, but need help getting them all together
This works for two sensors

{{((as_timestamp(states.binary_sensor.bedroom_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_stairs_motion_sensor.last_changed)) }}

These are the names of all the sensors


Edit 2:
I’m close, but this does not work

       device_class: presence
       value_template: >-
         ((as_timestamp(states.binary_sensor.bedroom_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_stairs_motion_sensor.last_changed)) 
         ((as_timestamp(states.binary_sensor.bedroom_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_door_open_close_sensor.last_changed)) 
         ((as_timestamp(states.binary_sensor.closet_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_door_open_close_sensor.last_changed)) 
         ((as_timestamp(states.binary_sensor.closet_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_door_open_close_sensor.last_changed)) 

I think I got it. Just sharing for others

         (((as_timestamp(states.binary_sensor.bedroom_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_stairs_motion_sensor.last_changed)) 
         ((as_timestamp(states.binary_sensor.bedroom_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_door_open_close_sensor.last_changed)))
         (((as_timestamp(states.binary_sensor.closet_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_stairs_motion_sensor.last_changed)) 
         ((as_timestamp(states.binary_sensor.closet_motion_sensor.last_changed)) > as_timestamp(states.binary_sensor.bedroom_door_open_close_sensor.last_changed))) 

Now just to figure out how to keep my doggo from messing me up when he goes up/down the stairs. haha

Nice job! You may be able to clean it up a bit using groups or template entities. These both allow you to combine multiple sensors into one, so you could create one for the occupancy sensors and another for the entrance sensors, and then do a single comparison between the last_changed of those two sensors. This would also make it easier to add more sensors if you ever need to, since your current method would quickly get out of hand with just a couple more :slight_smile:

Ahh, good call. Groups could help for sure.

Also anyway to carry over last changed after an HA restart, or is there a better attribute to use?