Automation for entering and leaving Zones

Hope it’s ok to piggyback on this one. I used the same code and I get notifications for:

Enter home
Enter school
Leave school

But I don’t get Leave home notifications

id: '1622919609227'
alias: Zone Activity
trigger:
  - platform: zone
    entity_id:
      - person.1
      - person.2
    zone: zone.home
    event: enter
  - platform: zone
    entity_id:
      - person.1
      - person.2
    zone: zone.home
    event: leave
  - platform: zone
    entity_id:
      - person.1
      - person.2
    zone: zone.school
    event: enter
  - platform: zone
    entity_id:
      - person.1
      - person.2
    zone: zone.school
    event: leave
action:
  - service: notify.family_devices
    data:
      message: >
        {{ trigger.from_state.attributes.friendly_name }} {{trigger.event}} {{
        trigger.zone.attributes.friendly_name }}
mode: single

I simplified the message logic for testing.

Person 1 activity here:
image

The 3:21 away activity which was Leaving Home didn’t alert me and there isn’t a trace for the automation at that time.

Logbook shows:

Person 1 was detected away
3:21:22 PM - 44 minutes ago

But there isn’t the Zone Activity was triggered… message

Ideas?

Seeing a lot of situations where this automation isn’t being triggered even though the logbook shows “Person X was detected XXX”.

Is there any further logging I can do or additional tests?

You just need to add an enter and leave trigger for each zone. Unfortunately you can’t have one trigger for multiple zones or for leave and enter event.

Is it possible to use platform.state as the trigger, person.brayden as the entity_id, and trigger.state as zone? This wouldn’t technically be triggered when someone entered or left a zone, but instead when a person’s state changed (and I think their state is the name of their current zone). If this is do-able, the benefit would be that you’d only need one automation for all persons (because you can list multiple persons as entity_ids) and for all zones (as the value of trigger.state would be whatever zone they are in).

And maybe somehow there’s a way to use the historical value of trigger.state to represent the zone they just changed from?

I’m new to this, so not only do I not know the syntax, I also don’t know if it’s even possible.

In addition to the automation in this thread I am also using an “Everyone Left” and “Someone Arrived” script that isn’t granular and instead uses a group of people to check against. It actually works more reliable than the individual person check. Sounds like that’s what you are talking about:

- id: '6534565422234'
  alias: Everyone Left Home
  description: ''
  trigger:
  - platform: state
    entity_id: group.people
    from: home
    to: not_home
  condition: []
  action:
  - device_id: 6abf55abb534c8sa376t5c0t56yh0f3r
    domain: mobile_app
    type: notify
    title: Everyone Left
    message: Everyone Left
  mode: single`
2 Likes

That’s kind of what I meant.

Like I said, I’m new to this and don’t know the language or syntax yet, but I hope the snippet of nonsense below at least conveys my idea. My thought is to have a list of hardcoded persons be the trigger*, then the payload of the message could display the name of the person(s) and the names of the zones they left and/or entered, without needing a separate automation hardcoded for entering and/or leaving each zone.

alias: 'test notify when enter or leave zone '
description: notification when person enters or leaves a zone.
trigger:
  - platform: state
    entity_id: person.john, person.jane
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - variables:
      person: '{{ trigger.state.attributes.friendly_name }}'
      to_zone: '{{ trigger.to_state.state }}'
      from_zone: '{{ trigger.from_state.state }}'
  - service: notify
      title: Location update {{ states.sensor.time.state }}
      message: >-
        {{ person }} {% if to_zone == 'not_home' %}has left {{ from_zone }}{% endif %}{% if from_zone == 'not_home' %}arrived at {{ to_zone }}{% endif %}
mode: single

*The trigger

entity_id: person.john, person.jane

acts as an “or”, right? As in: it will trigger if person.john or person.jane meet the criteria?

Hello.

Sorry for my maybe stupid question.
But do you need a tracker, or can i use my Phone.
Say, i’m almost home and i want to turn on the light in the hallway automatic
when my Phone enters the zone of my home ?
Thanks.

@KSC you rock man ! thx will be using this as well.
Maybe you can make a blueprint of your work… I think many will be using this !
Thx for the automation !

@KSC lol, theres something wrong with my automation
When leaving school, i got a message, when coming home, i didnt get a message, heres the error
Last logged: 16:33:21

Error while processing state change for person.x
Error while processing state change for person.y
Error while processing state change for person.z
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/event.py”, line 272, in _async_state_change_dispatcher
hass.async_run_hass_job(job, event)
File “/usr/src/homeassistant/homeassistant/core.py”, line 433, in async_run_hass_job
hassjob.target(*args)
File “/usr/src/homeassistant/homeassistant/components/zone/trigger.py”, line 61, in zone_automation_listener
from_match = condition.zone(hass, zone_state, from_s) if from_s else False
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 839, in zone
raise ConditionErrorMessage(“zone”, “no zone specified”)
homeassistant.exceptions.ConditionErrorMessage: In ‘zone’ condition: no zone specified

This is my automation

- alias: Zone Notification
  trigger:
  - platform: zone
    entity_id:
    - person.x
    - person.y
    - person.z
    zone: zone.thuis
    event: enter
  - platform: zone
    entity_id:
    - person.x
    - person.y
    - person.z
    zone: zone.thuis
    event: leave
  - platform: zone
    entity_id:
    - person.x
    - person.y
    zone: zone.school
    event: enter
  - platform: zone
    entity_id:
    - person.x
    - person.y
    zone: zone.school
    event: leave
  - platform: zone
    entity_id:
    - person.z
    zone: zone.z_werk
    event: enter
  - platform: zone
    entity_id:
    - person.z
    zone: zone.z_werk
    event: leave
  action:
  - service: notify.mobile_devices
    data:
      message: "{% if trigger.event == \"leave\" %}\n  {{ trigger.from_state.attributes.friendly_name\
        \ }} left {{ trigger.zone.attributes.friendly_name }}\n{% else %}\n  {{ trigger.from_state.attributes.friendly_name\
        \ }} arrived {{ trigger.zone.attributes.friendly_name }}\n{% endif %}\n"
  mode: queued

Fixed, my zones wrent set up correctly, love the automation !

Hiya

I’m trying to get this method to work; but I get what I assume is a syntax error:

alias: Son School notify
description: ''
trigger:
  - platform: zone
    entity_id: device_tracker.life360_son
    zone: zone.abc
    event: enter
  - platform: zone
    entity_id: device_tracker.life360_son
    zone: zone.abc
    event: leave
condition: []
action:
  - variables:
      event: '{{ ''left'' if trigger.event == ''leave'' else ''arrived at'' }}'
      person: '{{ trigger.to_state.attributes.friendly_name }}'
      zone: '{{ trigger.zone.attributes.friendly_name }}'
  - service: tts.cloud_say
    entity_id: media_player.googlehome7371
    data:
      message: >
        {{ person + ' ' + event ' ' + zone }}
mode: single

I get the error:

Message malformed: template value should be a string for dictionary value @ data[‘action’][1][‘data’]

This is from the automation gui’s yaml editor, so no - doodad at the top. (Which is another question of mine, as it make copy/paste hard from here regarding indents - but that’s probably for another thread!)

Thanks!!

You are using fancy quotes here:

event: '{{ ''left'' if trigger.event == ''leave'' else ''arrived at'' }}'

Use double quotes instead of two single quotes inside the curly brackets:

event: '{{ "left" if trigger.event == "leave" else "arrived at" }}'

Thanks, appreciate the help!
The problem seems to be within the call service/data, as even with removing the variables completely the error remains?

I don’t know. If you remove the variables it will give an error anyway,because then the variables are not defined.

Thanks for the great automation :slight_smile:
i have one question but can i modify the notification i receive:
actually : the notification says that : Iphone de “name” entered SAMCHA (it’s my zone home), can i have something more explicit ? For example : “name” vient de rentrer à la maison
Thanks

Awesome, and thanks for the great automation!

This implies that if the devices in the notifications group will also receive their own notifications, right?

Also the message part is missing one +

compare:

      message: >
        {{ person + ' ' + event ' ' + zone }}
      message: >
        {{ person + ' ' + event + ' ' + zone }}

That being said, I’m also having issues getting this automation to trigger.

Hi Guys,
I would like to do something similar.
The idea is, after a certain time (like 10 p.m.) check the location of people and if they are all in the house turn on a green light, if one is out of the house turn on a red light until they come back in.
This is what I did and it is working

alias: Back Home
description: “”
trigger:

  • platform: time_pattern
    minutes: /10
    condition:
  • condition: time
    after: “22:00:00”
    before: “02:00:00”
  • condition: zone
    entity_id: device_tracker.iphone_di_rosy
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.iphone_di_sofy
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.iphone_13_di_bea
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.unknown
    zone: zone.fede_home_milan
    action:
  • service: scene.turn_on
    target:
    entity_id: scene.lamp_green
    metadata: {}
    mode: single

But I do not like that the time pattern is always running, I did not find a way to start it after a certain time. And also I have to run in parallel another automation that turn a red light after 9:59PM like this:

alias: Turn ON Red Lamp
description: “”
trigger:

  • platform: time
    at: “21:59:00”
    condition: []
    action:
  • service: scene.turn_on
    target:
    entity_id: scene.lamp_red
    metadata: {}
    mode: single

Is there a way to simplify this? Thanks

Hey guys! just noticed this wasn’t working anymore. I used to use:

service: notify.mobile_app_craigss20v2
data:
  message: |
    {% if trigger.event == "leave" %}
      {{ trigger.from_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
    {% else %}
      {{ trigger.from_state.attributes.friendly_name }} arrived {{ trigger.zone.attributes.friendly_name }}
    {% endif %}

but now that call service I guess isn’t supported. Looks like you need to use Notification service. Where do i put this information so it specifies the person and the zone in question instead of a generic message?

I was also looking to setup this same automation and I think your solution is the best for what the OP was asking. I didn’t want to have to create a trigger for every zone. I find myself adding new zones all the time and I just wanted to be notified when my family enters and leaves these zones without having to update the automation. It was stated that you can’t have one trigger for multiple zones but your strategy does this perfectly. I’m surprised no one responded to you to help you figure out your syntax. Anyway, better late than never. Here is how I used you started to setup this automation:

alias: "Notify: Zone Changes"
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.laura
    id: Laura
  - platform: state
    entity_id:
      - device_tracker.robert
    id: Robert
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - variables:
      person: "{{ trigger.from_state.attributes.friendly_name }}"
      to_zone: "{{ trigger.to_state.state }}"
      from_zone: "{{ trigger.from_state.state }}"
  - choose:
      - conditions:
          - condition: trigger
            id: Laura
        sequence:
          - device_id: [DEVICE ID]
            domain: mobile_app
            type: notify
            message: >-
              {{ person }} {% if to_zone == 'not_home' %} left {{ from_zone }}{%
              endif %}{% if from_zone == 'not_home' %} arrived at {{ to_zone
              }}{% endif %}
      - conditions:
          - condition: trigger
            id: Robert
        sequence:
          - device_id: [DEVICE ID]
            domain: mobile_app
            type: notify
            message: >-
              {{ person }} {% if to_zone == 'not_home' %} left {{ from_zone }}{%
              endif %}{% if from_zone == 'not_home' %} arrived at {{ to_zone
              }}{% endif %}
mode: single

3 Likes

This is my first post and I hope it helps someone. This automation notifies with person’s name, left or arrived at, and the zone label.

alias: Notify Zones
description: ""
trigger:
  - platform: state
    entity_id: person.1, person.2, person.3
    not_from:
      - unknown
      - unavailable
    not_to:
      - unknown
      - unavailable
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.mobile
    data_template:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} {% if
        trigger.to_state.state == 'not_home' %}left {{ trigger.from_state.state
        }}{% endif %}{% if trigger.from_state.state == 'not_home' %}arrived at
        {{ trigger.to_state.state }}{% endif %}
mode: single
2 Likes