Notify Entering and/or Leaving Zone

This blueprint adds on to the official example by having an option for sending a notification when entering and leaving the specified zone.

There is one bug, and that is you have to toggle both booleans, otherwise you’ll get an error when saving the automation. I already made an issue for that: Blueprint selector Boolean needs to be toggled when creating automation · Issue #100658 · home-assistant/core · GitHub

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: Zone Enter/Leave Notification
  description: Send a notification to a device when a person enters or leaves a specific zone.
  domain: automation
  author: Sjorsa
  input:
    person_entity:
      name: Person
      selector:
        entity:
          filter:
          - domain:
            - person
          multiple: false
    zone_entity:
      name: Zone
      selector:
        entity:
          filter:
          - domain:
            - zone
          multiple: false
    notify_device:
      name: Device to notify
      selector:
        device:
          filter:
          - integration: mobile_app
          multiple: false
    notify_when_entering:
      name: Notify when entering zone
      description: Toggle this even if you want it off, otherwise you'll get an error when saving the automation. If both options are off, no notifications will be sent. You can also turn both on of course.
      selector:
        boolean:
    notify_when_leaving:
      name: Notify when leaving zone
      description: Toggle this even if you want it off, otherwise you'll get an error when saving the automation. If both options are off, no notifications will be sent. You can also turn both on of course.
      selector:
        boolean:
trigger:
  platform: state
  entity_id: !input person_entity
variables:
  zone_entity: !input zone_entity
  zone_state: '{{ states[zone_entity].name|lower }}' # Not really "state", but lowering the name works too.
  zone_name: '{{ states[zone_entity].name }}'
  person_entity: !input person_entity
  person_name: '{{ states[person_entity].name }}'
  notify_when_leaving: !input notify_when_leaving
  notify_when_entering: !input notify_when_entering
condition: []
action:
  - if:
      - condition: template
        value_template: '{{ notify_when_leaving == true and trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}'
    then:
        domain: mobile_app
        type: notify
        device_id: !input notify_device
        message: '{{ person_name }} has left {{ zone_name }}'

  - if:
      - condition: template
        value_template: '{{ notify_when_entering == true and trigger.from_state.state != zone_state and trigger.to_state.state == zone_state }}'
    then:
        domain: mobile_app
        type: notify
        device_id: !input notify_device
        message: '{{ person_name }} has entered {{ zone_name }}'
5 Likes

Is that a typo or are you saying the example you posted doesn’t work?

1 Like

No, probably the device that’s being used for testing doesn’t work.

Yes, I assumed the official one worked, but it does not for me. I tested it by spoofing my location with an app, I saw my location change on the home assistant map but I did not get a notification. When I manually execute both automations, the notification does work.

So this is the official zone blueprint example, with one tiny modification, that failed its tests.

Are you posting it here to help get it fixed?

So this is the official zone blueprint example, with one tiny modification, that failed its tests.

Yes, I figured it may be usefull for someone even less savvy than me. I assumed the official version worked, so figured that the small change shouldn’t effect break it.

Are you posting it here to help get it fixed?

No, I actually noticed it doesn’t work after I posted it (sorry about posting before testing :slightly_frowning_face:). Now I don’t know if the official one is broken in general or just broken on my end. If it is just broken on my end the zone_entering part should work for other people still.

I’m interested on this blueprint, looks like official leaving zone works for me, I will try your “entering zone” and provide feedback.

Yesterday I created 3 zones and 6 automations to notify when entering and leaving zones but didn’t received notifications.
I revised zones and I’ve seen zones are " Passive", I disabled option and today I received notifications for all, entering and leaving but only after I unlocked my phone, home assistant app was “sleeping” even if I disabled battery optimisation, maybe it’s possible to add some priority to blueprint?

So, it’s working, you can upload blueprint to github, thank you!

Thank you for confirming! I guess I have a different issue with the notifications then, at least this blueprint works :slight_smile:

I modified that same blueprint and noticed that it does seem to work for zones other than the home zone. I changed the notification to use my telegram notify and that does work but again, there seems to be something about the home zone specifically that does not trigger this automation when running from a blueprint. I’ve set up a separate automation not using the blueprint and the home zone does seem to work. It’s a bit frustrating…

2 Likes

Any idea why this would not work from entering Home? Works great for my other zones, but not my Home. Strange.

Would you mind posting your automation config? I am having the same issue with the home zone. thank you!

I found the official leaving zone blueprint doesn’t work for me. I tracked it down (thanks to the automation tracing) to the fact the state and name do not match case… trigger.from_state.state == home and states[zone.home].name == Home.

So, when the check of the state vs the zone name happen, it never matches as coming from THAT zone. It might only happen with certain device trackers (I’m using nmap for this example) or the default Home zone has some quirks.

2 Likes

The official leaving zone does work for me, but not this one for entering. Can anyone help me?

Edit: I can trigger the automation manually and that results in notifications. So the problem is that the autmation does not get triggered

Hi, it’s December 2022 as I type this. This is an old thread, but still has some value.

What I want:

A notification to my android Home Assistant app that a person arrived at a zone

How I did it:

  • add person in Home Assistant under Settings > People
  • install Android Home Assistant app in those people’s phones, enable notifications, vibrate, pop on screen, etc, enable location (of course)
  • create your target zone(s) in Settings > Area & Zones
  • open File Editor
  • navigate to /config/blueprints/automation/homeassistant
  • open notify_leaving_zone.yaml
  • copy all code from notify_leaving_zone.yaml
  • create a new file, name it notify_entering_zone.yaml
  • paste all code from notify_leaving_zone.yaml into the newly created notify_entering_zone.yaml
  • reverse the logic in line 37, from this

value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}"

to this

value_template: "{{ trigger.from_state.state != zone_state and trigger.to_state.state == zone_state }}"

  • and change the action text:
action:
  - alias: "Notify that a person has left the zone"
    domain: mobile_app
    type: notify
    device_id: !input notify_device
    message: "{{ person_name }} has left {{ zone_state }}"

to this

action:
  - alias: "Notify that a person has entered the zone"
    domain: mobile_app
    type: notify
    device_id: !input notify_device
    message: "{{ person_name }} has arrived at {{ zone_state }}"
  • Save changes
  • Navigate back to Settings > Automations & Scenes > Bluebrints
  • You should see your newly created Zone Entry Notification blueprint
  • Select “create notification” to create a new automation

Just tested (by pressing Run in the automation), it’s working. Crossing fingers that it works when the person actually enters

4 Likes

The quirk is that for all zones EXCEPT zone.home the device will report back the zone name (ie friendly name), while in zone.home the device will report back “home” rather than the friendly name, this poses some challenge to the blueprint as it needs to test for the special case zone.home being supplied as input.

I am (trying to) use this automation too and have the same problems as the posters above. I can run the automation manually and receive notifications. But when devices enter zones (even if they are not the home zone), nothing happens. When checking the logs of the autmation, I did see a trigger where

    trigger.from_state.state: not_home
    trigger.to_state.state: Remote Office

In the graphical Trace Timeline I see that the automation did reach the “send notification” step, but the notification never actually got sent.

Please help us debug this issue, so others also get this working.

I replaced the blueprint with a normal automation from scratch:

alias: Me at the Office
description: ""
trigger:
  - platform: zone
    entity_id: person.rolf
    zone: zone.office
    event: enter
condition: []
action:
  - device_id: f6f1e7XXXXXXXXXX32bf2
    domain: mobile_app
    type: notify
    message: Rolf at Work
mode: single

Of course this is easiest with the graphical editor (or paste this script in yaml and then switch back to the UI to get a headstart on what to fill in where.

I’ll post an update here if things start working.

The normal automation seems to work more reliable than the blueprint for some reason. I also note that homeassistant, the browser and the app require some restarts/logins for some changes which to me seem like trivial. That could be an improvement.

I added a custom iPhone sound to the notification in the yaml (this cannot be done in the GUI):

alias: Me at the Office
description: ""
trigger:
  - platform: zone
    entity_id: person.rolf
    zone: zone.office
    event: enter
condition: []
action:
  - device_id: f6f1e7XXXXXXXXXX32bf2
    domain: mobile_app
    type: notify
    message: Rolf at Work
    data:
        push:
           sound: 'shake.caf'
mode: single

I hope this helps others with these intermitting notification problems, although it is a bit frustrating that I did not get to why the blueprint does not work reliably.

I just updated the blueprint, you can now do entering and leaving in the same automation. I have it tested on my end and everything works.

I actually made it slightly simpler since the default one also checks extra for zone.home for some reason, which I found to not be neccesary.

There is only one bug, and that is you have to toggle both booleans, otherwise you’ll get an error when saving the automation. I already made an issue for that: Blueprint selector Boolean needs to be toggled when creating automation · Issue #100658 · home-assistant/core · GitHub