Zone triggers, status references and testing

i’m trying to use automations based on zone triggers, to post arrival and departure notifications. This code works properly:

- alias: Test trigger5
  trigger:
    platform: zone
    entity_id: device_tracker.misty
    zone: zone.home
    event: leave
  action:
    service: notify.notify
    data_template:
      message: Misty has left home

Of course, I have multiple users and multiple zones, so I would prefer to do this all en masse, and pull data from the application as to “who triggered what where” and reduce the number of lines of code.

I built this line of code off of information provided by others:

- alias: Test trigger4
  trigger:
    platform: zone
    entity_id: device_tracker.misty
    zone: zone.home
    event: leave
  action:
    service: notify.notify
    data_template:
      message: '{{ trigger.to_state.attributes.friendly_name }}'

When I trigger this automation from Service Call, I get an error:

Error rendering data template: UndefinedError: 'trigger' is undefined

I tried it manually as well, with the same results. I have also tried it with
{{ trigger.entity_id.attributes.friendly_name }}
again to no avail.

First question is, should this work with a simple service call, or is there information that is not being passed? Second, would this formatting work, or is there something else I should be doing to make this work?

UPDATE:
So, first part of the problem is, my code works when I have someone move out of the area. I just can’t trigger in from inside the app to test it. If someone can give me info on how to do that, that’d be great. :slight_smile:

Now here’s the second fun part. this block of code works:

- alias: Test trigger8
  trigger:
    - platform: zone
      entity_id: device_tracker.1,device_tracker.2,device_tracker.3,device_tracker.4
      zone: zone.home
      event: leave
    - platform: zone
      entity_id: device_tracker.1,device_tracker.2,device_tracker.3,device_tracker.4
      zone: zone.home
      event: enter
  action:
    service_template: notify.notify
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} triggered event {{ trigger.event }} (Test 8.)"

So, with this I can get an automated pull of “who did what”.

Now, what I’d like to do with this, is to take the “What” part, which would be “leave” or “enter”, and use a different message for each one.

here’s what’s NOT working:

  action:
    service_template: notify.notify
    data_template:
      message: >-
        {% set in_out = {{ trigger.event }} %}
        "{{ in_out }}"

for the set in_out part, I’ve tried state_attr(“trigger.event”) which also didn’t work.

Any suggestions as to how to set the trigger.event to equal the variable in_out ?

UPDATE: This only works for enter; leave doesn’t work. apparently Enter is the default.

For everyone who wanted one block to handle entering and leaving…

THIS WORKS

- alias: Test trigger10
  trigger:
    - platform: zone
      entity_id: device_tracker.1
      zone: zone.home
  action:
    service_template: notify.notify
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} triggered event {{ trigger.event }} (Test 10.)"

Removing the event enter/leave doesn’t break this; you can grab what happens with trigger.event.

I’m going to test further and see if this breaks with zone removed. if it doesn’t, then we can ostensibly have one small block of code that handles everything.

I still need help grabbing the value of trigger.event into a variable, if anyone can help?

UPDATE:
Removing the zone breaks the code.

maybe this is of help for you. Im using 2 ways of notifying: notification or announcements:

      - service_template: >
          script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
                 {% else %}notify_presence
                 {% endif %}
        data_template:
          trigger: Presence tracking
          entityid: '{{trigger.entity_id}}'
          tostate: '{{trigger.to_state.state}}'
          fromstate: '{{trigger.from_state.state}}'

the 2 scripts are:

  announce_presence:
    alias: 'Announce presence'
    sequence:
      - condition: template
        value_template: >
          {{tostate in ['home','not_home'] and
            fromstate in ['home','not_home'] }}
      - service: notify.ios_telefoon
        data_template:
          title: >
            {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }}  - {{ 'Home' if tostate == 'home' else 'Away'}} speech
          message: >
            {{-as_timestamp(now()) | timestamp_custom("%X") }} :
              {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} {{ ' arrived home' if is_state(entityid, 'home') else ' left home' }}.
          data:
            push:
              sound: >
                {% if entityid in 
                    ['group.daughter1','group.daughter2','group.daughter3','group.daughter4'] %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Front-Door-Closed.wav
                  {% endif %}
                {% elif entityid in ['group.wife', 'device_tracker.life360_wife'] %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Wife-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Good-Night.wav
                  {% endif %}
                {% else%}
                  {% if is_state(entityid, 'home') %}US-EN-Alexa-Husband-Is-Arriving.wav
                  {% else %}US-EN-Alexa-Good-Night.wav
                  {% endif %}
                {% endif %}

  notify_presence:
    alias: 'Notify presence'
    sequence:
      service: notify.m
      data_template:
        title: >
          {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} notification
        message: >
          {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
          {{as_timestamp(now()) | timestamp_custom("%X") }} :
          {% if tostate == 'not_home' %}
            {{-name }} left {{fromstate}}
          {% elif fromstate == 'not_home' %}
            {{-name }} arrived at {{tostate}}
          {% else %}
            {{-name }} left {{fromstate}} and arrived at {{tostate}}
          {% endif %}

@Mariusthvdb thank you :slight_smile: I’ll read through this as I can and see what I can do with it :slight_smile:

okay folks, here it is. Working code. We can use one code block with both enter and leave options, and use logic to report who did what where, instead of having two separate blocks, one for leave and one for enter. If someone triggers a leave, we get the leave message, if they trigger enter, we get the enter message. I’m using Life360 as my device_tracker platform, so I may build off of this later to include moving/driving templates.

- alias: location arrival/departure
  trigger:
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.home
      event: enter
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      event: leave
  action:
    service: notify.notify
    data_template:
      message: >
        {% if (trigger.event) == "leave" %}
          {{ trigger.to_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.to_state.attributes.friendly_name }} arrived at {{ trigger.zone.attributes.friendly_name }}
        {% endif %}
3 Likes

great coding…I believe it works for one zone (home). Is there a way modify this to work for multiple zones…home, work, school.

My goal is to announce location on an amazon echo/alexa.

could one code:

entity_id: device_tracker.1
zone:

  • zone.home
  • zone. store
    event: enter

Yes! Basically, reproduce the lines for trigger, for each zone. That’s how mine is set up and it works (I edited it for the sake of brevity). So for yours:

  trigger:
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.home
      event: enter
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.home
      event: leave
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.store
      event: enter
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.store
      event: leave
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.school
      event: enter
    - platform: zone
      entity_id: device_tracker.1, device_tracker.2
      zone: zone.school
      event: leave

it’s just the matter of adding them as pairs, one for enter, one for leave. :slight_smile:

I know this is old but is there not a way to list out multiple zones and a enter/leave in one line? I can’t seem to figure it out and get it to work so either I’m dense or it’s not possible, or both.

I was thinking this:

- alias: location arrival/departure
  trigger:
    - platform: zone
      entity_id: device_tracker.a_s_iphone_ios, device_tracker.brads_iphone_ios
      zone: zone.home, zone.work
      event: enter, leave
  action:
    service: notify.ios_brads_iphone
    data_template:
      message: >
        {% if (trigger.event) == "leave" %}
          {{ trigger.to_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.to_state.attributes.friendly_name }} arrived at {{ trigger.zone.attributes.friendly_name }}
        {% endif %}

or this:

- alias: location arrival/departure
  trigger:
    - platform: zone
      entity_id: device_tracker.a_s_iphone_ios, device_tracker.brads_iphone_ios
      zone: 
        - zone.home
        - zone.work
      event: 
        - enter
        - leave
  action:
    service: notify.ios_brads_iphone
    data_template:
      message: >
        {% if (trigger.event) == "leave" %}
          {{ trigger.to_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.to_state.attributes.friendly_name }} arrived at {{ trigger.zone.attributes.friendly_name }}
        {% endif %}

neither of which work. Just trying to learn and keep the code clean where I can.

I tried this a while ago but wasn’t able to consolidate it much.

I’m with ya on keeping the code clean. I tried getting it down to the idea of data, activities, etc, and couldn’t make it work either. I had to settle for the all users per zone, per event, and a single unified action, which still reduces the code by a lot. That said, if someone can build on what I’ve done and make it cleaner, I’m all about it :slight_smile:

old thread, but I recently wrote this (which works) after googling and editing/combining other people’s suggestions:

- alias: notify zone enter/leave
  initial_state: false
  mode: single
  variables:
    person: "{{ trigger.to_state.attributes.friendly_name }}"
    from: "{{ trigger.from_state.state }}"
    to:  "{{ trigger.to_state.state }}"
  trigger:
    platform: state
    entity_id: person.james
  condition:
    condition: template
    value_template: "{{ from != to }}"
  action:
    - service: notify.pushover
      data:
        title: >
          {{ person }}
          {% if to == 'not_home' %} left
          {% else %} arrived
          {% endif %}
        message: >
          {% if to == 'not_home' %} {{ from }}
          {% else %} {{ to }}
          {% endif %}
          at {{ states.sensor.time.state }}

It’ll work for every defined zone you have.

you can change the trigger for multiple people:

trigger:
    platform: state
    entity_id: person.1, person.2, person.3, person.4

How do you specify what device i want the notifications to go to?

Also, when i click on run, nothing happens.

I have it set to send everywhere, but you can specify particular devices. Check the docs

you would add:

data:
  target:
    - device_1
    - device_2 

That’s expected, due to your use of the trigger variables.

1 Like

sorry I am new to this, do you know how i can fix this?

not without more information, no.

Have you checked the docs?
What have you tried?
Please share your yaml.