Using "to_state.attributes.friendly_name" does nog seem to work in IF statement

Trying to welcome a specific person entering the house by saying who is home, except for the person who just entered. Works perfectly, however I alwasy get the complete list of persons A, B, C and C.
What is wrong with my “if” statement?

Automation trigger:

- alias: Iemand komt thuis
  trigger:
    platform: state
    entity_id:
    - person.A
    - person.B
    - person.C
    - person.D
    to: home
  action:
    - service: tts.google_cloud_say
    data:
      entity_id: media_player.nest_keuken
      message: >
        {%- for persoon in 'A','B','C','D' -%} 
          {% if trigger.to_state.attributes.fiendly_name != persoon %} 
          {{ " "}}{{persoon}} is 
            {%- if states("person."+ persoon) != "home" -%}
            not 
            {% endif %}
          at home 
          {% endif %} 
        {%- endfor%}

This always gives the complete list. What statement should

{% if trigger.to_state.attributes.fiendly_name != persoon %} 

be to make it skip the “persoon” ?

Missing ‚r‘ …

alas! even with

        {%- for persoon in 'A','B','C','D' -%} 
          {% if trigger.to_state.attributes.friendly_name != persoon %} 
          {{ " "}}{{persoon}} is 
            {%- if states("person."+ persoon) != "home" -%}
            helaas niet 
            {% endif %}
          thuis. 
          {% endif %} 
        {%- endfor%}

still getting all persons.

Do some debugging output infront, that displays what is returned by this statement, like:

{{ states("person."+person) }}

Try this.

- alias: Iemand komt thuis
  trigger:
    platform: state
    entity_id:
      - person.A
      - person.B
      - person.C
      - person.D
    to: home
  action:
    - service: tts.google_cloud_say
      data:
        entity_id: media_player.nest_keuken
        message: >
          {%- for persoon in ['A','B','C','D'] -%} 
            {%- if trigger.to_state.attributes.friendly_name != persoon -%} 
              {{- persoon }} is {{ 'not' if states('person.' ~ persoon) != "home" }} at home.{{ ' ' if not loop.last -}}
            {%- endif -%} 
          {%- endfor -%}   

I would probably do the message like this though.

          {% set people = ['A','B','C','D'] %}
          {% set others = expand(people)|rejectattr('entity_id','eq','trigger.entity_id')|map(attribute='friendly_name')|list %}
          {%- for person in others -%} 
              {{- person }} is {{ 'not' if states('person.' ~ person ) != "home" }} at home.{{ ' ' if not loop.last -}}
          {%- endfor -%}

Actually, if I read your automation correctly, it is to be announced if somebody came home or left. So it would be suffficent to use:

  action:
    - service: tts.google_cloud_say
      data:
        entity_id: media_player.nest_keuken
        message: >
          {{ state_attr(trigger.to_state,'friendly_name') }} 
          is
          {{ 'heelas niet' if is_state(trigger.to_state,'not_home') }}
          thuis.

Hi @m0wlheld, its a bit more complicated. I have several persons in HA, but for only 4 of them i want to announce who is home when a person enters… And, of those 4, i do not want to announce the name of the person that just came home.
So:

  • B is home, A, C and D are not.
  • If A enters home, it should be announced that B is home, and C and D are not.

and (replying to your previous question), the

{{ " "}}{{persoon}} is 
            {%- if states("person."+ persoon) != "home" -%}
            not 
            {% endif %}
          at home 

Works perfectly. “not” is announced when that person is not home

and the elegant solution of @jazzyisj works as well. Unfortunately, also with that solution, all persons A, B, C and D are announced.

AHHH! bugger!

I check the friendly name with all lowercase, but actually the friendly name begins with uppercase letter
so obviously trigger.to_state.attributes.friendly_name “Frans” is not equal to persoon “frans”

Fixed that, now it works like a charm.

Thanks all for your help

1 Like