Automation goal: DRY programming (Don’t Repeat Yourself)
I want to have quite a lot zone notifications sent to my devices. But not a lot of automations. I want to keep it DRY. So I thought to put multiple “person” triggers per zone event (“enters”) and have the notification message filled with a template. I don’t want to make the “same automation” for each person individually.
My aim in human-words / pseudo-code will be : When person Jacqueline or person Ton leaves zone Den Haag, send notification with message “{{person}} leaves {{zone}}” to group.
This is my automation so far:
alias: "Notify Location: Den Haag - Leaves >> group JacqTon"
description: "Notify Location: Den Haag - Leaves >> group JacqTon"
trigger:
- platform: zone
entity_id: person.jacqueline
zone: zone.den_haag
event: leave
- platform: zone
entity_id: person.ton
zone: zone.den_haag
event: leave
condition: []
action:
- service: notify.jacqton
data:
message: "{{ person_name }} leaves {{ zone_status}}"
title: "Location update {{ person_name }} leaves {{ zone_status}}"
mode: single
Output of automation: “leaves” Wished for output: “Ton leaves Den Haag” or “Jacqueline leaves Den Haag” - depending on who triggered
Problem:
The notification is sent, but the message shows “leaves” , it doesn’t resolve the templates {{ person_name }} and {{ zone_status}}
I guess because the automation does not know which one to take
in the conditions part I cannot ( or don’t know how to) select “if a certain person has triggerd”
Questions:
What template attributes can I use to get this information? In pseudo-code something like “triggered.person_name” or “triggered.zone_status” or “this.person_name”
Or is my approach bassically worng? What would be a better approach?
You can list entity IDs, but you need a trigger for each zone/event pair that you want to track. You can have both “leave” and “enter” triggers in the same automation by templating the event type in your message as well.
At first I made the mistake of copying your code to the “ACTION”-yaml part. It throwed errors.
Then I realised I had to copy your code at the complete automation yaml - level with the 3-dots in the right corner.
It works but the automation return the entire object with all of its attributes. How do I use just the attributes I want (like zone.friendly_name) in the {{trigger.zone}}.
{{trigger.zone.attributes.friendly_name}} does not work.
Can you please give me an extra hint @Didgeridrew ?
Sorry about that. I should have written {{ trigger.zone.name }}. The name property should return the friendly name as long as one has been assigned, otherwise it returns the entity ID.
In your original posts you asked about other approaches to keep your configuration DRY… you could use a single State trigger with a Choose action instead of setting up 2 Zone triggers for each zone. By setting appropriate to: and/or from: values you can limit which zones trigger the notifications.
The honest answer is I wasn’t aware that event was an optional field, and I’ve never tested using it that way.
If that is the case, then we need to get some congruity between the docs, UI editor, and actual function. For example, the Automation Editor requires exactly 1 event to be chosen. It does not supply a checkbox to enable/disable the field, or allow both to be chosen. If you edit the YAML to remove the event field, the UI editor posts an error in the trigger description:
hmm, maybe you’re right, I didnt check the ui editor, but made it in yaml, and have seen no config errors.
havent left the home yet either, so cant tell if its working as expected.
also, I posted an honest question
so you’re answer might be the answer to that.
it would be just like with:
- id: sun_sets_outside_motion_sensors
trigger:
- platform: sun
event: sunrise
- platform: sun
event: sunset
action:
service: >
switch.turn_{{'off' if trigger.event == 'sunrise' else 'on'}}
but there, the config checker fails on:
Automation with ID 'sun_sets_outside_motion_sensors' failed to setup triggers and has been disabled: required key not provided @ data['event']. Got None
trigger:
- platform: zone
zone: zone.home
entity_id: person.marijn
event:
- leave
- enter
a try, but that also doesnt pass config checker:
Automation with ID 'sync_bureau_marijn_person_tracker' failed to setup triggers and has been disabled: not a valid value for dictionary value @ data['event']. Got None