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 %}
- 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 -%}
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.
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”