For now, I’m stumped. HA will change the state of the wife and I, when we are home or away -
I’m trying to get a couple of automations run upon home or away detection. -
trigger:
- platform: state
entity_id: person.chris
id: chris
from: home
to: away
condition:
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- service: switch.turn_off
target:
device_id: 645a96c59cd945bd9d8f20df1be6f8df
mode: single
However, it did not run.
The code for the person, as defined in HA -
Once again ardy saved my bacon. I was just looking at that, the not_home versus away, come back and you replied! Teach me to look a little harder at dev tools. ^^
This was done thru the automation ui, is it important that the home and not_home be in quotes in the VSC yaml version, as I read somewhere before?
I’ll watch and see if that works tomorrow. Thanks!
OK, this did not work. Not sure what I’m doing wrong, still. State changed correctly, however the automation did not fire.
alias: Porch light warren gets home
description: Porch light on us arrival
trigger:
- platform: state
entity_id: person.sandman
from: not_home
to: home
condition:
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- type: turn_on
device_id: 645a96c59cd945bd9d8f20df1be6f8df
entity_id: switch.front_porch_light
domain: switch
mode: single
The switch is a Zwave switch, fully controllable by HA correctly.
If I have another automation working with the switch, does that interfere?
This is also running, it works correctly on door opening, obviously too late to be useful…
alias: Porch light on at door opening-sun condition
description: Porch light on at door opening-sun condition
trigger:
- type: opened
platform: device
device_id: 7b9635a7847f5b99d24b0c381549d2b5
entity_id: binary_sensor.front_door_sensor_ias_zone
domain: binary_sensor
condition:
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- type: turn_on
device_id: 645a96c59cd945bd9d8f20df1be6f8df
entity_id: switch.front_porch_light
domain: switch
mode: single
These two are currently running together, I’m posting this to see if its an interference thing.
On top of that, if I set my state in developers tools to not_home, then refresh, wait maybe 30 seconds, then change state back to home, then refresh again, shouldn’t that fire off the automation?
Thanks!
You also control the sun condition with a template that will operate it with degrees of elevation. Gives you you ability to have the light come on before sunset, when it may be starting to get dark.\
Good catch, Ardy… didn’t even notice that. No errors in the VSC editor either
Think I’ll shorten up the other 2 automations to that code as well. Less clutter the better
Based in that, just quick-checked other conditions in the autom yaml file. The rest are on par.
Kanga, I’m working up to that level, going to take a while until I get a better hang of this. If nothing else, I’m determined to learn a lot more and branch out.
Thanks for both of your inputs!
Here is a working config we’ve used, I’m sure it isn’t 100% perfect because it doesn’t cover all states, triggers and conditions.
However the light always comes on when one of us gets home in the dark and I’ve never seen it turn on during day.
Main difference I can see is I don’t set the from state for person and the sun entity is checking for below horizon instead of sunset / sunrise (the sunset/rise logic is confusing to me and never works as expected) below horizon just worked and I’ve stuck with that.
alias: Arriving Home (Night Time)
description: ''
trigger:
- platform: state
entity_id: person.x
to: home
- platform: state
entity_id: person.y
to: home
condition:
- condition: state
entity_id: sun.sun
state: below_horizon
action:
- type: turn_on
device_id: xyz
entity_id: light.hallway_ceiling
domain: light
brightness_pct: 100
- wait_for_trigger:
- type: not_occupied
platform: device
device_id: abc
entity_id: binary_sensor.hallway_sensor_occupancy
domain: binary_sensor
for:
hours: 0
minutes: 0
seconds: 30
continue_on_timeout: false
- type: turn_off
device_id: xyz
entity_id: light.hallway_ceiling
domain: light
mode: single
Extra bit of config is the motion sensor that waits until motion stops in the hallway and then turns the light off after 30 seconds.
As noted above, I’d avoid the from part in the trigger. This is only useful, if you really want to check specifically for that state change. Normally it’d be better to just use the state you want to react on, in your case to: home. If the sensor has any kind of problem and reports something else than not_home (eg unknown or unavailable) your trigger will not fire. And for this use case you only want to check, if someone is getting home.
Next thing, I wouldn’t work with device_ids. They complicate things more than necessary, at least at the moment. If you ever change a device, you’d need to change all device_ids, in all scripts, automations and so on, whereas you’d only need to change the entity_id for a new device. There are ways to do this automatically, but for now they are not really needed and complicate things.
To debug your current setting, use the automation history (I don’t know the correct name for it, sorry) and see, where things get sideways. You can find it here:
After running the automation it should show exactly what has been executed, and if not, why.
Btw. I’m not sure that changing the state via developer tools actually triggers an automation. I’d try to really change the state (eg. if you have your presence detection via a phone, turn it off and on).
Will do. I didn’t like that device id thing either, it already happened : switching out a like device for another. Then I had to go in and tweak some automations.
I had a multipurpose sensor going crazy, main body mounted on doorframe, magnet on door, and all day the accelerometer was going off to on to off several times an hour in some cases. Replaced. Nothing was rattling the doorframe, or door, no obvious movements. Metal door, wood frame, shut tightly, no wiggle in the deadbolt hole (think I checked everything it could be anyway). Oh well.
Wanted to follow up, so far this code is working pretty good. Haven’t totally cleaned it up yet, that’s dependent on time
alias: Porch light we get home-after sunset
description: Porch light on us arrival-after sunset
trigger:
- platform: state
entity_id: person.sandman
to: home
- platform: state
entity_id: person.chris
to: home
condition:
- condition: state
entity_id: sun.sun
state: below_horizon
action:
- type: turn_on
device_id: 645a96c59cd945bd9d8f20df1be6f8df
entity_id: switch.front_porch_light
domain: switch
mode: single
This works when as soon as we pull in the driveway, during sun below horizon time, the porch light will kick on. Done mostly for the boss, sometimes a good source of motivation to get those automations done