I’m trying to automate so I get a notification on my phone when someone is changing zone.
This works, but the notification template does not. It shows: person.oscar from Oscar to Oscar
Why is the friendly_name of from_state the friendly_name of the person object?
Or should I just do this: {{ trigger.from_state.name }} to {{ trigger.from_state.state }}
alias: Kids location notify
description: ""
trigger:
- platform: state
entity_id:
- person.oscar
- platform: state
entity_id:
- person.christian_bendtsen
condition:
- condition: template
value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
alias: Not the same state
action:
- service: notify.mobile_app_michael_mobile
data:
title: Kids status
message: >
{{ trigger.entity_id }} from {{ trigger.from_state.name }} to {{
trigger.to_state.name }}
mode: single
Most of the time your people are going to be either leaving or entering an undefined zone, so I would set this up a bit differently… Also, you may want to use queued or parallel mode in case they happen to be changing zones at the same time.
alias: Kids location notify
description: ""
trigger:
- platform: state
entity_id:
- person.oscar
- person.christian_bendtsen
condition:
- condition: template
value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
alias: Not the same state
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.from_state.state == 'not_home' }}"
alias: Leaving Undefined zone
sequence:
- service: notify.mobile_app_michael_mobile
data:
title: Kids status
message: >
{{ trigger.to_state.name }} arrived at {{ trigger.to_state.state }}
- conditions:
- condition: template
value_template: "{{ trigger.to_state.state == 'not_home' }}"
alias: Entering Undefined zone
sequence:
- service: notify.mobile_app_michael_mobile
data:
title: Kids status
message: >
{{ trigger.to_state.name }} left {{ trigger.from_state.state }}
default:
- service: notify.mobile_app_michael_mobile
data:
title: Kids status
message: >
{{ trigger.to_state.name }} from {{ trigger.from_state.state }} to {{
trigger.to_state.state }}
mode: single
Agree with Drew for how to solve the problem you’re having. Small suggestions for additional improvements/simplifications though:
This does the same thing:
trigger:
- platform: state
entity_id:
- person.oscar
- person.christian_bendtsen
to:
You can list multiple entities to watch in a state trigger, don’t need to list them each separately if the triggers are otherwise identical.
And if you add to: or from: without any value specified then it will only trigger if the state changed, exactly the same thing your condition is doing. The other benefit of this (besides the shorter code) is you won’t get a trace clogging up the list of traces when only an attribute of a person entity changed by doing it this way.
Your automation seems to assume everyone will go from home to not_home to some other zone. And while I’m sure that will happen a vast majority of the time, sometimes phones die. In which case they may go straight from home to some other zone without stopping in not_home which seems like it would really mess up your automation.
You may want to consider just checking the to_state.state to see what it is and notifying accordingly rather then looking at the from_state.state and assuming the pattern of travel.