Entity states does not resolve properly in a template used in an automation

Hi all,

Running home assistant on VM (esxi) for over 4 years now:
Version - core-2024.12.2
Supervisor - 2024.11.4
Installation Type - Home Assistant OS (14.0)
Frontend - 20241127.7

I have an issue with a relatively simple template.

My specific goal:

=> to get a notification on iphones when someone pushes the gatebutton (opening/closing the gate), listing who pressed the button, except if a certain user is doing this (simple if then else logic)

For this i have made one automation, making use of a template that I made from several posts on the forums here. The part of the automation that sends the notification is below:

metadata: {}
data:
  message: >-
    {% set user_id = states.input_button.poorttogglebutton.context.user_id %}             
    {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list %} 
    {% set first_name = "System" if not triggered_by else state_attr((triggered_by |
    first).entity_id, 'friendly_name').split()[0] %}  
    {% if is_state('first_name', 'Ben') %}
      de poort wordt opengedaan!
    {% else %}
      {{ first_name }} heeft de poort opengedaan!
    {% endif %}
enabled: true
action: notify.mobile_app_iben2024

The problem:

When the user ‘Ben’ presses the poorttogglebutton, I still get this message below insted of “de poort wordt opengedaan!”

Ben heeft de poort opengedaan!

even though I know that

{{ first_name }} 

resolves in

Ben

As I checked this in the Developer tools template tab.

Off note and probably important to this, when I was troubleshooting and check the below in Dev tools template tab

    {% set user_id = states.input_button.poorttogglebutton.context.user_id%}             
    {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list %} 
    {% set first_name = "System" if not triggered_by else state_attr((triggered_by |
    first).entity_id, "friendly_name").split()[0] %}  
    {% if is_state('first_name', 'Ben') %}
      de poort wordt opengedaan!
    {% else %}
      {{ first_name }} heeft de poort opengedaan!
    {% endif %}

    {{first_name}}

    {{states('first_name')}}

I get the following output:

Result
Result type: string
Ben heeft de poort opengedaan!
    
Ben
unknown

This template listens for the following state changed events:

Domain: person
Entity: first_name
Entity: input_button.poorttogglebutton
Entity: person.touchscreenkeuken

So the problem probably is related to {{states(‘first_name’)}} resolving into “unknown”, but I don’t know the reason for this, neither what to do to resolve this.

Many thanks for reading and of course would appreciate any help if possible!

kind regards,

Ben

'first_name is not a valid entity ID. So you.cannot ask for its state or check its state with is_state. Wht you are probably looking for is as simple as:

first_name == 'Ben'

Edwin is correct but to be more explicit…

You are wrapping the variable name first_name in quotes so the template interprets that variable name directly as a string that is expected to be a valid entity_id.

{% if is_state('first_name', 'Ben') %}
.
{{ first_name }} heeft de poort opengedaan!
.
{{first_name}}
.
{{states('first_name')}}

if you leave off the quotes then the interpreter will use the data in the variable (in this case ‘Ben’) as the input to the states and/or is_state functions.

but again that value (“Ben”) isn’t a valid entity_id so it will fail in that circumstance as well. But if the variable contained a valid entity_id it would work (but again without the quotes)

so for example this would work

## assuming you have an entity that is called sensor.name and it's state is Ben
{% set first_name = 'sensor.name' %}
{% if is_state(first_name, 'Ben') %}
  de poort wordt opengedaan!
{% else %}
  {{ states(first_name) }} heeft de poort opengedaan!
{% endif %}

But to use your logic above correctly:

{% if first_name == 'Ben') %}
  de poort wordt opengedaan!
{% else %}
  {{ first_name }} heeft de poort opengedaan!
{% endif %}
1 Like

Many thanks both! Learned again something today!

Have a great weekend,

Ben

2 Likes