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

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') %}
1 Like

Thank you… I had a hard time to find the proper syntax for this.
It helps to use the markdown card to find what was the content of the variable.
I would not believed this works since there’s a capital letter in the username, but indeed, I confirm this works: person.Julien = person.julien.

I have a very old script. that remembers the last known location. Last known is better than not home. better make your own with a helper script.

I recently found out that the build in location is kinda flaky. So i made a script.
It uses a device_tracker AI knows the device_tracker.
It has default code for using the device_tracker and the user_id if no device_tracker is given.
And a case you can select any given device_tracker and return what ever you want. My gps coords of my cell phone and room i am according to my deco mesh.

alias: get_location
sequence:
  - variables:
      zone: unknown
      location: unknown
      room: unknown
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ device_tracker == 'glitch mobile' }}"
        sequence:
          - variables:
              zone: |
                {{ states('var.bram_lastknownlocation')}}
              location: |
                {{ states('sensor.glitch_mobile_geocoded_location')}}
              room: |
                {%if states('device_tracker.glitch_4') == 'home' %}
                   {{ state_attr('device_tracker.glitch_4','deco_device') }}
                {% else %}
                  not in a room
                {% endif %}
    default:
      - if:
          - condition: template
            value_template: "{{ (device_tracker is defined) and device_tracker is not none}}"
        then:
          - variables:
              zone: >
                {% set tracker = states.device_tracker 

                | selectattr('attributes.friendly_name', '==', device_tracker) 

                | list 

                | first %}


                {% if tracker is not none %}

                {% set zone_name = tracker.state %}

                {% set zone_entity = 'zone.' ~ zone_name %}

                {{ state_attr(zone_entity, 'friendly_name') if zone_entity in
                states else zone_name }}

                {% else %}

                Unknown device

                {% endif %}
        else:
          - variables:
              zone: >
                {% set user_id = context.user_id %} {% set matching_users =
                states.person
                  | selectattr('attributes.user_id', '==', user_id)
                  | list %}

                {% if matching_users | length > 0 %}
                  {% set person = matching_users[0] %}
                  {% set zone_name = person.state %}
                  {% set zone_entity = 'zone.' ~ zone_name %}
                  {{ state_attr(zone_entity, 'friendly_name') if zone_entity in states else zone_name }}
                {% else %}
                  Unknown user unknown device
                {% endif %}
          - variables:
              location: |
                {% if zone == "home" %}
                  Haarlem
                {% endif %}
  - variables:
      list: |
        {{ {'zone': zone, 'location':location,'room':room } }}
  - stop: finished
    response_variable: list
description: use this script when ever a location is needed.
fields:
  device_tracker:
    selector:
      text: null
    name: device_tracker
    required: false
    description: use the known device_tracker used for the query if available