Count people that are home

Is there anything you have to put in your config file? This is my first attempt at adding a script, I can execute the script but nothing happens.

Do you use an automation to trigger the script?

hi, new here, in a steep learning curve…

had this binary people_sensor defined:

platform: template
sensors:
  people_home:
    friendly_name: "Er is leven thuis"
    entity_id:
      - device_tracker.telefoon
      - device_tracker.iphone
      - device_tracker.telefoon_frederike
      - device_tracker.telefoon_marte
      - device_tracker.telefoon_louise
      - device_tracker.telefoon_hanna
    value_template: >-
      {{ is_state('device_tracker.telefoon', 'home')
         or is_state('device_tracker.iphone', 'home')
         or is_state('device_tracker.telefoon_frederike', 'home')
         or is_state('device_tracker.telefoon_marte', 'home')
         or is_state('device_tracker.telefoon_louise', 'home') }}
         or is_state('device_tracker.iphone_hanna', 'home')

and would love to try the script @balloob posted above about counting people, but int seem to find the correct syntax for Hassio. Could you please guide me in the right direction. below doesn’t do it obviously, since it doesn’t come close to being correct…

count_people_home:
  alias: Count people home
  sequence:
    - home = 0
      for entity_id in hass.states.entity_ids('device_tracker'):
        state = hass.states.get(entity_id)
        if state.state == 'home':
          home = home + 1
    - hass.states.set('sensor.people_home', home, {
       'unit_of_measurement': 'people',
       'friendly_name': 'People home'
})

furthermore, id want it to show the names of the people at home, and people away.

Hope you can help.
Cheers,
Marius

It’s not a “script”, but a python_script, that’s why it’s not working.

2 Likes

----edit—
the issue has changed into how to select the correct set of devices to check, its now checking all of my device_trackers. resulting in 21 people home
i tried to narrow that down by adding device_trackers.telefoon since all family members have a phone setup like that but that won’t do it, just as device_trackers.telefoon_* for that matter. Ive made a group.family to track, but i cant use that either so it seems.
How to change the line for entity_id in hass.states.entity_ids('device_tracker') to use this group.family, or a maybe a list of devices?
thanks
Marius

----end edit----

HI Isabella,
duh…
thank you for that rather basic info… talking about a steep learning curve, next to yaml and json i now have python to add :wink:

please, how to show up this python script’s result in the front end. I know i should be using an automation, but how should it read? id love to have it show the number of tracked people Home, and preferably the names to of course.
does it create the sensor.people_home in the fly dynamically, or should i define that first. I already have a binary_sensor.people_home, maybe some confusion here is added to the… confusion.

made a very basic automation for testing purposes, no result yet im afraid…

— Run the people_home.py script every 15s

- alias: 'People Home
  id: '1511601478424'
  trigger:
    platform: time
    seconds: 15
#  condition:
#    condition: state
#    entity_id: group.family
#    state: 'home'
  action:
    service: python_script.people_home

Thx for your trouble!
Marius

I filter by ‘phone’ in entity (also make a list with the names to show in a summary card):

for entity_id in hass.states.entity_ids('device_tracker'):
    if entity_id.find("phone") >= 0:
        state = hass.states.get(entity_id)
        if state.state == 'home':
            home_count = home_count + 1
            home_desc = home_desc + state.name + ', '

Summary

Full script at https://github.com/maattdiy/home-assistant-config/blob/master/python_scripts/summary.py

1 Like

cool, thats what im looking for indeed. cant find your automation though :wink: I must check mine and the python I’ve made with your help, since it is still showing the in the front end. The filter must not work??

13

python:

home_count = 0
home_desc = ""
inuse_count = 0
inuse_desc = ""
summary = ""

for entity_id in hass.states.entity_ids('device_tracker'):
    if entity_id.find("telefoon") >= 0:
    state = hass.states.get(entity_id)
    if state.state == 'home':
        home_count = home_count + 1
        home_desc = home_desc + state.name + ', '

if home_count > 0:
    home_desc = str(home_count) + ' at home: ' + home_desc[:-2]
else:
    home_desc = 'Nobody in home'

summary = home_desc

hass.states.set('sensor.people_home', home_desc, {
    'unit_of_measurement': 'people',
    'friendly_name': 'People home'
})

automation:

- alias: 'People Home'
  id: '1511601478424'
  trigger:
    platform: time
    seconds: 15
#  condition:
#    condition: state
#    entity_id: group.family
#    state: 'home'
  action:
    service: python_script.people_home

cheers,
Marius

Use the variable ‘home_count’ for the numer of people.
‘home_desc’ is for description, suggestion:

hass.states.set(‘sensor.people_home’, home_count, {
‘unit_of_measurement’: ‘people’,
‘friendly_name’: home_desc
})

Check the indentation. After check the log for other erros.
The sensor name used (sensor.people_home) is the one in the view config?

yes thats the sensor, it is created on the run as i understand it.

Right now, it shows 6 people home, but i believe it is just counting the known devices with name telefoon in it, not the devices that have state Home.
Ill check the indentation, have to find a python checker like yamllint, and then find a way to display the names of the people home. How do you do that? its not in the python but in an automation i think?

errors:

Error executing script: name 'home' is not defined
17:02 components/python_script.py (ERROR)
Error executing script: name 'home' is not defined
17:01 components/python_script.py (ERROR)
Error loading script people_home.py: Line 10: IndentationError: unexpected indent in on statement: if state.state == 'home':
17:00 components/python_script.py (ERROR)

mac python_scripts complaining about line 7, name ‘hass’ is not defined…

Wih this it will show the names below the badges, because it is set in friendly_name (used as badge label). If you have a lot of names maybe is better to show in another sensor/a custom state card.

hass.states.set(‘sensor.people_home’, home_count, {
   ‘unit_of_measurement’: ‘people’,
   ‘friendly_name’: home_desc
})

right, thanks, i certainly will try that lateron, but for now it must be correct first just the way it is. Something is still not in order, the indentation is not as straight forward as Yaml i suppose.
do you have a suggestion for a checker?

could i filter from a separate group also? Ive joined the devices to be tracked into a group.family, and it might be simpler to have the original line for entity_id in hass.states.entity_ids('device_tracker') check in this group for entity_id’s with state ‘home’ ?

For syntax errors I just check the error log (it will show the incorrect line numer).
For logic errors I add logger.error(“message”) for debug, like here.
But if is the same as here should be ok.
Yes, you can check the group.family, not sure if is less or more code, I can check it ou later when I’ll be in my setup.

our posts crossed…

making progress, functionality seems to be working now.:

49

have to let it show friendly names below the badge, or indeed maybe a custom card.

do have a look for me please filtering from a group though, that seems to be more robust, and logical somehow, which is always easier to debug if necessary

btw, my phone is off for testing purposes, and still it shows Marijn IC and telefoon MHB… iCloud and HomeAssistant. Strange they’re not gone… been over an hour, so they should have been updated.

anyways,
Thanks!

for the sake of being able to see whats output, ive added the sensor to a badge. So don’t judge the layout yet please…

still ive tried to add the last change of state, halfway successfully. i now need to reformat that to show only H, M, S of the last state change. Any suggestion how to do that please? Here’s my current script and result in the frontend.

hass.states.set('sensor.count_people_home', home_count, {
    'unit_of_measurement': state.last_changed,
    'friendly_name':  home_desc

48

as a badge:

47

HI @mviezzer,
can i ask about your alarmclock.yaml. different thread maybe but same person :wink: or would etiquette require a new thread for this.

question short:
copied the alarmclock into a packages dir, and after first restart the alarmclock shows up fine in homepage.
when i move it (both of them) to a separate dedicated Tab (group/view ) the clock disappears, and the selectors showup???

Cheers,
Marius

Hi, I have the group filter and state last change done :grinning:
It’s in my repo… I’ll keep updated as improvments go on…
I went a little further and now you can set a group list to do the summary with different parameters. So I create a device group always on and will alert if anyone is offline.
If the state change yesterday it will show the date too.
Now I’m trying figure out the timezone…


summary

PS; About the alarm clock,I also have in a dedicated tab and working.I had a similar problem in other ocasiations: cache (do a force refresh) or yaml/script syntax (check the log).
Open a new thread or drop me a message.

1 Like

thats cool! your python looks much better than my effort…

copied it and somehow things don’t show up as they should.
Ive put the 3 sensors in a group and tab, and then only this shows up:

55

notice the summary sensor doesn’t do anything here.
If i take that out of the group, the group card remains the same, but this badge (the most left one) is shown in the homepage:

29

i did take out the input_select for developer mode. I have no other modes yet, so that would not have been functional,and it gave me several error messages.

Also took out a typo(?) in line 53 and 54: domnains

btw notice the 2 other badges. These are the template sensors of the alarm clock, which is in its own card. ill do another thread for that indeed,

Cheers,
Marius

just rechecked all and might have found it. would state-card-value_only be the culprit of the summary badge not showing? i don’t have that declared anywhere in the custom ui?

Cheers,
Marius

I have updated my script.
I think that addresses all your issues: there are some flags at the beginning, and if the entity do not exist will not throw error, so now it’s easier to anyone copy the file and start using, with minimun changes.

I also:
Add a summary of the alarm clock (similar to you)
The ‘in use’ is now also doing by group, and the group options to generate the summary are more generic.

summary

In debug mode:

summary_debug

wow,
will try this weekend.
Strangest thing is that the 2 issues (alarm and people count) seem to be related somehow. The alarmclock shows up in the frontend homepage, and when i move it to a separate page in a group, or use the People Home scripts, the alarm splits up in 2 separate inputs…
must have something to with the separate declaration of parts in the scripts or the package itself.

Thanks for developing, and thinking along. will try to be more specific after ive changed into your new script.

Cheers,
Marius