I set up an automation to turn on lights via a motion detector and turn off after a minute. Works great except when the Z-wave light switch is manually turned on; it still goes off after a minute. How can I make the switch keep the lights on and still operate with the sensor?
I think your 1 hour project just became a many hour project my friend
In all seriousness there are many solutions to this problem. What I do personally to detect manual overrides is look at context when the lights turn on. This field is present in the trigger
variable and can give you insight into what caused an event to happen.
If context.user_id
is set for example then you know that the event was caused by a user and not by an automation (so like someone pressing something in the homeassistant UI or telling google to do something). If context.parent_id
is set then you know that the cause was an automation. If neither of those is set then that gets a little tricky because there are two possibilities:
- The cause was an external event (like someone touching the light switch on the wall)
- The cause was an automation but the trigger was one that does not set
parent_id
(Iâve seen this withtime
andtime_pattern
so if you use those watch out for this).
For my case I avoid triggers that donât set parent_id
for turning on my lights so I can detect overrides as follows:
-
context.user_id
is set orcontext.parent_id
is null -
from_state.state
is notunknown
orunavailable
(thatâll happen on startup)
I then use that information to set a select
entity in that room which has the options on
, off
, override
. Presence lights only function if its room controller is in state on
. I differentiate override
and off
because override
times out and returns to on
after 30 minutes of inactivity or 2 hours if the room is continuously occupied. off
does not time out, it is used when we sleep or we leave and returns to on
on the inverse event.
I have been meaning to put together a package for this but have been lazy about it, I should get on that. It takes a bit of work to make things sharable, refactoring out all the names, details and making sure all the dependencies are encapsulated. But thatâs the general idea.
Although just fyi I always thought magic areas looked pretty cool. Iâm a âdonât fix what isnât brokenâ kind of person so Iâve been sticking with what I made. But if youâre trying to deal with all the overrides and stuff from scratch it might be worth checking out before investing a ton of time. Seems pretty complete to me in terms of what I automated about my own rooms.
Wow! that is a postgrad level answer. Unfortunately, I am still at a Kindergarten level. Thank you for taking the time to answer.
Make an input_boolean in HA.
This boolean is set to on whenever the switch is manually switched to on and off when it is manually switched to off.
Now your automation just need to check if the boolean is on or off.
Its just a condition you need to set to equal off.
Donât you still have to check context
to know when the switch is manually controlled?
It depends.
If the light switch is built into the PIR and activate the light directly then yes.
But if it is a standalone switch, then you can catch it in HA and make it do multiple actions, like set the boolean and turn on the light.
Seems like a big if. I use lutron lights for example. Touching the switch on the wall informs the integration that the state of the device changed. It then updates the state of the light entity accordingly. There is nothing to listen for with an automation besides the light changing state.
Same with my hue lights. The switch on the wall is bound directly to the lights. So I see that the lights changed but the switch tells me nothing. I could change that but I wouldnât want to. Having the switch talk to HA and HA talk to the lights slows things down drastically and means you literally canât turn the lights on or off in that room when HA is down.
Also if you do that then that means you have to lay out a complete list of every way someone could possibly manually override the lights and make sure each one is accounted for. Usually the list is longer then you think:
- Light switch on the wall
- HA UI
- Google home (?)
- Alexa (?)
- Homekit (?)
- Another voice assistant (?)
- IFTTT (?)
âŚ
IMO its much cleaner to just look at the state change event and check its context with my logic above. But to each their own, like I said, thereâs a lot of different ways to do this task.
That doesnât seem like the situation described in the first post (an automation was created to perform the function).
How do you âcatchâ when the switch is operated manually, by a user, versus automatically, by an automation, without inspecting context
?
Can you post sample code demonstrating the technique you have in mind? At the very least, the trigger that would be used.
My Hue and Friends of Hue switches still tell my that a button have been pressed, so I can react on that and set the boolean.
I thought that was the case for most of the devices, especially for standard protocol switches, like Zigbee and Zwave.
OK, now I think I understand what you mean.
Some brands of wall switches transmit an event (that is received by Home Assistant) when their rocker button is pressed/released/held. Given that capability, itâs possible to know when the switch is manually operated (as opposed to operated via an automation). However, it remains to be seen if jkhoodâs device supports this feature.
I believe the light switch in question is a GE. I watched HA while I turned on the light and it showed immediately on the dashboard. If that is what you needed to know
No, what WallyR proposed is to detect an event sent by your Z-Wave light switch when itâs manually operated. That event is separate from the its state (on or off).
Assuming your light switch supports this feature, it will report when its rocker button has been pressed and released. It would not be reported if the switchâs state is changed by an automation, only if its state is changed manually via its rocker button.
How would I find out if the switch sends an event in HA?
Assuming you are using the ZwaveJS integration, you can use Developer Tools > Events to listen for zwave_js_value_notification
events (enter the eventâs name then click Start Listening).
Press the light switchâs rocker button (on) and, if it sends events, they will be disabled on the Developer Tools > Events page.
Press the switchâs rocker button again (off) to view its event (if any).
Click Stop Listening, then copy-paste all of the information that was displayed to a file for safekeeping because you will need it to create a proper Event Trigger (for an automation).
Hi!! Thanks for sharing this idea, i think this approach is suitable for me too.
But i cant manage to implement it because i have no experience scripting.
I checked by listening to the state_changed
event that the parent_id
filed will help my need to condition mi automations as i want⌠The problem is i cannot get it to work as a condition inside the automation.
I tried to put some code in the âtemplatingâ section on âdeveloper toolsâ.
{{ trigger.light.my_entity_id }}
{{ state_changed.trigger.my_entity_id }}
{{ event.state_changed.trigger.my_entity_id }}
i get âUndefinedError: âeventâ is undefinedâ (or trigger, or state_changed). Im trying to see if i can get the context.parent_id
for a specific light change to use it. Cant get anything around events, i can only get attributes of the entities.
Can you share same of your code? or a tutorial on scripting using events?
Sorry about my english.
Thanks in advance!
I had the same problem. When my light already was on it still turned off after 2 minutes.
To solve that I took a scene snapshot of the lights status and then I could restore to that same state after 2 minutes instead of turning off the lights.
Check snapshotexample the last one on this page.
https://www.home-assistant.io/integrations/scene/
I solve this the simple way by having the motion automation set the light to something like 97%, then in the turn off part of the automation I check for this specific level. It isnât likely that a user turn-on would result in this specific light level.
Could you please share your code?