Automation for entering and leaving Zones

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!

ok thanks bro. I made them like that so the button on the app if I look at my wife’s status says work but on the map we can distinguish between my work and my wifes work. thanks again KSC.

No problem! I actually gleaned some ideas on how to better use tracking. I haven’t messed with that part of my automation for a long time, and it’s out of date. You’ve given me some thoughts on how to improve my own!

1 Like

that is great to hear.

tried it yesterday and today… anything i added doesn’t work, probably some syntax. for the most part it’s working. One thing that was weird i was watching the app while my parents drove the kids to school… both turned to the school zone at the same time but i only got a notification for one of them. Will have to keep doing some testing.

If you can post what doesn’t work, we can try to find the problem - more eyes, the better!

That may be because the automation was already running - a relatively recent automation feature (and a very welcome one) is the mode option - it can be used to prevent this. Just add the following before the action: line (and with the exact same indenting):

mode: queued

Then the one that tried to trigger the automation a half a second later will be queued to run after the first one finishes (here’s the doc on that: Automation Modes - Home Assistant (home-assistant.io). Note that single is the default. Assuming there’s an error in the logs stating the automation is “already running.”

thanks KSC you’re a rock star… i will see what happens today to make sure i get notifications for my son and repost, hopefully this will help someone down the road… happy Friday sir!

HaHa - I’m not sure my wife agrees when she can’t get a light to turn on :upside_down_face:

Happy Friday to you, too, sir!!

1 Like

hey Kirby, can you just confirm about adding before the action line? i looked through the automations file and a bunch of my automations have mode: single as the last line in the automation. want to get this right the first time so i can trigger with the kids coming from school, lol…

this is what i have right now… it’s weird i didn’t notice that id: on the last line, not sure if HA added that. this is the config that i got no notifications when my wife got to work. All the shit you did worked fine, lol.

- alias: Zone Notification
  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
    - person.craig
    zone: zone.home
    event: leave
  - platform: zone
    entity_id:
    - person.jordyn
    - person.brayden
    zone: zone.centennial
    event: enter
  - platform: zone
    entity_id:
    - person.jordyn
    - person.brayden
    zone: zone.centennial
    event: leave
  - platform: zone
    entity_id:
    - person.richelle
    zone: zone.richelles_work
    event: enter
  - platform: zone
    entity_id:
    - person.richelle
    zone: zone.richelles_work
    event: leave
  mode: queued
  action:
  - service: notify.family_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"
  id: ceb4d7d59a904f2bacd48a31fd646442
2 Likes

It can pretty much go anywhere as long as it’s at the same indent level as the action, alias, and condition statements (not the condition statement that might be inside the action statement). Apparently the UI automation editor places it at the end. Hope it works well!

The id with a unique but seemingly random number is needed for the UI editor to work. If you just edit the yaml file, you don’t need those. HA does add it if you ever use the UI editor. I think I’ve deleted most of the lines as I do all my editing now directly in the yaml file. The earlier versions of the UI editor were not feature-rich, but the folks have really ramped it up now so it has a lot more features. If you do delete those lines then you can’t edit the automation inside the UI editor, which is no big deal to me - I tend to use a lot of templates…

And hey, this is fun! :wink:

1 Like

thanks Kirby, will update on how it works… thanks again!! makes my head hurt, you guys are so smart, lol…

works perfect, thanks. when we all leave in the car together I get 4 notifications a few seconds apart. thanks.