YAML attached. It’s a very simple thing, but my brain cannot find the simple solution.
I have a presence sensor in the kitchen. When it detects, lights on. When it stops detecting (it has a 90s clear time from Z2M), lights off.
I want to add an extra condition to the lights going off. With this automation, we NEVER use the light switch anymore to turn them ON.
The extractor hood in the kitchen is wired with the kitchen light. So when cooking, and we leave, the lights go off in 90s and the hood goes off with it.
I don’t want to add extra devices or change wiring. I have been suggested humidity sensors, temp, etc, but they didn’t work well, so I’m going back to basics with this approach.
So I want either of these 2 things, whatever is easier or more elegant.
IF light was switched on MANUALLY (from the switch, and not from this automation), delay going off for 15min. I don’t know how to differentiate the manually from the automation to do it myself.
Create a helper or whatever is needed, so when activated, it starts a timer for 15min. Then use this as a condition in the automation for the lights to go off. I would then either activate this helper with a double click in the switch, or a press and hold (not sure if possible) or if not just a button from Home Assistant itself.
This issue has been chasing me for a long while and wife is getting very annoyed.
Many thanks!
Is the switch an actual smart switch/relay or just a device that sends a signal?
For a smart switch, you can likely use the context of the state change of the switch. For a device like a smart button that just sends a signal, you will need to trigger off the entity or event that represents that device action.
Does the switch report power usage? You could always wire up a relay of whichever protocol you use (Z-Wave, Zigbee, ESPHome, etc), most that I use have a manual switch component that would allow you to know when you used the local paddle.
I have no contribution toward the solution to your question, unfortunately. I simply came here to say that sounds like a terrible wiring configuration and you should look to rectify that. OMG I would lose my mind if the ventilation turned on every time the lightswitch turned on, and vice/versa. Not to mention that such a thing isn’t even to code here in the US (though I don’t know where you are, so that may not be the case there).
In any event, if you rewired it to a much more user-friendly configuration, the solution to your problem would also become much simpler.
I use helper input Booleans for this. I.e. have the automation turn on a boolean when it turns the device on. When your time trigger goes to turn it off, if the boolean is not set it’s been turned on manually, so don’t turn off, etc.
@Swallowtail Hmmm that sounds kinda like what I want. I will double read that and try it myself.
I have done this as you suggest @Swallowtail but I’m not certain it will work great. I can see it working, but for it to work I have to manually switch on the light manually (either from HA or the switch without being picked by the presence sensor). It doesn’t feel very natural, as most scenarios will be that I went into the kitchen to cook, so the light turned on already. Now the boolean is On. I would have to manually turn down the boolean (which needs to be done from HA).
Using the context variable, start a timer when the switch is turned on manually. Just FYI, if you are using a helper to change device type of a switch to light, it is best to use the switch entity in the first automation.
Amazing help mate. Thank you very much for the time.
I understand the modified automation fully, but I would like some help to understand the first one as I’m not familiar.
I see you’re setting variables from the trigger turning on, which is the lights. Ok. You define its id, parent and user.
And you check if they are true, None, None.
Can you explain where can I see the possible values of those variables and when do they change? That will help me understand to use it in future occasions.
It would help me seeing them in UI, but it seem variables do not work outside of YAML.
All service calls, fired events and states in Home Assistant have a context object. This object allows HA to attribute changes to events, services, and users.
In this case, the value of the variables in the context object is based on the event of the light changing state. To inspect the context object of specific events you can use the Events tool in the Developer Tools menu. Use the following truth table to determine the general origin of an event:
Action
id
parent_id
user_id
Physical
Not Null
Null
Null
Automation
Not Null
Not Null
Null
UI
Not Null
Null
Not Null
Context is also observable in the Logbook. I haven’t personally tested this extensively, but generally:
Thanks for the explantation.
Whats wired is that even an automation (in node red I should say) still has user_id and parent_id “null”. And over the HA dashboard it is an Automation instead of UI.
Maybe there was a change in HA 2024.02? Or maybe it is the “Supervisor”?
So I can not differentiate between Physically and Supervisor… right?
I’d say it must be node-red or supervisor as you say. Because I’m on 2024.02 too and my light is getting turned on by automation most of the time, and that is not triggering my timer (which does activate when switching manually).
The primary reason I don’t use Node Red! I played with it but when I considered the ramifications of having all my automation on there I decided against it. I think there is real benefit to using Node Red on some things but not everything.
Got it! Its not Node Red.
If I set an automation in NR it shows as “Automation”.
But the majority of my light automations turns Philips Hue scenes on. That causes it to show as “Manual”.
Dont know (for now) if its just the Hue scenes or if it is a scene (HA) in general.
For the future googler that lands here, you don’t need a separate automation to check if the device was manually changed. The context is automatically written when the trigger occurs.
If you want to see the context, trigger the automation manually then → traces → click on the choose icon :
and click on the “changed variables” on the right. You will see the context info buried in there under the “to_state” info:
If you’re using “choose” actions you can just add a “template” condition and enter this code:
The Template Condition you have suggested will always pass because it always reports boolean true. It’s equivalent to doing this, which also reports boolean true.
{{ 1 + 1 == 2 }}
The id, parent and user variables in your template don’t reference the trigger object that is generated when the automation is triggered. Therefore they’re completely unrelated to any actual triggering activity.
The Solution post of this topic shows how to properly reference a trigger object’s context properties:
Actually, my current automation using this is failing often and I’m unsure why.
I want a timer to start only when the Kitchen lights is switched on manually, either button or via HA dashboard. Not when it’s been triggered via an automation (ie. motion sensor).
But I’m finding the timer is getting started sometimes when the motion sensor has switched on the lights, and I guess I made some mistake.
Why do you put the variables statement inside the trigger and repeat it 2x at that. This might work but I would worry that the context of the variables may only be within the trigger.
Pull those out and add a global variable key with those same variables designated. Then they will work in the actions as intended without a question.
Variables put in global are executed after trigger and before conditions.