Get user info for automation or script

Is it possible to get info wich user last clicked a switch or whatever to use this In an automation or script? I can see the info in logbook but have no idea how to get this. I want to do different thing depending on wich user clicked a switch for example.

Not pretty, but doable:

With a static entity_id

{% set changed_by_id = states.switch.w01.context.user_id  %}
{% set changed_by = (states.person | selectattr('attributes.user_id','==', changed_by_id))| list | first or "system" %}
Changed by: {{ "System" if changed_by == "system" else state_attr(changed_by.entity_id, "friendly_name") }} 

With using the entity that triggered the automation

{% set changed_by_id = trigger.to_state.context.user_id  %}
{% set changed_by = (states.person | selectattr('attributes.user_id','==', changed_by_id))| list | first or "system" %}
Changed by: {{ "System" if changed_by == "system" else state_attr(changed_by.entity_id, "friendly_name") }}   
3 Likes

Great now I need to check why some switch get the user info and some not

1 Like

I think context needs to be implemented on a integration, so it can be that some of your integrations does not implement it yet (I may be wrong here)