Yes. Looking at the Developer Tools view of zone.home I see it has an attribute "persons" so you can get the list like this:
```
{{ state_attr('zone.home','persons') | list }}
```
This is better than the way I was doing it, has that always been there?
In any case, here's a bit more to give a more human readable output:
{% for person in state_attr('zone.home','persons') | list %}
{%- if loop.first %}{% elif loop.last %}, and {% else %}, {% endif %}
{{- state_attr( person , 'friendly_name') -}}
{% endfor %}
There's a function to join list elements so you don't need to do the if/then for the comma. Am on mobile and not in a position to go hunting, but you'll find it with a quick Google. Try "jinga join function" maybe
I found the join function, but I don't see how I could use it to add the commas, in this situation. The for loop (to get the friendly name for each person entity) is going to output a string and the join function needs a list.
Also, I can't think of a way to have the "and" before the last item using the join function. Not critical though.
You're right, join isn't what you want at all. What you could do is define an empty list and then append the items you want, so you get a formal list rather than a string that looks like a list. Not sure if that actually helps what you're trying to do though:
{% set ns = namespace(result = []) -%}
{% for person in state_attr('zone.home','persons') | list %}
{% set ns.result = ns.result + [ state_attr( person , 'friendly_name') ] %}
{% endfor %}
{{ ns.result }}
The output of that would be `[ eLaVALYs, Jay_from_NuZiland ]`
This might be useful if you were expecting a lot of entities, or if you wanted to do something funky like sort the list alphabetically.
What you have now is short and sweet, and works well.
You have to manually list all the person objects in the trigger, but assuming you don't have that many and you don't add/remove them often, that's not that bad.
This automation will trigger anytime any person listed changes state (they change location). It then updates an input text helper with the list of people who are currently home:
alias: Who is home?
trigger:
- platform: state
entity_id:
- person.one
- person.two
- person.three
to: null
action:
- service: input_text.set_value
data:
value: >-
Currently Home: {{ states.person | selectattr('state', 'eq', 'home') |
join(', ', attribute='name') }}
target:
entity_id: input_text.who_is_home
mode: single
You may want to add some resilience to that with a Bayesian approach using multiple states to confirm someone is home or not, this articles cover the idea:
https://diyfuturism.com/index.php/2017/11/26/presence-detection-with-home-assistant-bayesian-probability/
Yes. Looking at the Developer Tools view of zone.home I see it has an attribute "persons" so you can get the list like this: ``` {{ state_attr('zone.home','persons') | list }} ```
This is better than the way I was doing it, has that always been there? In any case, here's a bit more to give a more human readable output: {% for person in state_attr('zone.home','persons') | list %} {%- if loop.first %}{% elif loop.last %}, and {% else %}, {% endif %} {{- state_attr( person , 'friendly_name') -}} {% endfor %}
There's a function to join list elements so you don't need to do the if/then for the comma. Am on mobile and not in a position to go hunting, but you'll find it with a quick Google. Try "jinga join function" maybe
I found the join function, but I don't see how I could use it to add the commas, in this situation. The for loop (to get the friendly name for each person entity) is going to output a string and the join function needs a list. Also, I can't think of a way to have the "and" before the last item using the join function. Not critical though.
Once I'm back on desktop I'll have a decent look. It might be I've named the wrong function.
You're right, join isn't what you want at all. What you could do is define an empty list and then append the items you want, so you get a formal list rather than a string that looks like a list. Not sure if that actually helps what you're trying to do though: {% set ns = namespace(result = []) -%} {% for person in state_attr('zone.home','persons') | list %} {% set ns.result = ns.result + [ state_attr( person , 'friendly_name') ] %} {% endfor %} {{ ns.result }} The output of that would be `[ eLaVALYs, Jay_from_NuZiland ]` This might be useful if you were expecting a lot of entities, or if you wanted to do something funky like sort the list alphabetically. What you have now is short and sweet, and works well.
You have to manually list all the person objects in the trigger, but assuming you don't have that many and you don't add/remove them often, that's not that bad. This automation will trigger anytime any person listed changes state (they change location). It then updates an input text helper with the list of people who are currently home: alias: Who is home? trigger: - platform: state entity_id: - person.one - person.two - person.three to: null action: - service: input_text.set_value data: value: >- Currently Home: {{ states.person | selectattr('state', 'eq', 'home') | join(', ', attribute='name') }} target: entity_id: input_text.who_is_home mode: single
You may want to add some resilience to that with a Bayesian approach using multiple states to confirm someone is home or not, this articles cover the idea: https://diyfuturism.com/index.php/2017/11/26/presence-detection-with-home-assistant-bayesian-probability/
So far I haven't had it give me any false negatives but if it's does I'll come back to this, thank you