Hey guys, I’ve been struggling with an automation and have not been able to figure this out or find a good example. What I want is to: On motion, wait 2 minutes, then turn on a light, keep the light on while there is motion and another minute after the last motion, then turn it off.
As you can see in my code below, I made it work for the part where the light comes on right away and stays on 1 minute after the last motion. But I’m not sure how to make it wait the initial 2 minutes. I’ve considered inserting a second timer for the delay which comes on with motion as well, but I would need some boolean variable to only make it run the first time it sense motion, or maybe a counter, either inserted as part of an if/then block…but I’m not sure how to make that work. Any ideas? Thank you so much!
alias: example
trigger:
- platform: state
entity_id: binary_sensor.q_sensor_motion_detection
to: 'on'
from: 'off'
for:
minutes: 2
- platform: state
entity_id: binary_sensor.q_sensor_motion_detection
to: 'off'
from: 'on'
for:
minutes: 1
condition: []
action:
- service: 'light.turn_{{ trigger.to_state.state }}'
target:
entity_id: light.z_wave_plus_700_series_s2_dimmer_switch_10
mode: single
NOTE
The usual way this is done is that the first State Trigger (detect motion) doesn’t employ the for clause and simply triggers for any motion no matter how brief.
In the posted example, the light won’t be turned on until the automation detects 2 solid minutes of motion.
So you would need continuous motion for this? What if there is a break in motion, there’s motion at t1=0s and then more motion at t2=60 or 90s or 115s but nothing in between?
Yes. The binary_sensor representing your physical motion detector would need to report on for a continuous 2 minutes in order to trigger that State Trigger.
Because that’s what you agreed to when you replied “the former” to my previous question.
If that not what you actually want, then you’ll need to clarify your requirements.
Thank you for walking through this with me, @123. Here’s the exact use case, I was trying to avoid the details
This is for a small toilet room part of a larger bathroom. If a person walks in there for #1 and they’re in and out in 2 minutes, don’t turn on the fan, no need. But if they go in and start playing Candy Crush in anticipation of #2 (so they’re still in the bathroom generating some motion), turn on the fan after 2 minutes and leave it run for 5 minutes after the last time motion is triggered. I have set the sensor timeout to 30 seconds and sensitivity high so if you’re in there on your phone or reading a book, it will pick you up.
This is really what I’m working through. The part that I’m struggling with is that initial delay. Does this make more sense now? I know that it’d be easier to just turn on the fan right away on motion, it’s how it works now…but let’s just say that I received some push back.
It all depends on your motion detector’s ability to detect their presence, even when they’re virtually motionless.
For example, if their activity causes your motion detector to report motion for several seconds, followed by no-motion, followed by motion, etc, all within that 2 minute window, it voids the use of a State Trigger with a for option. Every time the motion detector reports off it serves to reset the for option’s countdown.
You would have to be using mmWave for this to work, and it seems unlikely you have a powerpoint in a useful spot for it.
As Taras says, you come into the room and it triggers motion. Then you either leave 30 seconds later triggering no-motion after the cooldown period; or you sit down which also triggers no-motion after the cooldown period. Either way, after 2 minutes there’s never going to be motion (except the one going down the toilet ).
And while the cooldown for my Hue motion sensor is 10 seconds, for my Aqara sensors it is 2 minutes (although I think this can be adjusted at the expense of battery life).
You may be able to achieve what you want by combining with a door sensor. Door open then motion then door closed is somebody coming in; whereas motion then door open is somebody leaving. If somebody came in 2 minutes ago and haven’t left, then start the fan. Unless you like to do #2 with the door open
I actually started looking into some TVOC sensors for this purpose but they don’t seem to be sensitive enough. It would have made things so much simpler. Maybe a seat pressure sensor if I can find a compact one.