Know which user triggered UI event

I want to be able to know which user was the one that e.g. changed state of input_boolean toggle and use that info in an automation.

Use case: a user “locks” some resources (e.g. lights, switches) for certain period of time or until s/he unlocks them. So when locked, only the user that locked them can change their state etc.

How would I go about that? I saw that user_id is populated in UI triggered events {... "context": {... "user_id": "d4dc8..." }} , but context is not available in automations, components, … (or is it?).

Anyone has any ideas? Using zones is not an option.

2 Likes

Really no one can help? I would be grateful for a hint!

I saw that logbook has user available in 0.112, is it now possible to get user_id in an automation?

I just figured this out today.

I want to execute some actions only if the trigger was not performed by a user, which happens when I toggle a wall switch or motion sensor for example.

condition:
    - condition: template
      value_template: >
        {{trigger.to_state.context.user_id is none}}

but you can also check for user_ids, which you can find at http://your-url/config/users.

Hopefully this is helpful for you!

6 Likes

Hi Duncan,

May I know how can your find out this value template ? {{trigger.to_state.context.user_id is none}}

I am trying to determine the user who scanned the tag in order to do further automation based on the user_id but I cannot find any related documentation.

I have tried the {{ states.state_object.context.user_id }}, but seems it only works for state objects like input_boolean … etc. Is there any way to find out which user fired an event like tag_scanned?

Pointers are welcome. Thanks!

Hi Sorry to dig up an old thread but I have the same question.

I would like to know which user turned on/off locked and unlocked something using HA. I can see it in the logs, I just don’t know how to extract it

I’m not sure if it’s possible at the moment. According to the data documentation context only contains context_id, user_id, and parent_id.

The only way I see it happening at the moment and what I’ve implemented into one of my automations and will continue to use until another solution is found, is the below.

The id you’ll insert where is says user_id_here can be found under users.

Open your Home Assistant instance and show your users.

variables:
  triggered_by: >
    {% if trigger.to_state.context.user_id == 'user_id_1' %}
      User 1
    {% elif trigger.to_state.context.user_id == 'user_id_2' %}
      User 2
    {% endif %}

Then in my notification simply use {{ triggered_by }} to output the user who triggered the action. If you have more than two users simply copy the elif line and insert it underneath User 2 as in the below.

variables:
  triggered_by: >
    {% if trigger.to_state.context.user_id == 'user_id_1' %}
      User 1
    {% elif trigger.to_state.context.user_id == 'user_id_2' %}
      User 2
    {% elif trigger.to_state.context.user_id == 'user_id_3' %}
      User 3
    {% endif %}
2 Likes

I’ve created a feature request here:

4 Likes

‘{{ trigger.to_state.context.user_id }}’

1 Like

I can’t figure out where to put these variables.

Can you give an example? Thanks

Trying to find a way to see who has triggered a script and didn’t find much info. I worked out how to do this and hope it could help someone else.

Find the user ID as mentioned above, in Settings → People → Users

Then as a value template, use the below

{{ this.context.user_id == 'your_user_id' }}
1 Like