How to show name of user who triggered event?

alias: Shopping list - Notify when new item added
trigger:
  - platform: event
    event_type: shopping_list_updated
    event_data:
      action: add
action:
  - service: notify.notify
    data:
      message: >
        {{ trigger.context.user_id.to_friendlyname() }} added item "{{ trigger.event.data.item.name }}" to shopping list.
      title: New item added to shopping list
mode: single

See the above. I have added the pseudo to_friendlyname() function.
But of course that does not work.

What I need is a way to translate the user_id in to the users real name, so the notification would look like:

John added item “Bananas” to shopping list.

Anyone who can help me with how to translate the user_id in to real users first name?

Search the forum for triggered_by.

1 Like

I tried that and also googled for it but do not really find any relevant answer to my question. Can you point me in the right direction perhaps?

This is from a script I use:


          {% set user_id = states.script.shutdown_remote_host.context.user_id %}
          {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list %}
          {% set first_name = "System" if not triggered_by else state_attr((triggered_by | first).entity_id, "friendly_name").split()[0] %}
          {{ first_name }} shut down {{ name }}.
2 Likes

A few references:

I was looking for the way to notify who triggered an entity state change from an automation.
With this code I get the user name when I trigger manually the automation but get System when it is triggered automatically.

Someone knows how can I get the user who did the last change over an entity?

{% set user_id = context.user_id %}
{% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list %}
{% set first_name = "System" if not triggered_by else state_attr((triggered_by | first).entity_id, "friendly_name").split()[0] %}
    
{{ first_name }} cambió bomba de pozo a estado {{ states("switch.switch_bomba_pozo") | upper}}.