Automation for entering and leaving Zones

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…

I’m pretty sure a lot of us can relate!

Have to admit I never use the UI. I edit the YAML files directly. The UI interface was good to get started, but I quickly moved to direct YAML editing, including the lovelace files.

Wow - completely missed this little gem! Thanks, @Burningstone!

here is the code, removed some personal info…

- id: '1609617372924'
  alias: Turn on Alarm when Craig Leaves
  description: ''
  trigger:
  - platform: zone
    entity_id: person.craig
    zone: zone.home
    event: leave
  condition: []
  action:
  - service: alarm_control_panel.alarm_arm_home
    data: {}
    entity_id: alarm_control_panel.home_alarm
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Alarm is Now On
  mode: single
- id: '1609618911422'
  alias: Turn off Alarm when Craig Arrives
  description: ''
  trigger:
  - platform: zone
    entity_id: person.craig
    zone: zone.home
    event: enter
  condition: []
  action:
  - service: alarm_control_panel.alarm_disarm
    data: {}
    entity_id: alarm_control_panel.home_alarm
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Alarm is OFF
  mode: single
- id: '1609619013116'
  alias: Coffee Machine On Weekdays 6 AM
  description: ''
  trigger:
  - platform: time
    at: '6:00:00'
  condition:
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  action:
  - type: turn_on
    device_id: ebfd43d57a492a3df2b06e2ca55b2a19
    entity_id: switch.coffee_machine
    domain: switch
  mode: single
- id: '1609619243384'
  alias: Coffee Machine On Weekends 8 AM
  description: ''
  trigger:
  - platform: time
    at: '8:00:00'
  condition:
  - condition: time
    weekday:
    - sat
    - sun
  action:
  - type: turn_on
    device_id: ebfd43d57a492a3df2b06e2ca55b2a19
    entity_id: switch.coffee_machine
    domain: switch
  mode: single
- id: '1609620474852'
  alias: Turn off Alarm Richelle Arrives
  description: ''
  trigger:
  - platform: zone
    entity_id: person.richelle
    zone: zone.home
    event: enter
  condition: []
  action:
  - service: alarm_control_panel.alarm_disarm
    data: {}
    entity_id: alarm_control_panel.home_alarm
  - service: light.turn_on
    data: {}
    entity_id: light.exterior_back_yard_lights
  - service: light.turn_on
    entity_id: light.exterior_front_door_lights
  mode: single
- id: '1609653705623'
  alias: Alarm On Nightly 11:30 PM
  description: ''
  trigger:
  - platform: time
    at: '23:30:00'
  condition: []
  action:
  - service: alarm_control_panel.alarm_arm_night
    data: {}
    entity_id: alarm_control_panel.home_alarm
  mode: single
- id: '1609666069205'
  alias: Alarm Off 5 AM
  description: ''
  trigger:
  - platform: time
    at: '5:00:00'
  condition: []
  action:
  - service: alarm_control_panel.alarm_disarm
    data: {}
    entity_id: alarm_control_panel.home_alarm
  mode: single
- id: '1609724097915'
  alias: Brayden Lights off at 12:30
  description: ''
  trigger:
  - platform: time
    at: 00:30:00
  condition:
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - sun
  action:
  - type: turn_off
    device_id: 9e071faf21145fcb246a611811a5be81
    entity_id: light.braydens_light
    domain: light
  - service: notify.mobile_app_craigs_s20_mobile_app
    data:
      title: Notification
      message: Braydens Lights are OFF
  mode: single
- id: '1609734649963'
  alias: Jord gets to Centennial Notification
  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 has Arrived at Centennial
  mode: single
  alias:
  trigger:
  - platform: zone
    entity_id: 
      - person.jordyn
      - person.brayden
      - person.richelle
      - person.craig
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: 
      - person.jordyn
      - person.brayden
      - person.richelle
    zone: zone.home
    event: leave
  - platform: zone
    entity_id: 
      - person.jordyn
      - person.brayden
      - person.richelle
    zone: zone.centennial
    event: enter
  - platform: zone
    entity_id: 
      - person.jordyn
      - person.brayden
      - person.richelle
    zone: zone.centennial
    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 %}

You are missing a dash in front of alias in the last automation.

1 Like

added a dash, then gave it a name, no errors and config passed check, will see what happens when my kids leave school… thanks guys!

1 Like

hey guys, i did get a couple notifications, going to keep testing… quick question on syntax. i am trying to add another zone but it is work richelle and it didn’t like that. should i rename the zone to a single word?

If it’s “work richelle” it won’t work. Just make it “work_richelle”

They have to be one contiguous name - using underscores, not hyphens.

Glad it’s starting to work right!