Automation: how to retreive triggered entity and zone information using multiple triggers in one automation (DRY-programming)

Hi,

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.

alias: "Notify Location: Den Haag - Leaves >> group JacqTon"
description: "Notify Location: Den Haag - Leaves >> group JacqTon"
trigger:
  - platform: zone
    entity_id: 
      - person.jacqueline
      - person.ton
    zone: zone.den_haag
    event: leave
  - platform: zone
    entity_id: 
      - person.jacqueline
      - person.ton
    zone: zone.den_haag
    event: enter
condition: []
action:
  - service: notify.jacqton
    data:
      message: >
        {{ trigger.to_state.name }} {{ iif(trigger.event == 'enter', 'enters', 'leaves') }} {{ trigger.zone.name }}
      title: >
        Location update {{ trigger.to_state.name }} {{ iif(trigger.event == 'enter', 'enters', 'leaves') }} {{ trigger.zone.name }}
mode: single
2 Likes

Nice! Thank you very much!

It works like a breeze!

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.

For anyone who is as stupid as me:

Right yaml level

Wrong place to copy the yaml

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.

1 Like

Looks very promising. DRY as the dessert in off-mousson season. I will certainly use it. Thank you.

sorry for bumping this, its just that I had been streamlining some automations myself and found this…

given the zone trigger events are either ‘enter’ or ‘leave’ why not just do:

trigger:
  - platform: zone
    entity_id: 
      - person.jacqueline
      - person.ton
    zone: zone.den_haag

and have the trigger.event templates do the rest of the logic?

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. :wink:

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

Me neither… :grin:

Can you post if it does work? Then we can do a coin flip for who gets to open an issue for the UI team and who gets to edit the docs…

hmm, mixed results…

  - id: sync_bureau_marijn_person_tracker
    trigger:
      - platform: zone
        zone: zone.home
        entity_id: person.marijn
#         event:
#           - leave
#           - enter
#       - platform: zone
#         zone: zone.home
#         event: enter
#         entity_id: person.marijn
    condition:
      condition: state
      entity_id: input_select.activiteit
      state: Aan de slag
    action:
      - service: >
          light.turn_{{'on' if trigger.event == 'enter' else 'off'}}
        target:
          entity_id: light.bureau_marijn_lampen
      - condition: state
        entity_id: input_boolean.notify_developing
        state: 'on'
      - service: notify.mobile_app_marijn
        data:
          message: >
            Marijn, je bureaulampen zijn {{'aan'
              if trigger.event == 'enter' else 'uit'}}gezet.

entering the zone triggers the automation correctly, turning on the lights, and notifying

leaving seems to not trigger, there’s no notification sent. the lights are turned off, but that probably is because of my main system doing so. :wink:

as a fellow in Discord mentions, there is a default for the event in the code so that explains why it does not error.

given Enter is the default, that explains what I described above

I did give:

    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

so, in the end, we can only do as you suggested…