Create a template sensor that keeps track of total number of people at home?

Trying to create a template sensor that gives me total number of people at home. I use the people entity, so just need help on the syntax to write an array or something using Jinja that holds the entities, then I can use a for loop to add a number each time someone arrives home but subtracts a number when someone is not home.

If this is not possible using template sensors then i will need to use Appdaemon or NodeRed and maybe i will feed that as a sensor to home assistant so i can reference it there, but becuase home assistant is the main one i want to keep track of this logic there.

1 Like
{{ dict((states.person)|groupby('state'))['home']|count }}
5 Likes

You’re amazing! I didn’t know if I would have arrived at this ever or not until hours of fiddling on google. Haven’t tried it but will plop it in when I get home!

for some reason, and i think i am just not following, but it seems the syntax is different when you put this in a template sensor. I have errors right now.

Invalid config for [sensor.template]: invalid slug people_atHome (try people_athome) for dictionary value @ data['sensors']. Got OrderedDict([('miguel_state', OrderedDict([('friendly_name', 'Miguel status'), ('value_template', '{% if not is_state("person.miguel", "home") %}\n  Away\n{% elif is_state("person.miguel", "home") and is_state("switch.miguel_asleep", "off") %}\n  Home\n{% elif is_state("person.miguel", "home") and is_state("switch.miguel_asleep", "on") %}\n  Sleeping\n{% endif %}')])), ('amanda_state', OrderedDict([('friendly_name', 'Amanda status'), ('value_template', '{% if not is_state("person.amanda", "ho.... (See ?, line ?). 

i inputted this

    people_atHome:
      friendly_name: "People at Home"
      value_template: >-
        {{ dict((states.person)|groupby('state'))['home']|count }}

EDIT: evidentially i can’t have capitals on the name…

1 Like

The suggested template isn’t suited for use in a Template Sensor because it contains no entities. Without entities, Home Assistant has nothing to monitor for state-changes. As a result, the Template Sensor will be evaluated when Home Assistant starts and then never again (not until the next restart).

The following template contains a list of entities whose state-changes will cause the Template Sensor to be evaluated (i.e updated).

    people_at_home:
      friendly_name: "People at Home"
      value_template: >-
        {{ [states.person.john, states.person.mary, states.person.bob]
           | selectattr('state', 'eq', 'home')
           | list | count }}

Also, don’t use capitalized letters in entity names. That’s what the error message is complaining about:

invalid slug people_atHome

All entity names are in lowercase.

2 Likes

ok thank you! I hadn’t experienced that yet as we were all home right now, but just entered this and will test it out. I wonder why this is the case?

Is that really true? When entering this template

{{ dict((states.person)|groupby('state'))['home']|count }}

in template editor it shows the following message:

This template listens for the following state changed events:
   Domain: person

I’ve also tested it and it updates nicely when a person comes or leaves home

You’re commenting on a statement made over a year ago.

Home Assistant evolves rapidly and statements made months or years ago are likely to have been true at the time but rendered false by subsequent enhancements. The detection and evaluation of entities within a template has changed significantly over the past year (notably in 0.115 when the entity_id option was eliminated for Template Sensors).

Go back to version 0.109 and see if that still works for you.

Well, it is not a critic but just an update. This Q&A still here and shows up in google search so I find it useful for any other person that will have the same question to have an up to date answer. I hope you’d agree. And it doesn’t matter if this was not working a year ago I believe.

Except it wasn’t written as an update but as a question:

If it’s meant to be an update, then write it like an update. Minimally explain how and when it changed. All you did was challenge the veracity of an old post with a single example and failed to explain why it now works.

It mattered to everyone who was using the product a year ago. You couldn’t use states or sub-domains like states.light or states.person in a template and expect them to be monitored (because they aren’t entities and so would not be assigned listeners). That wouldn’t change until many versions (and months) later.

There are years worth of outdated information on this forum and Google search is likely to point it to as well. If your plan is to update more of them then explain what has changed and why and not simply challenge the old post with “Not true, it works now.”

You are absolutely right. With just one exception. I did’t know if and when this has changed so I can’t explain it. The only thing I can write differently is to change my question to: “Is that still true?” I didn’t come here to update outdated information but I was looking for answer to the same question as the author of this thread.

Is there a need to keep a history of what worked how in the past and explain it? I still believe it doesn’t matter to anyone anymore. What matters is how it works at the time that someone finds the very same question and if can get most up to date information - bingo. And that was my intention here. Sorry that you don’t like the form in which I’ve posted my question/update here based on my current experience. I should maybe also add which version of HA I’m using (2021.6.4) as maybe in the future it won’t work anymore.

You previously claimed you did:

As a general rule, there’s no value in questioning the behavior of a very old version of Home Assistant (early March 2020 it was version 0.106) to the latest version (2021.06). That’s a difference of nineteen versions of Home Assistant; of course there will be changes in the way it operates. The clue is that the post you were “updating” is clearly date-stamped as Mar '20 which means March 2020.

Screenshot from 2021-06-15 15-52-12

The other clue is that after you submitted your post it was tagged with 1 year later which clearly indicates you are commenting about old information.

Screenshot from 2021-06-15 15-53-37

Your current experience is that you don’t understand the purpose of a post’s datestamp. Unless you understand what it implies, you will have a very difficult time with this community forum’s information because it dates back many years. In other words, it contains a lot of historical information.

Pro tip:
If it’s more than 6 months old, whatever information it provides might work differently now.

Or use NodeRed, it doesn’t change so often because it has all those features for many years already.

Jesus, really don’t understand guys why you are getting on adorobis. Such attitude makes this place so unfriendly. The forum is hell outdated while the attempt of updating it hits the wall of aggresivity. Really? is it so important if it is updated by question or by statement?

1 Like

adorobis is of two minds on that point. First it was stated to be an update (but not written as one) and then stated to be not an update. Perhaps it’s a quantum state …

Basically the comment was attempting to challenge how something worked 19 versions ago. There’s simply no value in questioning the behavior of outdated software.

Seriously, what’s next? Challenge every old post that advised people against using now() in a template? Or “correct” posts that included entity_id in a Template Sensor? How about the examples showing looping before repeat was introduced? Perhaps we should also challenge all the old posts concerning the State UI. Honestly, questioning old posts is silly.

It was neither; it challenged a statement made over a year. There’s little value in questioning what is now effectively a historical oddity.

Thanks! It’s the first time I’ve met such a response… I’m just waiting for a sentence or a fine now :wink:

there is great value in that: updating information which is indexed by google. Otherwise 99% of it leads to confusing and outdated information.
If he is right he should get response: yes you are right. It is like you say since ver. x.y.z.

1 Like

Anything prior to four versions ago might contain information that is no longer applicable, therefore a good chunk of this forum contains historical, or what you called “outdated”, information. Chances are good that Google will exhume historical posts. It’s generally inadvisable to rely on any old software information so it’s a caveat emptor situation.

adorobis is correct for all versions since 0.115 (and more changes implemented in 0.116) so over six versions ago. That’s what makes challenging the comments in this old post so ludicrous. The information adorabis presents is too little and too late.