Change user ID to Friendly name in Automation

I believe the failure is due to how my example refers to the Trigger State Object (a.k.a. the trigger variable).

Your automation has three triggers, the first and second are State Triggers, referring to the same input_boolean, and the third is an Event Trigger listening for mobile_app_notification_action. The following is designed to work with a State, Template, or Zone Trigger because all have a “to_state” as documented here (Templating).

trigger.to_state.context.user_id

If your automation is triggered by the Event Trigger, all references to trigger.to_state will return nothing for the simple reason that the returned object doesn’t have to_state (it has event). In other words, it should be:

trigger.event.context.user_id

However, that will fix one thing but break another. If the automation is triggered by the input_boolean, then trigger.event will be undefined.

To handle both types of triggers, the template will first need to check if trigger.platform is event.

See if this work any better (remember, you cannot test this by manually executing the automation):

          - service: notify.mobile_app_matt_s_pixel_5
            data:
              message: >-
                Garage Door Notifications have been disabled by
                {% set uid = trigger.event.context.user_id if trigger.platform == event else trigger.to_state.context.user_id %}
                {% set p = states.person | selectattr('attributes.user_id', 'eq', uid) | list %}
                {{ p[0].attributes.friendly_name if p | count == 1 else 'unknown' }}
              data:
                ttl: 0
                priority: high
5 Likes