I’m still a newbie with HA and probably my question is a basic one, but bear with me, please.
I want to turn on a light by when motion (or any other event, for all that matters) is triggered on a sensor and turn it off after x minutes (let’s say 5), but only if the light was originally off or extend the 5 minutes before turning it off if the light was turned on by the motion sensor only.
I’m want to avoid turning the light off after 5 minutes, when there’s motion, if the light was already on for another reason (another switch, scheduled, or whatever).
It would be something like this:
Event is triggered (motion)
– Light is on (for another reason): do nothing
– Light is off: turn it on and schedule turning it off after 5 minutes
– Light is on (was turned on by the event): keep it on and reset the off timer and shut it down after 5 minutes from now
I could probably create an input boolean to keep the “turned by event” status on the light, but that makes me create another entity which I think is overkill for this (or isn’t it).
It would be cool if we could just create attributes on entities and set values to it directly, on the fly, like “turned by motion: true or false”.
What if you turned the light manually (because you want it on) and then you trigger the motion sensor by passing in front of it. Will the light turn off after 5 minutes or will it stay on (as I mentioned it’s a requisite on my post)?
it turns off after 5 min.
understand your requirement, and is ok , but I prefer to think in the energy efficiency and if no movement for 5 min, it turns off .
to keep things as you want, it would imply to have a condition on the turn off automation, if it was turn_on by automation.sensor.office_pir turn_on desktop_light_if_Night
nevertheless, you could have another autmotation like mine, but having a 30 min - then off . it would turn off 30 min after no movement .
I think I did it, as I mentioned in the original post, by creating an input boolean (called outdoor_light_turned_by_motion) to store if the light was turned by the motion or not.
Yes, it’s really easy.
All you need to do is have a delay in the automation to turn the light off.
Set the automation to restart, instead of single mode.
Then every time the automation is triggered, it restarts which means the delay is restarted.
I use a timer instead of a delay in the script, just to decouple from the automation - but it’s the same basic idea.
Not sure what’s happening to cause the switch to not trigger? The screen shot below shows the motion detector is triggering. And when I click on Run Actions the switch activates. So not sure why the switch is not triggering via the automation?
I tweaked things a bit and it now triggers the switch… only, it turns off while I am sitting in the room. So the PIR is not seeing me moving, so turns off the switch, which turns off the light.
Looks like I need to find a better movement detector, that isn’t a PIR I guess. I’ve heard there are some new movement sensors around, so will look into it.
What I would do is put a wait time since last time motion was detected. Not quite as compact as your code, but this works flawlessly in my house. I have a seperate automation to turn on the lights based on the motion group going off, so this runs after motion is detected… starts at 6pm my time until sunrise –
Maybe a little bit of this code could be added to yours around the wait for trigger.
- id: "1631764775708"
alias: Kitchen Light OFF 7 mins after motion
description: Kitchen Light OFF 7 mins after motion
trigger:
- platform: state
entity_id: group.kitchenmotion
to: "off"
from: "on"
for:
hours: 0
minutes: 7
seconds: 0
milliseconds: 0
condition:
- condition: or
conditions:
- condition: time
after: "18:00:00"
- condition: sun
before: sunrise
action:
- type: turn_off
device_id: 4856f51b9d4f30xxxxxxxxxxxxx
entity_id: switch.kitchen_light
domain: switch
mode: single
For anyone looking to do this in 2024 via the GUI, it’s really easy now with the Scripts section under Automations. For my use, I wanted a virtual button I could press to turn the garage lights on for ten minutes then turn them back off again. Start a new script then just add things in the order you want them to happen. Delay is inserted by clicking “Add Building Block”. Mine looks like this: