How can enable a view/tab only when the current user is home?

Hello,
I’m trying to figure out a simple way to enable a view/tab when the current user is home but always on for others users?
Basically I want to enable a view/tab with cameras for our Nanny to monitor our kids sleeping, but only when she’s home. I will always be activate for my wife and I. Our Nanny will also have access to other views to control other devices in the house.
Thanks for your inputs,
Ben

As there is no permission management currently in HA, your best bet is probably to go with the custom header plugin (https://maykar.github.io/custom-header/#intro/intro)

It allows you to show/hide tabs depending on who’s logged-in.

On what would HA be shown to your Nanny?

1 Like

Thank @koying,

What i’d like to do is enable a card when the user is detected home. Something like:
{% if is_state("person.benouzao", "Home") %} true {% else %} false {% endif %}

I’m trying to play around with the custom headers exceptions templates, but not sure if that will work.

Another side question if that’s OK. In the same spirit as the above, how can I can grant user access to addons selectively? No need for conditions, just yes/no.

Thanks lot.

Hi,

I searched for a long time to find out how to do this with the current logged in user. I wanted to lock a custom button if the user wasnt at home - i.e. extra step for the garage door opener.

I couldn’t find anything so hence this post as I managed to get this working fine…

Here is the condition…

[[[ 
      for (var entity in states)
      {
        if (entity.startsWith('person.')) 
        {
          if (states[entity].attributes.user_id == user.id)
          {
            if (states[entity].state != "home")
              return true;
          }
        }
      };
      return false;  
    ]]]

And usage…

entity: null
hold_action:
  action: none
icon: 'mdi:garage-open'
icon_height: 50px
lock:
  duration: 3
  enabled: |
    [[[ 
      for (var entity in states)
      {
        if (entity.startsWith('person.')) 
        {
          if (states[entity].attributes.user_id == user.id)
          {
            if (states[entity].state != "home")
              return true;
          }
        }
      };
      return false;  
    ]]]
  unlock: hold
show_icon: true
show_name: true
styles:
  card:
    - height: 60px
    - background-color: green
  icon:
    - color: black
tap_action:
  action: call-service
  service: script.1595366441469
type: 'custom:button-card'

Thanks @KiwiLostInMelb. I tried to use the same code, but I’m having some difficulties.

Is there any pre-requisites for it to work?

With the code below, I’m just trying to display a text as the card name based on my presence. But it outputs the code as plain text:

2020-08-25 22_18_26-Overview - Home Assistant

  - title: Test
    path: test
    badges: []
    cards:
      - aspect_ratio: 50%
        entity: person.benjamin
        image: 'https://demo.home-assistant.io/stub_config/bedroom.png'
        name: |
          [[[ 
            for (var entity in states)
            {
              if (entity.startsWith('person.')) 
              {
                if (states[entity].attributes.user_id == user.id)
                {
                  if (states[entity].state != "home")
                    return 'I am Home';
                }
              }
            };
            return 'I am Away';  
          ]]]
        type: picture-entity

Any suggestion?

Thanks a lot.

Can’t you use a conditional card?

I don’t think it will work

What I need is to enable cards and/or tab (entirely), for user3 only when he is home but all the time for user1 and user2.

The condtional card doesn’t seems to allow such logic.

If logic in the card is not enough then you can always use helper(s) to get what you want.

the square brackets [[]] denotes javascript inside. Not all fields support javascript BUT you could rewrite the same logic in jinga as well.

ok, so there are a few requirements - the field must support templating. Name fields dont and conditional fields dont either BUT here is a suggestion:

Then you could use the following jinga logic (as opposed to javascript first version)

{%- for entity in states %}
{%- if (entity.entity_id | regex_search('person.') )  %}
{%- if entity.attributes.user_id == user.id %}
{%- if entity.state == 'home' %}true{%- else %}false{%- endif %}
{%- endif %}{%- endif %}{%- endfor %}

Good luck and please post your success…

EDIT:
Ok so looking back through my notes yet again - the real problem is getting the user_id - the component I used was a custom:button-card. If you use this component then you should be able to get it to work as it passes a user object.

Thanks a lot @KiwiLostInMelb for taking the time to look into this.

I should probably have used another example than name field as this was just for testing.

To be more precise, what I’m trying to achieve is to enable IP camera images for one user only when the user is presence is at home. Current cameras images are available using picture-entity type cards. So what I’m thinking to to is dynamically change the entity field of the card (from camera entity to person entity which will display selected still picture) depending on the presence of the said user.

Would you know if the entity field support templates?

    cards:
      - type: picture-entity
        name: Kids Room
        entity: {jinga code here?}
        image: 'https://demo.home-assistant.io/stub_config/bedroom.png'
        aspect_ratio: 50%

I’m also not very familiar with Jinga, would you be able to help to translate the logic below?

If (currentUser=(User1 or User2) or (CurrentUser=User3 and User3.presence=home))
	Then entity=camera.entity
Else entity=person.entity

Not sure this is the best approach to do this. Any other suggestion is welcome.

Thanks again for your help and looking into this.

where in https://www.home-assistant.io/lovelace/picture-entity/ did you read you can use templates at all for core picture-entity card?

you can use regular jinja templates in CH for that:

show_tabs: >-
  {% if is_state('input_select.mode','Developer') %}0 to 16
  {%- else -%}0 to 3, 5, 11,12, 14 to 16
  {%- endif -%}

or, use the exceptions to control even more fine-grained and still hold the show_tabs logic set in the above template:

exceptions:

  - conditions:
      user: Gezin, User1
    config:
      show_tabs: 0 to 3, 5, 11,12, 14 to 16
      voice_hide: true
      hide_refresh: true
      hide_reload_resources: true
      hide_help: true

of course you can also template the current User to a name and check if user is home, try something like this:

{{user == 'Username' and is_state('device_tracker.user','home')}}

you can even test these templates in CH using the test_template config option. Above template then shows:

in the inspector.

combining that would be like:

show_tabs: >-
  {% if user == 'Username' and is_state('device_tracker.user','home') %}0 to 16
  {%- else -%}0 to 3, 5, 11,12, 14 to 16
  {%- endif -%}

Yup, use custom header with multiple tabs and enable the tab you want if the user is at home.

@Mariusthvdb has the answer above - just use the user at home jinja to enable/disable the tab - i.e. hide the camera if not at home and display the image only tab.

custom_header:
  hide_header: false
  header_text: >-
    {% for entity in states.person %}{% if (entity.attributes.user_id == userID ) %}{% if entity.state == 'home' %}home{% else %}away{% endif %}{% endif %}{% endfor %} 
  show_tabs: >-
    {% for entity in states.person %}{% if (entity.attributes.user_id == userID ) %}{% if entity.state == 'home' %}home{% else %}away{% endif %}{% endif %}{% endfor %}
views:
  - badges: []
    path: default_view
    title: home
  - badges: []
    cards: []
    path: away
    title: away

I prefer to not use device tracker and use the person.state as it allows for multiple device trackers per person (and uses the most recently updated one).
I use a combination of home assistant app and wifi presence on the person configuration.