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'
})
@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?
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?
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.
----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
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
cool, thats what im looking for indeed. cant find your automation though 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??
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'
})
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ā¦