Sensor for Active (Current) User

I need to determine the current active user to use as a condition for some automations.
For example, customizing TTS for specific users or limiting non-admins to only some actions.
I have looked everywhere for an attribute that I could use to create a sensor template without success.
Has anyone found a need for this or figured out how to determine the “current user”.

Thanks

It exists but not in a manner that is accessible to a Template Sensor.

Let’s say you use the Lovelace UI to turn on a light and you have an automation that is triggered when the light changes state. The automation will have access to:

trigger.to_state.context.user_id

The user_id will identify who turned on the light via the UI. However, user_id is not the User account’s name but an alphanumeric string that serves as a Person identifier. You use it to search through all defined Persons and the matching one provides the name.

None of this is available to a Template Sensor.

1 Like

Fantastic! Thank you very much. Precisely what I needed and it works great.
Now I can condition my automations depending on the user:

  condition:
    - condition: template
      value_template: >
        {{trigger.to_state.context.user_id == '568843365a8c4a29b5687393515471ea' }}

Is there any way to to make the user a condition for a conditional card in Lovelace, or is the info only available when an action is initiated?

Thanks!

2 Likes

Greetings,

is this work with you. I couldn’t get it work. please give an example how to make your automation ?

I need to know who turn on or off the light or open the garage door from my user list in HA??
I will be very grateful and appreciate you.

It works great for me, but not exactly the same use case you are trying to implement.
I wanted to condition automations and scripts depending on the user executing them.
First, identify the user ID. Under Settings → People->Users. Select a user and the ID will show on top.
Mine in this case is 981443365a8c4a29b1232593515471ea
With that ID, you can do something like this example. I want Google assistant notifications if anyone other than me opens the gate.

- id: "Gate Action Notification"
  alias: Gate Action Notification
  initial_state: true
  trigger:
    - platform: state
      entity_id: switch.gate
      from: "off"
      to: "on"
  condition:
    - condition: template
      value_template: >
        {{ trigger.to_state.context.user_id != '981443365a8c4a29b1232593515471ea' }}
  action:
    - delay: '00:00:04'
    - service: tts.cloud_say
      data_template:
        cache: false
        entity_id: >-
          {% if is_state('cover.gate','open') %}
            media_player.all_assistants
          {% elif is_state('cover.gate','closed') %}
            media_player.selective_assistants
          {% endif %}
        message: >-
          {% if is_state('cover.gate','open') %}
            'The Gate is Opening'
          {% elif is_state('cover.gate','closed') %}
            'The Gate is Closing'
          {% endif %}

Hope this helps some

If each user account has a corresponding person entity, then your automation can use it to determine which user account was responsible for triggering the automation.

  action:
    - service: notify.persistent_notification
      data:
        message: >
          {% set p = states.person | selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) | list %}
          The door was opened by {{ p[0].attributes.friendly_name if p | count == 1 else 'unknown' }}

If you don’t have a person entity for each user account then the procedure is slightly more complicated. You will first have to create a list containing each user account’s user_id and name. Go to Configuration → People → Users and click on a user’s account. A popup will be displayed containing the user account’s details.

Copy the user account’s ID and Display name (or Username).

ID: aeb3c5a538d24216b3a5eba5e8375614
Display name: Frederick

Do this for each user account because you will be using all of this information to create a dictionary for a template. The ID represents the user account’s user_id.

The following example has a variable named all_users containing a dictionary with each user account’s user_id and Display name (you will create this using the values you had copied).

  action:
    - variables:
        all_users:
          'aeb3c5a538d24216b3a5eba5e8375614': Frederick
          '6c2938d872e74c84941d3c1bb3b94d0f': Louise
          '3d4c14591edb4b0bacc755772dc8bd60': Harry
          '232f173fba9d47ecb4d53d27bcf8e013': Susan
    - service: notify.persistent_notification
      data:
        message: "The door was opened by {{ all_users.get(trigger.to_state.context.user_id, 'unknown') }}"
1 Like

Dear Zoriontsu & Taras,

Thanks a lot for your support I appreciate you very much.

1 Like

is there a way to get the current user not using a trigger? I just want to include the current user into a MQTT payload when a button is pressed and a script is called.

The following doesn’t work, it will store the first user of the user list, but the current user could be some other person.

  - service: mqtt.publish
    data:
      qos: '1'
      topic_template: '{{ states.input_text.grossprojekt.state }}/{{ states.input_text.desktop01_wohneinheit.state
        }}/{{ states.input_text.desktop01_alarm.state }}/documentation/user'
      retain: true
      payload_template: '{{ states.person|selectattr("attributes.user_id")|map(attribute="attributes.friendly_name")|first
        }}'

For people that see this now, you can use (example):

 {% if is_state('person.' ~ user, 'not_home') %}