Zone notifications

I would like to get a notification when a person enters or leaves a zone.

I tried with

alias: Notify on Zone Enter/Exit
description: Send notification when a person enters or exits a zone
triggers:
  - event_type: zone
    trigger: event
conditions: []
actions:
  - device_id: 9921fc3e7d0b46f213a71a6d0a5cfa6d
    domain: mobile_app
    type: notify
    message: |
      {% if trigger.event.data.event == "enter" %}
        {{ trigger.event.data.person }} has entered {{ trigger.event.data.zone }}
      {% elif trigger.event.data.event == "leave" %}
        {{ trigger.event.data.person }} has left {{ trigger.event.data.zone }}
      {% else %}
        {{ trigger.event.data.person }} has changed zones
      {% endif %}
    title: Cambio zona

But I get

Error rendering message: UndefinedError: 'dict object' has no attribute 'event'

The error is likely being cause by the method you are using to test the automation. The docs explain how to test automations that use the trigger variable.

Device actions are designed for new users and basic uses, they generally do not support templating. You will need to use an entity-based action (formerly known as a service call action).

AFAIK there isnā€™t a built-in event that match your triggerā€™s requirements. Are you using a custom event? If so, you will need to share the details of the action you are using to raise the event for us to be able to help. Maybe you meant to use a Zone Trigger?

I also tested by physically moving.

The trigger is working: if I put a static text in the message I get the notifications when I enter or exit a zone.

Can you download and post the json of the automationā€™s debug Trace so we can actually see the values of the trigger variable?

Hi matteocorti,

Are you able to go here:
Open your Home Assistant instance and show your event developer tools.

And see an event listener called ā€˜zoneā€™ that you can query?
IE you would likely have had to make one, I donā€™t see one on my system.

1 Like

I have done something like this in a blueprint. Feel free to use it or copy the code that would help you from itā€¦
HA_Blueprints/Automations/Person_Alert_Blueprint.md at d1c15d75a7b5fa3042d60f7eee2d7633ea8a27a3 Ā· SirGoodenough/HA_Blueprints Ā· GitHub.

If I trigger a ā€œzoneā€ event I get the notification (without any parameter as I didnā€™t specify them). If I change my state to home or not_home nothing happens (no zone event). Tomorrow Iā€™ll try to move out of the zone but I am suspecting that @Didgeridrew was correct about the wrong trigger

Thanks Iā€™ll take a look

My zone trigger is:

triggers:
  - entity_id:
      - person.me
      - person.her
    to: null
    trigger: state

Then, I set variables like:

  - variables:
      event: "{{ 'arrived at' if trigger.from_state.state == 'not_home' else 'left' }}"
      person: "{{ trigger.to_state.name.split(' ')[0] }}"
      zone: >-
        {{ trigger.to_state.state if trigger.from_state.state == 'not_home' else
        trigger.from_state.state }}
      phone_notify: "{{ 'notify.my_phone' if person == 'Her' else 'notify.her_phone' }}"
      phone_ask: "{{ 'notify.my_phone' if person == 'Me' else 'notify.her_phone' }}"

I then use those variables for a bunch of stuff.

Thanks. I managed to have something working:

alias: Notifica cambio zona
description: Manda una notifica quando una persona cambia zona
triggers:
  - trigger: state
    entity_id:
      - person.matteo_corti
conditions: []
actions:
  - device_id: 9921fc3e7d0b46f213a71a6d0a5cfa6d
    domain: mobile_app
    type: notify
    message: |
      {% if trigger.to_state.state == "not_home" %}
        {{ trigger.to_state.name.split(' ')[0] }} ĆØ partito/a da {{ trigger.from_state.state }}
      {% else %}
        {{ trigger.to_state.name.split(' ')[0] }} ĆØ arrivato/a a {{ trigger.to_state.state }}
      {% endif %}
    title: Cambio zona

If you want to trigger any time any person enters or leaves a specific zone, you would want a state trigger using the zone entity and specifying the persons attribute.

1 Like

I monitor the personā€™s state and look at the zone they move into or out of. The tricky part about that was that the zone friendly name is what is in the personā€™s state, and I had to generate a list of the zone friendly names to compare against in my blueprint, as the zones are added by the user and I couldnā€™t code in the friendly names, but in a one-off in your own set-up you know the zone friendly names, so that is easier.

I have set-up a couple of dozen zones so that when one of us are out and about, HA tells us where the other is. At work, at the hospital, at a store, etcā€¦ (voice on the google speaker)

1 Like