Presence detection for multiple rooms > Templates?

Hi All,

I have about 10 motion sensors to install throughout the house. My idea is to control lights, AC, music, etc based on presence in rooms.

To make it clean I’ve started to create Helpers of type Toggle and the helpers would be called for example:

  • input_boolean.office_presence
  • input_boolean.kitchen_presence
  • input_boolean.bathroom_presence

Based on an automation I would then set a trigger:

binary_sensor.office_motion_sensor_motion = Detected

with action: set the appropriate above toggle to “On”.

With my current knowledge of HA, I would create 10 automations and would need to maintain these 10 automations as well… So my questions is if there is a way to configure this with 1 automation if I use the same naming convention for my sensors and for the toggles?

I’m pretty sure this is possible with Templates, but I can’t get my head around it :see_no_evil: I do everything in the GUI with regards to automations, so bonus points if someone can put me on the right path if this can be done in the GUI. If not, happy to learn some more about YAML :grinning_face_with_smiling_eyes:


I am sure there is a more elegant way to do it that I have, but I am very interested in your final solution… I am outlining briefly my paradigm below - and the only place where I had been able to centralize code for same.

First of all the kinds of motion sensors you use is a key consideration. Are they more expensive ones that really truly can detect with near 100% accuracy if someone is in the room or not (can sense a person’s location as well as if they are sitting still but breathing - or much less expensive ones like what I have which only detect motion -)

My paradigm is when there is motion, (re)start a dedicated timer. A different automation is tied to that timer to turn the lights on when the timer is (re)started and another one to turn the lights off when the timer is cancelled or finished. (I am a big believer is keeping each automation very simple.) I have updated the code to handle widgets on my dashboard for: sliders to change the duration of the timers, as well as dropdowns to mark each automation enabled or disabled.

The only thing that all of the above would have that is the same for every room is - when motion is detected - they all ultimately end up (re)starting a certain timer with a specified duration, and only if the automation is enabled. Therefore all the automations call one script to which I pass three entities - the timer entity, the input_number entity with the duration, and the dropdown helper entity representing whether or not that automation is enabled.

I have three examples below: 1. How the script is called from an automation, 2. How the script is called from another script, and 3. The script itself that is called for different rooms from all over the place:

  1. How the script is called from an automation (action section):

  1. How the script is called from another script (note you have to select call the serviced, and then switch to YAML to enter the data, then switch back to the visual editor to see it this way):

  1. The script itself that is called for different rooms from all over the place:

Finally, a couple of notes:

I would LOOOVE to be able to combine all the above functionality into one generic piece of code to remove redundancy (separate copies for each of the rooms that are handled the same way). Of course you would lose the ability to custromize one room different than another.

A. Automation to turn the light on when the timer starts.
B. Automation to turn off the light when the timer ends (if automation is “Enabled”)
C. Automation to, if the timer is active, to stop that timer if the dropdown is switched from “Enabled” to “Disabled” (If the light is a wall switch you treat it differently than if it is a smart plug for a lamp (otherwise WAF goes down when the spouse cannot turn the lamp on manually etc.)
C. Automation, if the slider is changed, and the timer is active, to (re)start the timer with the new duration.
D. On HA startup if a timer is active to turn the related lights on if the automation is enabled
E. On HA startup to turn the light off if the timer is inacive and automation is enabled and the light is turned on

I don’t care (and have no choice for reliability with these motion sensors) if there is nobody in the room while the timer is still counting down, the lights will eventually go off, but they will not go off if anyone is in the room because every time they move, motion is detected, and the timer (re) starts. If they don’t move for 10 minutes, that is fine too if for example the duration of the timer is 15 minutes.

Then I ran into an issue where if people turn off a wall light switch and then walk out of the room, sometimes the motion sensor will detect the movement just before they leave the room and turn the lights back on. So I have another input_text (because datetime does not work well for this for some reason) which:

F. Automation to place the current exact time into that input_text whenever the switch is turned off (only when done manually, not by an automation or a dashboard change).

Then I went back and changed all my logic for turning the lights back on from motion being detected, but only if it was not done within 5 minutes after the light was manually turned off.


My next steps are to do some overall logic, such as if there are only two pople in the house and motion is detected in two different rooms at nearly the same time, then all timers for the other rooms that are actived can be cancelled so the lights in those other rooms turn off - but only for those rooms where that automation is turned on…

(I have not done the last paragraph above yet - )

So, I am VERY open to simplification and removing other code dpendencies but not sure where to start other than making for example just pieces of A, B etc. generic but I still have to have those automations to call such generic scripts anyway !

My way is onerous to say the least - but it works well - but I am VERY open to how to improve this…

What a hassle!