Get currently logged in user in a template?

Is there any way in templating to figure out which user is logged into the app? I want to do some minor conditional logic based on who is logged in, as well as using card_mod to display certain elements, or display them differently, depending on which user is logged in and use in conditional cards if possible. thanks

2 Likes

I’m a bit late for this but I’m trying to figure out the same thing… odd no one seems to ever answer the question…

hass.user

It’s only accessible by cards that support JavaScript which currently means some custom cards (not standard cards).

Example:

1 Like

Now that makes sense!!! Thank you!!

Thomas Lovens browser mod integration can get this information, but you should understand the limitations:

  1. Multiple people can be logged in at the same time. Your template will have to account for that.
  2. The same person can be logged in from multiple locations.
  3. Browser mod will create a number of entities that represent each logged in endpoint. You have to always search these entities and new ones that are created.

TLDR: It’s possible with a custom integration, but it’ll be a complex template.

1 Like

Did anyone work on this? I’m curious, especially with recent login attacks.

nope

:frowning: Thanks for the quick response.

Hi

With the latest 2023.11 release, introducing conditional card based on user, is there perhaps a new/easier way to use the current user in a template?

Was hoping for {{ user }} but nope, lol

You can only get the user in the frontend. You can’t get the user in the backend. You’ll likely never be able to get the user in the backend (i.e. template entities).

1 Like

Is that a technical incapability or a a decision not to implement it for an end user implement?

I see the logged user icon is visible on the bottom left on desktop, maybe someway to replicate this inside a card perhaps?

As it was already told here:

  1. The currently logged in user is available in Frontend ONLY. The username may be acquired by using a “{{user}}” template - but only in cards which support templates.
  2. The currently logged in user is NOT available in automations, scripts, template sensors etc.

This works in automations:

{{ trigger.to_state.context.user_id }}

Not in scripts though.

Edit: seems like this works for scripts:

{{ context.user_id }}

2 Likes

that’s only if a user performs an action. You still cannot get a list of users that are logged in.

Yeah I was responding to the post above mine which said that you can’t get the user in automations or scripts… which can be quite handy

1 Like

I don’t know how well this works yet - I only built it over the last few hours and haven’t tested it completely. Might help some of you though:

Because there’s only me and one other person using my HA instance, I just wanted / needed to run certain background processes depending on who most recently logged in. This seems to achieve that (mostly).

This may not solve the original problem - i.e., it doesn’t give me the list of all logged in users (though you could probably change the code up to achieve that).

====

I first created a sensor template that pulls out the last changed browser_mod visible browser entity like this:

template:
  - sensor:
    - name: "Last Browser Visible"
      unique_id: last_browser_visible
      state: >
        {{ expand(integration_entities('browser_mod'))
          | rejectattr('state', 'in', ['unavailable'])
          | selectattr('entity_id', 'match', '.*visibility.*')
          | selectattr('state', 'in', ['visible'])
          | sort(attribute = 'last_changed', reverse=true)
          | map(attribute ='entity_id') 
          | first }}

I then created an automation that triggers when the above entity is changed

alias: Get user who just viewed home assistant
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.last_browser_visible
    id: When Last Browser Visible changes state
condition: []
action:
  - variables:
      user_id: |
        {% set entity = expand(integration_entities('browser_mod'))
             | selectattr('entity_id', 'match', '.*browser_user.*')
             | sort(attribute='last_changed', reverse=true)
             | first %}
        {{ entity.state if entity else '' }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ user_id == 'Jonathan' }}"
        sequence:
          - service: notify.persistent_notification
            metadata: {}
            data:
              message: "Option 1 Fired: Hello {{user_id}}"
      - conditions:
          - condition: template
            value_template: "{{ user_id == 'Sydney' }}"
        sequence:
          - service: notify.persistent_notification
            metadata: {}
            data:
              message: "Option 2 Fired: Hello {{user_id}}"
mode: parallel
max: 10

I know the code is kinda rough and doesn’t account for error cases, etc. But it seems to work well enough to post for others and to get feedback on it.

There does seem to be a bit of a delay when I’ve tested this code and logged in too quickly with two test users. If anyone has ideas about how I can shorten that delay up or other ideas to improve this, I’d be grateful.

Also wanted to thank @petro for giving me the idea to use browser_mod in post #4 to get this info.

1 Like