Sometimes it would be nice to get a notification when someone enters or leaves a Zone. An example could be that a person leaves home, or a person enters school.
In Home Assistant this could be done by just creating automation based on entering/leaving Zone. But what if you have 10, 15, or even more zones you will get a notification on?
It is possible to create an automation, with some variables, to trig on every zone – and get a notification on who entered/left the zone and what zone we are talking about 🙂
The trigger is a state change for a person. If any person in Home Assistant gets a new location, this will trigger the automation. Note – the change has to be there for 10 sec to be valid, just to make sure that you don’t get spammed of many new locations.
To be sure that we do not get a lot of updates, we also have a condition to check if the old location is the same as the new location – if it is, the automation just stops.
The action is a notification sent to a mobile phone. In this example, the title for the notification includes the time when the change happened. The message will then include the name of the person who changed location, and format it to be left/arrived at the location.
Code:
alias: 'Zone notification '
description: ''
trigger:
- platform: state
entity_id: person.one, person.two, person.x
for:
hours: 0
minutes: 0
seconds: 10
condition:
- condition: template
value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
- variables:
old: '{{ trigger.from_state.state }}'
new: '{{ trigger.to_state.state }}'
person: '{{ trigger.to_state.name }}'
- device_id: 5b9311606698b3e9d2b64f15eada6464
domain: mobile_app
type: notify
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 %}'
title: Location update {{ states.sensor.time.state }}
mode: single