Count people that are home

Requires Home Assistant 0.47.1 or later

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'
})

17 Likes

@balloob I modified the above script to count the number of lights (including smart lights and switches) that are on and I get the right value.

on = 0
for entity_id in hass.states.entity_ids('light'):
    state = hass.states.get(entity_id)
    if state.state == 'on':
        on = on + 1
for entity_id in hass.states.entity_ids('switch'):
    state = hass.states.get(entity_id)
    if state.state == 'on':
        on = on + 1
hass.states.set('sensor.lights_on', on, {
    'unit_of_measurement': 'lights',
    'friendly_name': 'Lights On'
})

Is there any way to combine the two for loops in one? Also, I am guessing the script runs only when called. So, we will need an automation to continuously update the sensor valueā€¦is that correct?

1 Like

You could run the script via an automation only when entity states change for light & switch.

I assume you can use the ā€œspecialā€ groups group.all_lights and group.all_switches

1 Like

Read the docs for the available functions to fetch states https://dev-docs.home-assistant.io/en/master/api/core.html#homeassistant.core.StateMachine

And yes you will have to connect it to an automation to trigger it. You cannot use the all_lights group because it only goes to off when all is off.

1 Like

Thank you for the simple example, I am a very novice Python user so it is helpful to see things like this.

How would you make it read the attribute friendly_name and set the sensor to a list of names? I have tried to adapt it but canā€™t get it right

Use state.name

1 Like

Hi, i try to make a python script but I donā€™t work. I installed home assistant in my raspberry pi with All-In-One Installer. Hi call also a simple hello world programme with the Python script service but I donā€™t do anything. Have you any idea?

Without seeing your script it is difficult to help you. Could you please open a new thread for that?

HI, this is my python script:

name = data.get(ā€˜nameā€™, ā€˜worldā€™)
logger.info(ā€œHello {}ā€.format(name))
hass.bus.fire(name, { ā€œwowā€: ā€œfrom a Python script!ā€ })

Is esactly the hello word script bring to the official home assistant website : https://home-assistant.io/components/python_script/

When I call the service:

Yes, is not show me anything

Events are not visible in the UI, set a state instead

1 Like

How do you set up the sensor in configuration.yaml? Does it need to be a specific type?

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ā€¦