Understood but I’m trying to build a scalable automation where I can add other members of the household. The plan was to add other members of the household to the zone leave/enter list however I can now see that isn’t going to work as I’ll need to trigger off the person state, not zone event.
If you want to combine arrive at and left home, you can try- (have not tried it myself)
trigger:
- platform: state
entity_id: person.wife
condition: []
action:
- service: notify.husband
data:
message: >-
{% if is_state('person.wife', 'not_home') %}
Wife left home
{% elif is_state('person.wife', 'home') %}
Wife arrived at home
{% else %}
Wife is located at {{ states('person.wife') }}
{% endif %}
mode: single
The above code assumes- 1. If person.wife goes to undefined zone, the states will be not_home 2. If person.wife goes to defined zone, the states will be defined zone - such as Work
A flaw exist in the logic. Please read Burningstone’s post below.
There’s a flaw in your logic.
If his wife leaves a zone other than home (e.g. work), your automation will show “Wife left home”, which is of course not true.
This will get the friendly name and in case the friendly name doesn’t exist it falls back to the entity_id, see here for more details about the state object → State Objects - Home Assistant
Try this:
trigger:
- platform: state
entity_id: person.wife
action:
- variables:
old: "{{ trigger.from_state.state }}"
new: "{{ trigger.to_state.state }}"
person: "{{ trigger.to_state.name }}"
- service: notify.husband
data:
message: >
{% if new == "not_home" and old == "home"%}
{{ person }} left home
{% elif new == "home" %}
{{ person }} arrived at home
{% elif new == "not_home" %}
{{ person }} left {{ old }}
{% else %}
{{ person }} arrived at {{ new }}
{% endif %}
mode: single
This can be extended with as many persons as you like, by adding additional triggers. This should also cover all the possible cases, arriving at home, arriving at a zone other than home, leaving home and leaving a zone other than home.
Thanks everyone for your help on this, I’ve learnt a lot. @Burningstone thanks for the revised code which I tested yesterday. The code fits my use case perfectly. One issue I have found is after wife leaves ‘work’ and prior to arriving ‘home’ I am receiving continuous messages stating ‘wife arrived at not_home’. These recur circa twice a minute thus by the time wife was home I had about 30 message notifications on the mobile app.
That’s probably the sensor getting a new GPS measurement, that is not home or in any other zone, the state gets set again to not_home, even though it is already in this state. You can add a condition to overcome this issue:
For those interested my initial problem was also caused by user error. I was using ‘run actions’ from the automations window to test the automation. This of course was leading to the error as there was no state change on the person entity. The correct way to test this automation is via ‘Developer Tools > States’ and manually changing the person entity state. Hopeful that helps others.
Thought I was driving crazy as it worked out before and then suddenly didn’t anymore. Just because I used the „run action“ instead of physically triggering a sensor.