{% 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 }}.
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}}.