Format the output of a template entity attribute list

Hello,

I have a zone that lists the people who are in that zone via the persons attribute. e.g.

entity: zone.home
attributes:

latitude: xx.xxxx
longitude: -xx.xxxx
radius: 51
passive: false
persons: person.wife, person.son, person.me
editable: true
icon: mdi:map-marker-account
friendly_name: Home

When I call a service to send myself a message, I use this:

message: "People at home are {{ state_attr('zone.home', 'persons') }}"

Which results in the following:

People at home are ['person.wife', 'person.son', 'person.me']

Is it possible to format the output into a more friendly list? e.g.

"People at home are wife, son, me."

Many thanks in advance.

Writing this question must have triggered the right brain cells because I was able to join up some different examples I found on this forum that resulted in the answer!

{{ state_attr('zone.home', 'persons') | join(', ') | regex_replace(find='person.', replace='') }}
1 Like

Ok, bonus question, is it possible to capitalize the name of each person?

e.g.
wife, son, me
to
Wife, Son, Me

I found that I can add | capitalize to the end of the template, but this only changes the first letter of the whole string, which is W.

| title

But, because Iā€™m a pedant, I would do it like:

{% set x = (expand(state_attr('zone.home', 'persons'))
| map(attribute='name')) | list %}
{{' and '.join((x | join(', ')).rsplit(', ', 1)) 
| default("No one", 1)}}{{ iif(x|count <= 1, " is home", " are home")}}
2 Likes

Perfect, that does the trick nicely, thank you very much!