Dynamic Notification- User Enters/Leaves Zone (Automation Trigger Variables help) [SOLVED]

I currently have several automations to notify when a persons zone is updated.
This then updates a boolean if the person enters the ‘Home Zone’ (user is home)

I’m trying to make the automations a little more dynamic…
So ONE automation that will push a notification when a user enters/leaves a zone?

A nice to have would be…

JJ left ZoneA after 1hour 42Minutes

Is this possible?

Thread Updated...

I’ve changed the thread content to be more aligned to what I’m after :smile:

Not sure how I can get this working.

Hello,

I started an integration of this style with 2 people leaving a defined area.
The message is dynamic.

- id: '1580114122019'
  alias: Zone - Entrée
  description: ''
  trigger:
  - entity_id: person.joan
    event: enter
    platform: zone
    zone: zone.home
  - entity_id: person.jessica
    event: enter
    platform: zone
    zone: zone.home
  - platform: zone
    entity_id: person.joan
    zone: zone.ecole
    event: enter
  - platform: zone
    entity_id: person.jessica
    zone: zone.ecole
    event: enter
  condition: []
  action:
  - data:
      message: '{{ trigger.to_state.attributes.friendly_name }} est entré dans la
        zone {{ trigger.to_state.state }} à {{ now().strftime("%H:%M") }}'
      title: Entrée de zone
    service: notify.push
  mode: single

In my case, the message is dynamically composed with the name of the trigger as well as the name of the zone detected.

I hope I partially answer your question …

1 Like

Just bumping this thread as I’m looking to revisit this.
I’m hoping to have a dynamic automation whereby I can have multiple people as the trigger.
If a person’s zone changes, a notification is sent similar to below

JJ left ZoneA after 1hour 42Minutes

Hey,

Yep, this totally answered my question…

I did a slight update so I didn’t have to add each zone…
This way, if the attribute changes, the automation runs…

alias: '[USER ZONE NOTIFICATION] New Dynamic Rule'
description: ''
trigger:
  - platform: state
    entity_id: device_tracker.USER_A
    attribute: address
  - platform: state
    entity_id: device_tracker.USER_B
    attribute: address
  - platform: state
    entity_id: device_tracker.USER_C
    attribute: address
condition: []
action:
  - data:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} has entered zone {{
        trigger.to_state.state }} at {{ now().strftime("%H:%M") }}
      title: NEW Zone Notify
    service: notify.mobile_app
mode: single

The only thing I can’t figure out how to do, is say how long the user was in that zone for… only when the zone changes.

So the above doesn’t work… keep getting notifications even though nobody has changed zones

EDIT:
Had to put several conditions in… (struggling here haha!)

  1. Now triggers on the ‘at_loc_since’
  2. Had to put a ‘not’ condition as it continuously triggered when the tracker was on the move (not_home)
alias: '[USER ZONE NOTIFICATION] New Dynamic Rule'
description: ''
trigger:
  - platform: state
    entity_id: device_tracker.USER_A
    attribute: at_loc_since
  - platform: state
    entity_id: device_tracker.USER_B
    attribute: at_loc_since
  - platform: state
    entity_id: device_tracker.USER_C
    attribute: at_loc_since
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: device_tracker.USER_A
        state: not_home
  - condition: not
    conditions:
      - condition: state
        entity_id: device_tracker.USER_B
        state: not_home
  - condition: not
    conditions:
      - condition: state
        entity_id: device_tracker.USER_C
        state: not_home
action:
  - data:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} has entered zone {{
        trigger.to_state.state }} at {{ now().strftime("%H:%M") }}
      title: NEW Zone Notify
    service: notify.mobile_app
mode: single

Does anybody have any other solution?

Finally settled with this…

alias: '[USER ZONE NOTIFICATION] New Dynamic Rule (v2)'
description: ''
trigger:
  - platform: state
    entity_id: person.A
  - platform: state
    entity_id: person.B
  - platform: state
    entity_id: person.C
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - data:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} {% if
        trigger.to_state.state == 'not_home' %}has left {{
        trigger.from_state.state }}{% endif %}{% if trigger.from_state.state ==
        'not_home' %}arrived at {{ trigger.to_state.state }}{% endif %} at {{
        now().strftime("%H:%M") }}
      title: Zone Notify ({{ trigger.to_state.attributes.friendly_name }})
    service: notify.mobile_app
mode: single

I’m a HA noob and I’m scratching my head trying to understand whether the code in red should be adjusted or not?

I understood that I have to replace the person.A etc…
But is there something else to change?

1- Do I have to specify the Home zone or is the ‘not_home’ enough?
2- Does the notify mobile app send a notification to all people? If I leave Home zone, would HA notify me on my phone? I don’t want that.

Thanks!

  1. not_home will show as ‘away’ on the notification, also helps with whether someone has arrived or left.

  2. You can define which device you want to notify but will need one for each device. If you specify your mobile, that will get the notification.

Does this help?

1 Like

Thanks. So I need to setup two automations, one for each device if I don’t want people to get notifications about themselves leaving/entering home?
I was hoping I could do this in one automation to reduce the overall number of automations, but if it’s not possible then I’ll go with that.

I’m guessing under action I should add the following?
- device_id: *id of the device that is supposed to receive the notification*

That’s right, each automation will require each device to receive the notification (service: notify.xxx)

I’m sure once you’re a little familiar you could add a clause to only trigger if:
A: entering/leaving a specific zone
Or
B: ignore if entering/leaving home

1 Like