Is there a solution for {{ User }} in a template?

Hello Community.

I know, there are some topics with a similar question. But this topics are from years ago. So I ask again.

My son studies in another city. But he visit every weekend. He also use the companion app on android. On my main dashboard I have a mushroom template card for the title.

I also have a sensor which tracks the region (locality → university → “on”, elso “off”). I would be nice, when I have in the template card a possiblity to take the weather on his place correct. What I mean? I have two openweathermap instances: University and Home. When he is at the university, I want to have the weather and please from there, elso from home.

I hope I have made myself clear. :grimacing: Here is my current code of title:

type: custom:mushroom-template-card
primary: |
  {% set h = now().hour %} {% if h < 9 %}
    Guten Morgen, {{ user }}!
  {% elif h < 19 %}
    Hallo, {{ user }}!
  {% else %}
    Guten Abend, {{ user }}!
  {% endif %}
secondary: >
  {% set t = states('sensor.openweathermap_temperature') %} {% set f =
  states('sensor.openweathermap_feels_like_temperature') %} {% set w =
  states('sensor.openweathermap_condition') %} {% set map = {
    'sunny': 'sonnig',
    'cloudy': 'bewölkt',
    'rainy': 'regnerisch',
    'snowy': 'Schneefall',
    'fog': 'neblig',
    'windy': 'windig'
  } %}

  Die Temperatur in PLACE beträgt🌡 {{ t }} °C (gefühlt {{ f }} °C) und die
  Vorhersage ist {{ map.get(w, 'wechselhaft') }}.
icon: >
  {% set w = states('sensor.openweathermap_condition') %} {% if w == 'sunny' %}
  mdi:weather-sunny {% elif w == 'cloudy' %} mdi:weather-cloudy {% elif w ==
  'rainy' %} mdi:weather-rainy {% elif w == 'snowy' %} mdi:weather-snowy {% elif
  w == 'fog' %} mdi:weather-fog {% elif w == 'windy' %} mdi:weather-windy {%
  else %} mdi:weather-partly-cloudy {% endif %}
color: >
  {% set w = states('sensor.openweathermap_condition') %} {% if w == 'sunny' %}
  amber {% elif w == 'cloudy' %} blue-grey {% elif w == 'rainy' %} blue {% elif
  w == 'snowy' %} light-blue {% elif w == 'fog' %} grey {% elif w == 'windy' %}
  cyan {% else %} green {% endif %}
features_position: bottom
grid_options:
  rows: 1
  columns: 21
multiline_secondary: true
tap_action:
  action: navigate
  navigation_path: /dashboard/wetter
card_mod:
  style: |
    ha-card {
      background: #171717;
      border-color: #171717;
      border-radius: 18px;
      color: #ffffff;
    }

Is there an idea or a possiblity to set “PLACE” in secondary with a template code or something else?

Thanks for your help and your ideas. :+1::wink:

Have a nice week.

Ronny

I would probably create a boolean that returns on if his location is between (example) 10 and 15 km from home.
Then use a conditional card to change cards based on user and this boolean.
So you probably need three cards, one for you, one for your son at home and one at university.

Obviously this has the downside that he could be in a different direction the same distance.
I don’t know if you could create a large zone for university-area and how that stacks with other zones. I have never used that

First, thanks a lot for your help.

I have a helper like this. :wink: The problem for that solution is, that I have to copy the title in 3 cards. I think, it isn’t possible isn’t it? Or can I copy the code 3 times and set via yaml variables of visbility?

I am not using Mushrooms but may try to help.
Please clarify if I understood you properly:

  1. You have 2 or more “openweathermap” config entries for each zone - for YOUR home, your son’s home & the university.
  2. There are at least 2 clients - at your home & a Companion App on a mobile device of your son.
  3. There is a card containing:
  • a greeting for the current user;
  • a weather - which you want relate to the current place.

If this is correct, then you need this:

  1. Make sure that your “openweathermap” sensors’ names contain a zone’s name - like “sensor.openweathermap_university_temperature”.
  2. Make sure that you have “zone” entries named like “University” (for less complexity).
  3. Make sure that you have “person” entries named same as users (for less complexity).
  4. Make sure that the Companion App of your son provides location tracking - and thus provides a corresp. “device_tracker” entity - and thus updates a corresp. “person” entity.
  5. On a card, you need to compose an entity_id of a required sensor dynamically. Since the Mushroom card does support jinja, it is possible. Then do smth like this:
{% set ZONE = states('person.' + user) -%}
{%- set SENSOR_TEMPERATURE = 'sensor.openweathermap_' + ZONE + 'temperature' -%}
# check if there is an existing sensor since the person could be "Away" or in some other place
{%- if not has_value(SENSOR_TEMPERATURE) -%}
  ... fallback to some default place, could be different dependently on a user,
  ... you may try coding it by yourself
  {%- set SENSOR_TEMPERATURE = 'sensor.openweathermap_home_temperature' -%}
{%- endif -%}
{%- set t = states(SENSOR_TEMPERATURE) -%}
...

You can not really make a card for your son specifically. Any change to a card he cause will be seen by other users too.

What has been made possible is to hide or show cards conditionally, so if you make a home card and an university card, then you should be able to hide or show the cards for different users.

@Ildar_Gabdullin Thanks for your help. I have checked all points.

  1. → Yes, I have.
  2. → Yes, it works so.
  3. → Yes, it is, but in a different spelling → person.constantin / user Constantin
  4. → Yes it works
  5. → yesterday I tried this, but it doesn’t works correctly → {% set ZONE = states('person.' + user) -%} but I will try again.

@WallyR also thanks to you. But…

so if you make a home card and an university card

works also at title of a dashboard??? I will try it too. :wink:

Then lowercase it:

{% set ZONE = states('person.' + user|lower) -%}

I have not worked the visibility option myself.
It was introduced in 2024.6.

I use the visibilty option to hide some cards on mobile phones. I will try it and let you know, if it works or not :wink: .

Note that visibility is OK when you want to show a card conditionally.
But not the best way when you need to show SAME card with different content - i.e. like “one card for weather for home, another card for weather in university”.
If only using standard cards (which do not supports templates) - then yes, using a conditional visibility (show card 1 if … , otherwise show card 2) is the only way.
But with custom cards supporting jinja (or JS) you may use ONE card with a conditional content.
(this is mainly all about maintaining repeated code)

Correct.
That is why you need two cards and conditionally show only one of them at any one point.

If you try to do it with Jinja or JS, then HA will apply it for all users, so when the son is at university, then the father will see the university weather too.

It can not be done on a card level, but has to be done on the specific view.

Thanks to both of you. Then it probably won’t work with the dashboard title, correct?

Thanks Ronny

Disagree.
Different logged-in users will see a different card.

Chrome vs FF.

Another one:

Note that in case of a stock visibility feature when a condition is defined based on some entity - then all users will see same card because this condition is resolved on a server side, i.e. same entity has same state for all clients.

What do you mean by a “dashboard title”?
You mean this?


The only way you can modify this title dynamically is a card-mod-theme.

Also, note that a dashboard title is only shown when the dashboard is in edit mode.
May be you meant a “view title”.

This section I have ment. :wink:

I am not getting. Do you mean a label of this UI control to add a header? Or a text of an an added header?

I cann’t describe it in English better, then yet :man_facepalming:.

Dashboard → Section of the Dashboard (see the icon sun) and then on the screen, there is title again. Maybe that is only in German “Titel”. Unfortunately I don’t know how in English this is labeled.

Over all cards on screen, you have title and badges. → here I mean “title”.

No, they are the same card, but with their username inserted.
The username will only work in the views and not in a card condition.

Standard visibility conditions include a “user condition” - although in yaml it is defined in a not friendly way, but it works:

views:
  - theme: ''
    title: test_mone
    path: test_mone
    cards:
      - type: markdown
        content: hello, ildar
        visibility:
          - condition: user
            users:
              - 4c7b8d24fced4a6e863eda0e693b9ed3
      - type: markdown
        content: hello, guest
        visibility:
          - condition: user
            users:
              - cde97115e9bd47b49c0e35232aa35551

And both users will only see OWN card.

Also, the “{{user}}” templating variable only works in jinja & resolved on a client side in dashboards only, and thus you can use this variable in any card supporting templates. Every user will see OWN look.

No arguing, please. :wink: I will test it today and let you know, if it work on my dashboard.