Entity_id of logged-in user

I am using a mushroom template card to welcome users to the home assistant app, basically saying “Hi Max Muster”. And then there are diferent icons based on whether or not the user is at home or not.

This works well if the user name doesn’t have any special characters:
Hi {{user}}
and

          {% set zone_home = states('zone.home')|float(default=0) %}
          {% set person_home = is_state('person.' + user, 'home')|default(false)
          %} {% set light_home = is_state('binary_sensor.light_home',
          'on')|default(false) %} 

          {% if zone_home > 0 and person_home %}
            Schön bist du da 😊
          
         etc....

BUT! This requires the person entity_id and the username to be the same.
And as the entity_id can’t have special characters, this fails as soon as there is a space in the username for example. So nothing like “Family Mustermann” for a wall tablet or “Ann Kathrin” for a user account.

Then this fails because {{user}} == Ann Kathrin and person.Ann Kathrin doesn’t exist

Is there a way around this?

{% set person = states.person | selectattr('state', 'eq', 'home') | selectattr('name', 'eq', user) | list | first | default(None) %}
{% if person is not none %}
  Welcome Home {{ person.name }}
{% else %}
  You're out and about, have fun.
{% endif %}
1 Like

oh that’s great, thanks!

Quick question to make sure I really understand this and don’t just copy your code - is this how it’s working?
states.person gets all the person entities, then you select the ones that are home and of those the ones whose name is the same as the user’s name. If that still leaves any entities, then the person is home, otherwise not. Correct?

can you tell me what the purpose of | first is?
And is there a reason that I should know why it is “name” instead of “friendly_name”? I feel like the object has a friendly_name attribute, not name.
image

Would it make sense to only select the entity_id that matches the name and then checking if the state of that person is home or not_home or would that not be of any benefit?

states.person returns a list of all person state objects

selectattr('state', 'eq', 'home') filters that list down to only persons at home

selectattr('name', 'eq', user) filters that list down to only the persons who have the specified user_id

| list turns it into a list (from a generator)

| first takes the first item of the list

| default(None) defaults it to null or None if the list is empty.

So you end up with a variable person that is a state object or null/None that represents the person who is home, and the logged in user with some safety built in so that it doesn’t error.

1 Like

had to change is not None to != None because I kept getting the error No Test named None found
My understanding was that with None you should use is not and only with actual comparisons should you use operators.

Do you know why that is the case?

no, don’t do that, it should be

{% set person = states.person | selectattr('state', 'eq', 'home') | selectattr('name', 'eq', user) | list | first | default(None) %}
{% if person is not none %}
  Welcome Home {{ person.name }}
{% else %}
  You're out and about, have fun.
{% endif %}

typo on my part

1 Like

oh, wasn’t aware that this is case sensitive! Thanks for the explanation!
Would you mind fixing this in your initial reply as well, as I’ve marked this as the solution - then others don’t have to wonder about this the way I did :slight_smile: