Automation for entering and leaving Zones

hey guys, love the software and forum. I have setup a few zones, my work, home, my wife’s work, and the kids school.

i figured out how to get notifications on the app when anyone leaves or enters a zone. the problem is i have to write an automation for person 1 enters zone 1, person 1 leave zone 1, person 1 enters zone 2, etc. How do set it up so i get a notification when anyone comes or goes from a zone all in one automation?

any help would be much appreciated.

1 Like

Add them all as triggers to the one automation.

Show a couple of your automations and we can show you how to combine them.

hey thanks Tom… here are a few of them… sorry i was on my phone so formatting may be weird.
edit: fixed the code block.

daughter arrives school
- id: '1609734649963'
  alias: Jord Arrives at Centennial
  description: ''
  trigger:
  - platform: zone
    entity_id: person.jordyn
    zone: zone.centennial
    event: enter
  condition: []
  action:
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Jord Arrived at Centennial
  - service: notify.mobile_app_richelles_s10_mobile_app
    data:
      title: Notification
      message: Jord Arrived at Centennial
  mode: single

daughter leaves school
- id: '1609854795066'
  alias: Jord Leaves Centennial
  description: ''
  trigger:
  - platform: zone
    entity_id: person.jordyn
    zone: zone.centennial
    event: leave
  condition: []
  action:
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Jord Left Centennial
  - service: notify.mobile_app_richelles_s10_mobile_app
    data:
      title: Notification
      message: Jord Left Centennial
  mode: single

son leaves school
- id: '1609865230284'
  alias: Brayden Left  Centennial
  description: ''
  trigger:
  - platform: zone
    entity_id: person.brayden
    zone: zone.centennial
    event: leave
  condition: []
  action:
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Brayden Left Centennial
  - service: notify.mobile_app_richelles_s10_mobile_app
    data:
      title: Notification
      message: Brayden Left Centennial
  mode: single

son arrives at school:
- id: '1609813788530'
  alias: Brayden Arrives  Centennial
  description: ''
  trigger:
  - platform: zone
    entity_id: person.brayden
    zone: zone.centennial
    event: enter
  condition: []
  action:
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Brayden Arrived at Centennial
  - service: notify.mobile_app_richelles_s10_mobile_app
    data:
      title: Notification
      message: Brayden Arrived at Centennial
  mode: single

Try this:

- alias: notify on zone enter/leave
  trigger:
    - platform: zone
      entity_id:
         - person.brayden
         - person.jordyn
      zone: zone.centennial
      event: enter
    - platform: zone
      entity_id:
         - person.brayden
         - person.jordyn
      zone: zone.centennial
      event: leave
  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: notify.mobile_app_craigs_s20_mobile_app
      data:
        message: >
          {{ person + ' ' + event ' ' + zone }}
    - service: notify.mobile_app_richelles_s10_mobile_app
      data:
        message: >
          {{ person + ' ' + event ' ' + zone }}

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.
Edit:
And please format your code correctly. Read the community guidelines here, point 11.

6 Likes

hey man, thanks so much for helping as i am in a little over my head right now, will try when i get home. do i enter these just in the automations.yaml file or try to do in the UI? also, if i wanted to add more people, just put them under person.jordyn? what about adding a few more zones? thanks again Burningstone, appreciate you taking your time to help.

Yes, exactly.

Add it to automations.yaml.

I edited my post and added an explanation how to add more zones.

thanks so much, can’t wait to try and get rid of about 20 automations, lol…

edit… just re-read your response… sorry about the code, i was on my cell phone. not 100% sure i understand about adding triggers for the zones, will have to do some reading…

Hi, @xman111 - here’s a possibility to consolidate things a bit.

First, create a notification group - then all you need to do is notify the group instead of each person individually. Place the following in your configuration.yaml under notify: (or in your notifications.yaml file if you are using that) and, unfortunately, there isn’t a reload for notifications so you have to re-start (unless someone else knows an easier method):

- platform: group
  name: family_devices
  services:
  - service: mobile_app_richelles_s10_mobile_app
  - service: mobile_app_craigs_s20_mobile_app

Ref for notify groups: Notify Group - Home Assistant (home-assistant.io)

Then, in the automation, structure it like this:

- alias: Zone Notifications
  trigger:
  - platform: zone
    entity_id: 
      - device_tracker.person_one
      - device_tracker.person_two
      - device_tracker.person_three
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: 
      - device_tracker.person_one
      - device_tracker.person_two
      - device_tracker.person_three
    zone: zone.home
    event: leave
  - platform: zone
    entity_id: 
      - device_tracker.person_one
      - device_tracker.person_two
      - device_tracker.person_three
    zone: zone.school
    event: enter
  - platform: zone
    entity_id: 
      - device_tracker.person_one
      - device_tracker.person_two
      - device_tracker.person_three
    zone: zone.school
    event: leave

  action:
  - service: notify.family_devices
    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 }} entered {{ trigger.zone.attributes.friendly_name }}
        {% endif %}

I used two “pairings” in the automation - one for home and one for school. You can copy/paste those to add more zones to the automation. I can’t find where these can be combined together into something like this that simplifies it even further (maybe a future update will address it):

zone:
  - zone.home
  - zone.school

The action section is then the service call to the group you set up in the notify: section of your config file. The template in the data section simply pulls the info from the trigger.event and the trigger.entity_id. Note that the template is generic and shouldn’t need to change when you had more zones to the trigger section.

Coming clean: I wasn’t able to test this completely, but it does pass the configuration check… :crossed_fingers:

Let me know if you run into issues!

5 Likes

thanks KSC for typing all that out… you guys are awesome… i can’t wait to get home to try it out. I will just backup my config before i mess with everything. i really appreciate it sir!

Yea, I finally set up an automatic backup for all my config files… I’m embarrassed to say how many times I’ve had to recreate things because I didn’t have a recent backup…

1 Like

yes, it’s quite a bit of work to recreate everything. that is one thing i couldn’t believe that you had to do in smartthings, was happy to see it available in Home Assistant… i have about 15 backups, before Zwave, before Caseta, etc… lol…

i was just thinking about this and have another question. In my arrives home automation, i have it turn on the front lights, turn off our alarm, then notify me and my wife. Can that be done with this as well or should the notify just be by itself and do the alarm and lights in a separate automation?

Definitely it can be done, and is the way I’d do it. Just add the other services to the action portion of the automation. For example:

  action:
  - service: notify.family_devices
    data:
      title: Notification
      message: >
        {% if trigger.event == "leave" %}
          {{ trigger.from_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.from_state.attributes.friendly_name }} entered {{ trigger.zone.attributes.friendly_name }}
        {% endif %}
  - service: light.turn_on
    entity_id: light.front_lights
    # You can add attributes, such as brightness and transitions here, too. 

You can make it a little smarter by adding logic to determine if the sun is up or down with a service_template (make sure you have the sun component in your configuration.yaml file:

  - service_template: >
      {% if is_state('sun.sun','below_horizon') %} light.turn_on
      {% else %}                                   light.turn_off
    entity_id: light.front_lights

thanks again, can’t tell you how much I appreciate this. tbe wife and kids are annoyed at how much time I am spending on this, lol. can this all be done in the UI or don’t bother and just straight in the yaml files?

put it all in and try to execute it and nothing happens. i will keep playing around with it.

You can’t execute it, you need to enter/leave a zone. When you trigger it manually, it will skip all conditions and triggers and go directly to the action part and the action part doesn’t work because it uses data from the trigger, which is not available when you trigger it manually.
To test zones without leaving your home, you can use the device_tracker.see service.

2 Likes

thanks man. gives me hope, what a great community. thanks for going easy on me, lol.

1 Like

hey guys, tried again and i get a duplicate mapping key error in the alias line in the automations.yaml.

it say duplicated mapping key at line 156, column 3:
alias: Test

just going to google the error.

You have a parameter listed more than once. If you post your automation, we can help figure it out. Sometimes it’s not the exact line, but one near it (depending on whether or not there’s a syntax error in there somewhere)

Yea, that’s why I couldn’t fully test what I posted…

thanks KSC will post the code…