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.
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 ?