I have a basement where I want lights to turn on automatically when I enter (by opening a door → door sensor) or if movement is detected (by the use of multiple PIR movement sensors: this is because sometimes the door is kept open, so lights in this case will turn on because movement is detected). I also want the lights to turn of after 3 minutes of “no movement”
So far I have solved by using three automations that work this way: #1: when door sensor turns to open or any movement sensor detectes movement, start a 3 minutes timer #2: when timer goes from idle to active, turn on the lights #3: when timer goes from active back to idle, turn the lights off
Since my movement sensors have a timeout of about 90 seconds, if a person moves after 90 seconds the sensor should be triggered again and start the timer again keeping the lights on.
It was not perfect but worked most of the times. Now I’ve bought a Sonoff Presence Sensor (I think it works using mwaves) and I would like to add this to the picture, so that lights would stay on when someone is in the basement, even if not moving.
The problem is that I have no idea how to add the presence sensor to my automations, nor how to write a completely new one that serves my purpose.
It sounds like you are going about this the wrong way.
I would use a single automation since you are controlling a single entity(ies), in this case your lights.
The lights coming on is either triggered by the contact sensor opening or the movement sensor changing to detected.
The lights switching off is triggered by the movement sensor changing to clear +3 minutes.
You therefore don’t need the timer.
In respect of the presence sensor, I have the Aqara FP300 and it offers PIR movement detection and mmwave presence. At the moment, I am testing it but I have the following triggers:
I see your point, but (correct me if I’m wrong) your automations won’t cover this scenario: I open the door and leave it open.
Sometimes we do it so that the cat can go sniffing around down in the basement. In this case I would like the lights to turn off after a few minutes (the presence sensor “should” not detect the cat although I still have to test the sensitivity levels to achieve this) but with your automation I don’t think they would.
Am I wrong?
EDIT, I tried your suggestion and I can’t understand what I’m doing wrong but the lights will still go off after two minutes even if the presence sensors still detect presence. Here is the YAML, if you are willing to give it a look:
I don’t think it has to be any more complicated than that. There’s no need for a timer in this situation, the only addition is you could keep 1 motion sensor to trigger the light on and position it for optimal results, then have the presence sensor keep the light on until it’s not detecting.
With multiple PIR’s and a Presence sensor I would be tempted to ask if a simpler solution might work, specifically:
Do you even need the door sensor in the automation any more, could you just add all the sensors to a Binary Sensor group such that:
If any sensor is detecting movement or presence - turn on the light.
If all sensors are clear (none are currently detecting) turn off the light.
I have found that the presence sensors are not perfect so I use a small delay (the sensors have to be off for a while before the light is turned off) but otherwise I just do as I described above (all my sensors are Sonoff - both IR and Radar).
Yes. It was stated they will open the door for the cat. They want the light on for a few minutes. But, the presence sensors will be set to not detect the cat.
While I read quickly, its seems to me that the sensor hasn’t sent a new response to trigger the timer reset.
Using timers is the best way to do presence, as it’s easy to add new devices. You can do it all in one automation with trigger IDs and using queueing.
To debug, look at the log and see what triggers are missing. Perhaps you are not resetting the timer correctly.
Timer is easiest because it’s the only entity to control the light. Note how complicated the “simpler” methods suggested above are.
Also, the best design is to think about presence, not light control. Create an entity called Basement Presence. Make the light respond to basement presence buy the rules you want.
I used to have (now replaced) an intermittent IR sensor - occasionally it would not send an event (both detected and clear events). I created (an admitted convoluted) set of binary template sensors based on the real sensor.
I think something similar would work for the the door, i.e. your “template door” would register as open if (and only if):
The real door is open. (and)
The real door has been is open for less than 3 minutes.
You could then add the “template door” to the same binary sensor group, so the light would now be on if, either:
The door was just opened - in the last 3 minutes. (or)
So, after re-reading through everything and noting that many of us missed/ignored requirements (especially me where I skipped the initial question and answered with what was originally being used), I stick with my original expanded answer.
Use group helper for your presence sensors and only trigger on the helper.
It looks like you have two door sensors too. Group them as well.
Triggers for your open group, presence group, and timer finished.
When open or presence, start timer (will restart if already running) and turn on light.
When timer ends, check for presence. If presence, start the timer. If not, turn off light.
Why your current code is not working is what looks like five presence sensors. When one goes not_occupied, it turns off the light. But, other presence sensors can still show presence. The group will handle this.
First of all, thanks to all of you for the help and time you are dedicating to my issue.
I’ve read all your suggestions and I came to the conclusion that my initial approach using a timer is probably what would work best in my use cases.
I learned that I should (and I will) stop using devices and use entities instead. I will update my automations to reflect that.
The intended behavior of my original automations was that as long as anyone moved in the basement (and triggered a movement sensor) the timer would restart. This actually doesn’t happen and I find myself very often with the lights turning off on me and then back on after a few seconds, when the movement sensors catch a new movement.
So, keeping this in mind, my problem now is how to add the presence sensors to the mix. I’ll try to explain better: I have no idea how to have the timer restart each time movement is detected and/or presence is “on”, because I don’t know how to code something like “keep lights on AS LONG AS presence is detected”.
Or maybe this is a limit that’s would be solved by using entities? It’s late now and I don’t have the time to try …
I would love to have a “simpler” automation work but my scarce abilities in using HA don’t allow me to write an automation that’s sophisticated enough to cover all possibilities (especially the one where I open the door but no actual presence will be detected by sensors because no one will actually go down to the basement).
I only read this after having written my reply above this one… Thanks for the suggestions and I will try to implement them ASAP.
Just as a clarification, I have two door sensors, four PIR movement sensors and two (momentarily) presence sensors that will probably reduce to a single one as soon as I find out which one is more reliable, as one is battery operated while the second one uses a power supply.
Exactly, and I think this is why IMHO it wouldn’t work: if I open the door the lights will turn on triggered by the door sensor, but when no one (except the cat) actually goes down to the basement, no presence will be detected ever and therefore the lights would never be turned off. Am I wrong?
It sounds like the external timer is what you are looking for and, with so many sensors, creating grouped sensors, as suggested by others, would be the way forward.
You can still handle this in a single automation though.
door_left_open - One or more doors have been open for 3 minutes.
turn_off - All doors are closed or All motion/presence detectors clear.
The turn_on condition is simple - turn the light on.
In all other circumstances it checks the current status of the motion detectors and sets the light status accordingly.
If you open the door for the cat - since the motion detectors won’t pick it up the 3 minute timer will fire (or if you close the door) and the light will go out.
Otherwise if someone is in the basement the motion detectors will take priority.