Hello, I want to automate some lights around my house to turn off automatically when they have been “on” for enough time and some associated motion sensor has not detected motion in a X amount of time. I don’t want to rebuild this automation for each case, so I thought about using a script for this that takes those as arguments.
But I’m not sure if that’s a good idea or the right way of doing it.
Can scripts react to events given the proper inputs (the light and the sensor) or not? In that case, what should I use instead? A blueprint?
The idea behind that is not to turn lights on or off based on motion, I already have automations taking care of that. It is to prevent lights that have been turned on manually from being kept on for too long. So the wait times are going to be something like an hour or something like that.
alias: Sängvärmare on/off
description: Davids och mammas
triggers:
- entity_id: switch.davids_sangvarmare
to: "on"
for:
minutes: 30
trigger: state
- trigger: state
entity_id: switch.mamma_sangvarmare
to: "on"
for:
minutes: 30
- trigger: state
entity_id: switch.sonoff_1000b711c4
to: "on"
for:
minutes: 30
- trigger: state
entity_id: switch.limpistolen
to: "on"
for:
minutes: 30
- trigger: state
entity_id: switch.hallen
to: "on"
for:
minutes: 5
conditions: []
actions:
- data: {}
target:
entity_id: "{{ trigger.entity_id }}"
action: switch.turn_off
mode: single
So bed heater, hallway lights and daughters glue gun is shut off after a period of time automatically.
More and more devices has been added, so it should actually be renamed.
I realize I could make this a lot smaller but it has never been done though.
That is a very good solution for many situations.
However, for this particular scenario I also want a motion sensor to be involved, so I don’t turn off the light prematurely
Then add one by all means. Nothing says you can’t except for that paradox you have in your ask about not wanting motion.
The question was do I use… The answer is if it has triggers it’s an automation if not it’s a script. You need an automation. You CAN bud a script and call it from the automation of yih needed to reuse it elsewhere but probably not necessary on this case.
Blueprints are for making reusable scripts that end users can select the actual entities.
I see where the confusion is coming from here. I said what I said about motion because I don’t want a system to turn lights on or off based on motion. That’s a solved problem, and I specifically said that because I didn’t want anyone to just point me to use one of the many “motion-lights” blueprints available. That’s a solved problem, and that’s not my problem.
What I want is to build some kind of watchdog for turning off lights that may have been forgotten after turning them on manually. This watchdog should use the time the light has been “on” and also consider the motion in the area (or whatever motion sensor you ask it to monitor) to decide if the light should be turned off or not.
So to summarize, this is not a motion-based lighting system; it’s a watchdog that can take into consideration the time the light has been on, and additionally for better precision a motion sensor. I hope I’ve been clearer now.
You need to create a logical connection between the switches and the motion sensors. There are many ways to do that.
Starting from as simplified version of Hellis’ example (since you did not provide one of your own)
One option would be to use Trigger-attached variables to hold the entity IDs of the motion sensor you want to link to the switch:
alias: Sängvärmare on/off
description: Davids och mammas
triggers:
- trigger: state
entity_id: switch.davids_sangvarmare
to: "on"
for:
minutes: 30
variables:
motion: binary_sensor.davids_motion
- trigger: state
entity_id: switch.mamma_sangvarmare
to: "on"
for:
minutes: 30
variables:
motion: binary_sensor.mamma_motion
conditions:
- alias: Check if motion has been undetected for at least 5 minutes
condition: template
value_template: |
{{ is_state(motion, 'off') and states[motion].last_changed < now() - timedelta(minutes=5) }}
actions:
- action: switch.turn_off
data: {}
target:
entity_id: "{{ trigger.entity_id }}"
mode: queued
It would also be possible to use the Area of the triggering sensor to find motion/presence/occupancy sensors in that Area, if you don’t want to set a static sensor for each trigger or you have areas using multiple motion sensors:
...
conditions:
- alias: Check if motion in Area has been undetected for at least 5 minutes
condition: template
value_template: |
{% set area_last_motion = area_entities(area_id(trigger.entity_id)) | select('match', 'binary_sensor')
| expand | selectattr('attributes.device_class','defined')
| selectattr('attributes.device_class','in', ['motion','occupancy','presence'])
| sort(attribute = 'last_changed', reverse=true) | first %}
{{ area_last_motion.state == 'off' and area_last_motion.last_changed <= now() - timedelta(minutes=5) }}
No, I want to attach motion sensors specifically. Not all areas have a motion sensor, and some areas have many.
Your solution is exactly what I want, thank you very much.
I was not aware that you can attach variables like that to triggers, and the template in the condition is next level.
How did you learned all that? It’s been a while since I checked HA docs in detail, but they were not easy to grasp
I have a doubt about this.
Will the “condition” be re-evaluated once the “timer” of the motion sensor changes?
If the time has passed (let’s say, 30 minutes) but there were motions 29 minutes ago then this will not turn off the light. However, as soon as both surpass the 30 minutes threshold (31 minutes turned on, 30 minutes without motion) then this should be reevaluated and the light should turn off.
Because not both are part of the trigger, I am not sure if this will happen.
One potential solution could be create a “template” sensor, but then I will have to create a lot of them.
Yes, conditions are checked in the moment, they do not wait and they are not pre-checked.
If your template uses a 30 minute timedelta, that’s correct. But, you said turning off the lights based on motion was “solved”, so shouldn’t your existing motion-based automation handle that?
I see this is going to keep coming back to bite me over and over, .
What I tried to explain is that I was not trying to build a motion based lights. That is indeed a very common and solved case. Thar is all what I meant, not turning them off, I mean the usual “motion detected, turn on lights, no more motion, turn them off”. I tried to make clear this was a different case.
But what I want to build is a watchdog to take care of forgotten lights, with the additional help of motion sensors, thar is something I didn’t saw yet solved
The reason we keep coming back to it is because it’s pertinent to the discussion…
If you already have motion-based light control that is well tuned to your situation, arriving at the case you are trying to guard against indicates that either the motion-detection or some other part of the system has already failed. If it’s the motion detection that has failed, then how can you rely on it in your “watchdog”?
I believe the issue here is that it’s if the light was turned on manually.
As in someone manages to sneak by without triggering the motion detection and turn the switch on.
And in such case I would assume a simple 30 minute on => turn off would work fine as I see it.
I really can not gasp the situation we are trying to fix here.
You turn on the light manually by some how not triggering the motion detectors but you still want to turn it off but not if the detectors has noticed the motion.
So… When does that happen? When do you crawl slowly on the floor, turn on the light and then want to turn them off with a guard because you crawled on the floor very slowly?
I really don’t see the scenario.
If you did crawl in there and turned on the light and triggered the motion sensors then the lights would turn on automatically. So that scenario is not part of this scope I assume?
And not to mention we don’t know how this blueprint works, so we are discussing the unicorn and how this works together with the rainbow.
That is because everyone is thinking in the wrong scenario. This is something that we humans do naturally, we think we understand some requirements but in reality what we do is translate them to known examples or at least examples that we can understand, this has happened to me too.
I am not trying to be critical here, but I never said that the lights that I want to turn off are triggered or motion controlled. That was an assumption people made.The situation is exactly as I described, manually controlled lights that I want to automatically turn off but fine-tune it by motion controls.This is probably a easier to understand with an example so let me provide one.
My bedroom doesn’t have motion controlled lights. I don’t need them there and I don’t want them. However, there is a little corridor that leads to my bedroom which has a motion sensor. So here’s one of the scenarios:
I go to my bedroom, I turn on the light, I stay there for 30 minutes, I go to the bathroom, and I come back and I stay enough time to be there one hour. In that scenario I don’t want to turn off the light because it’s clear that someone is around the bedroom so someone must be aware that the bedroom light is on. However, if I turn on the light, stay there for 30 minutes. Then I leave and an entire hour passes without any motion, it’s clear that nobody is around and then I want to turn off the light. Both things must be fulfil, because I can get into my bedroom for 55 minutes and then turn on the light, do I want to turn thar light off in 5 minutes? Clearly not.
This is one of the bedrooms that I do not control using motion sensors but I want to automatically fix if they have the lights turned on for too long and fine tune the decision by motion sensors thar I have around.
Some people may suggest that this is better handle with a presence sensor but I don’t want to put a presence sensor in my room. This can be done with good old algorithmic logic and that is what I’m asking help about.
I’m not sure what are you talking about here.
I am explaining some logic that I want to apply to several different devices, and I’m asking for advice to implement it avoiding repetition
Yes now you did. 16 posts later.
There has not been any talk about this light not being controlled by motion before.
Your first post is quite the opposite of what you now say.
I would even say that what Didgeridrew posted before should work if you just change the time values to 60 minutes.
Both the triggers and the condition