As the title states, i need some help with setting up the motion sensors to control lights in the kitchen. I have the following devices that work together in these automations:
The Motion Sensor - Sonoff SNZB-03
An aqara mini switch that is set up to manually control the lights.
3x Tuya RGB + CCT lights hereafter “The Light”
A timer Helper with 2 minutes to extend the time the lights stay on. The sensor itself has one minute.
The concept is the following:
When the motion sensor detects motion in the room, it activates a specific scene depending on the time (turns the light on) and also starts the 2 min timer. Here is the automation:
The next automation is to turn the light off, when the timer expires. Here i faced a big problem, because the lights were turning off even if i continued being in the room and moving in which case the sensor’s state is on (motion detected). So i set a condition to turn the light off only if the timer expires and the sensor state is off. Here is the automation:
alias: Turn Off Kitchen Light on Timer Expiration
description: ''
trigger:
- platform: state
entity_id: timer.kitchen_motion_timer
to: idle
condition:
- condition: state
entity_id: binary_sensor.motion_sensor_1_ias_zone
state: 'off'
action:
- service: light.turn_off
target:
entity_id: light.kitchen_ceiling_lights
mode: single
I have also a 3rd automation that disables the above automation if the lights are turned on manually via the aqara mini switch but it is not relevant as this is not the problem.
The problem is this: I walk into the room, and the lights turn on. If i leave the room before the 2 minutes that the timer expires, everything works great. But if i stay in the room after the timer expires and i get out after 5 minutes, the automation to turn the light off doesn’t work because the trigger is already triggered but skipped because the sensor still detects motion.
How can i run an automation (in this case the one that turns the lights off) again and again until both timer has expired and the sensor state is off (no motion) in order to turn the lights off? I feel i am very close but still it doesn’t work for me. Any help would be appreciated!
The mode of the first one should be restart not single. And I cancel the timer (in case it is already running) and then start it. That way the timer is always restarted in the first automation.
In the second one, instead of looking for the state, you should be listening for the event timer.finished with the data of entity_id: timer.the-timer-name
Thanks for the quick reply. It didn’t work for me unfortunately. Are you using a motion sensor or a door contact sensor in your automation? The motion sensor works differently. It triggers when the first motion is detected and then it goes into a do nothing mode for a minute but keeps it state to on. Then it checks again and if there is no motion it sets the state to off.
Also, if i listen to the event timer finished on the light turn off automation, the light will turn off regardless if i am in the room and moving.
That’s why I set the mode to restart. I cancel the running the timer, and restart it when motion is detected, that way the timer will keep running for as long as there is motion. I then react to timer.finished which will only fire when no motion has been detected for the duration of the timer. mode: restart is crucial to making that work.
And I use door contact sensors, and motion sensors. Here is how I turn up the volume as I walk past a speaker when I am playing music across the house, and how I reset the volume to almost muted after a period of time:
You also don’t need two triggers of the same source and state to look at different conditions. Was there something additional in this automation that wasn’t listed? I would +1 looking at the timer finished event. Looking at the timer idle state would evaluate as true and trigger when starting up home assistant.
At first let me thank you all again for trying to help. I did try the solution of restart which totally makes sense, however, timer finished and it didn’t restart resulting into turning the lights off even if there is movement. I set the restart on the first automation that is turning the lights on and starts the timer.
The way that I solved it is by changing the automation that turns the light off. Originally, it had a trigger when the timer expires (by state). Now I added a trigger when the motion sensor has off state.
That way, even when the timer expires but the sensor reports movement the automation triggers uppon expiration but it doesn’t turn the lights off. When the movement stops it triggers again and the lights turn off.
For a beginner I think that it’s fine even if I would like to have everything within one automation. I will study yours and create a test automation to join everything!
That’s because you were originally listening to the state of the timer, rather than the timer finished event. With the timer state, even if you cancel the timer and then start the timer every time motion is detected, briefly the timer state will change from active to idle which would trigger the automation to turn the lights off.
Listen for timer.finished instead that will only fire when the timer actually completes the countdown.
I also had a problem with a light controlled by a motion sensor, which also had to work in different modes.
With just 1 automation and a helper, I couldn’t get it to work as wanted.
Now I have 2 automations:
1 that turns the light on at motion
the second that turns it off when there is no motion after some time
With a helper, you could influence the second automation from firing or not.
Don’t know if this is the ‘best’ way but it seems to do the job - only a few days in place now.
Unfortunately i cannot set it up… I would like please your help if you have the patience. I started from the beginning removing all automations and set up the first one that is supposed to turn the light on, when motion is detected. Please keep in mind that depending on the time, different scenes should activate. Here is what i have so far:
It seems to be evaluating correctly (so it is choosing the correct scene) however, even when set to restart, it doesn’t replenish the timer and therefore, even if there is motion the timer expires.
Let’s fix this at first and then we go to the automation that turns the lights off.
The trigger will ALWAYS either be the first or second trigger because the ID is just how you refer to the trigger. Your time checks need to be in the choose blocks, in order to decide what scene is activated.
You should only have ONE trigger for this, because both triggers are pointing to the same device.
The way I did mine was to set up scenes for the lights for different brightness.
Then 2 scripts to turn on/off the lights and in the ON script manage the time of day logic to call each the required scene.
Then use the automation just to decide when to call the on/off scripts.
e.g. is there motion: yes, is it dark enough: yes, call script ceiling.light.on
In the ON script, is the house set to “sleep”: no, is it after midnight: yes, call the 30% brightness scene
The script logic looks for a match from the top down, so best to set your dimmest setting first then gradually brighter until you get to 100% at the bottom of the script (found that out the hard way)
In case you’re wondering why there are double OFF commands, my Ikea Tradfri bulbs sometimes don’t fully switch off but running the request twice fixes that.
I will try to revive this topic.
How about his automation:
if motion is not detected for 2 minutes dim lights to 50% of its current value.
if no motion is detected for another minute, turn lights off.
if motion is detected within that 1 minute bring lights to original brightness and start waiting cycle again.
I am new to HA, played with some automations, but here I am reaching my current limit, could somebody please point me to right direction?
Yes you are correct, although I didn’t reply to this post I have solved it like you propose. The motion sensor turns the light on when motion is detected and then instead of turning the light off immediately, it reduces the brightness to lower percentage twice to give a visual indication that the light will eventually turn off.
Although the ultimate solution would be an mmWave sensor, this is the best workaround I have found so far.
So, i figured it out thanks to your post, however I am using timer helpers instead of bulit in timer, so it sholud survive restart, that is just technicality.
However there is one issue I can’t figure out yet. I want the lights to dim to 50% of their actual brightness and then bring them back to that original brightness (rather than 100%)
I created an input helper to store original brightness into before dimming, but when calling a service in automation I don’t know how to use that value in brightness_pct and how to store the brightness in it. How to properly handle that in automation?