thomasloven / lovelace-state-switch

🔹Dynamically replace lovelace cards depending on occasion
MIT License
382 stars 27 forks source link

Change state based on users current zone? #54

Closed KairuByte closed 2 years ago

KairuByte commented 3 years ago

I've been wracking my brain trying to determine if this is possible. I have a number of guest accounts/persons who use the iOS app. I am trying to change state based on the current user, and that users current location.

Is this currently possible?

thomasloven commented 2 years ago

Yes. With templates.

Mincka commented 1 month ago

Just giving a examples for anyone ending there:

type: custom:state-switch
entity: template
template: "{% if not is_state('person.' ~ user, 'home') %} user_not_home {% else %} user_at_home {% endif %}"
states:
  user_at_home:
    type: markdown
    content: You are at home.
  user_not_home:
    type: markdown
    content: You are not home.
type: custom:state-switch
entity: template
template: >-
  {% if user == 'Julien' and not is_state('person.' ~ user, 'home') %} julien_not_at_home {% elif user == 'Morgane' and not is_state('person.' ~ user, 'home') %} morgane_not_at_home {% elif user == 'Julien' %} julien_at_home {% elif user == 'Julien' %} morgane_at_home {% else %}
  unknown {% endif %}
states:
  julien_not_at_home:
    type: markdown
    content: You're Julien and you are not at home.
  morgane_not_at_home:
    type: markdown
    content: You're Morgane and you are not at home.
  julien_at_home:
    type: markdown
    content: You're Julien and you are at home.
  morgane_at_home:
    type: markdown
    content: You're Morgane and you are at home.
KairuByte commented 1 month ago

Just an FYI @Mincka , those templates are overly constrained. Here's a more flexible version:

Get current user with zone name (or away): {{ user|lower }}_{{ states('person.' ~ user)|replace(" ", "_")|lower }}

Get current user with home or not_athome: ```{{ user|lower }}{{ 'home' if is_state('person.' ~ user, 'home') else 'not_at_home' }}```

Mincka commented 1 month ago

Indeed, that's a way better template! Thank you so much for sharing!