Count people that are home

Thanks a lot! It finally showed up. :smile:

If you want it to update regularly you have to connect it to a state change I assume?

Did you restart home assistant after putting the code there?
And you have to add an entry for pythons scripts to the configuration.yaml, see the documentation for that.

That’s all I can think of :slight_smile:

hey guys two questions
1 how do i filter to count only phones?
2 do i need to make an automation to run the python_script every x time?
this is my configuration.yaml

home = 0
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 + ', '

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

sensor:
  - plataform: people_home
    name: home

Finally got python script to work but all i’m getting is the below. How do I modify the script to display the names of the items it detects? Or to show me the summary card?

CaptureCapture2

— edit----

the automation doesn’t help. Though the badges seem to update when running the script, they don’t do so automatically after a state change in the group. Must be a faulty automation.

Also, the Card is now out of sync with the individual badges, and error logs says its max its display characters.

5743

Wed Dec 06 2017 16:36:53 GMT+0100 (CET)

Error executing script: Invalid state encountered for entity id: sensor.summary. State max length is 255 characters.
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "summary.py", line 140, in <module>
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 717, in set
    self.async_set, entity_id, new_state, attributes, force_update,
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 405, in result
    return self.__get_result()
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/usr/lib/python3.6/site-packages/homeassistant/util/async.py", line 167, in run_callback
    future.set_result(callback(*args))
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 745, in async_set
    state = State(entity_id, new_state, attributes, last_changed)
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 540, in __init__
    "State max length is 255 characters.").format(entity_id))
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.summary. State max length is 255 characters.

hi @mviezzer

things are getting better, was kind of a search to tie it all together, but, here we are:


59
please let me ask a few questions:

  • as you see I’ve got no description below the individual badges, in either mode. Is that some custom ui setting i need?
  • the Home Summary doesn’t always show the mode, in this case the developer mode wasn’t updated. how can i make that robuster. I think it has to do with the script following a mode selection?
  • I’ve put the separate badges in a view/group now, but would love them to be on a dedicated card, as badges. Ive set that in the customize but, but when i do so, they appear like this:

26

no badges, and no description below the badges
.

  • I’ve .png files for all activities and modes. Some of them contain 2 or 3 words, like activity "Naar bed’, Uit huis’, ‘Aan de slag’. The image aan_de_slag.png won’t show though, neither does ‘aandeslag.png’ or ‘aan de slag.png’. How to show those images?
  • what scripts do you use after the mode-selection? i cant seem to get anything to happen for the individual modes, the than the dev badge changing into the visitors, (and now and then displaying the mode at the bottom of the card)

The Summary doesn’t alway seem to update, especially the home/away states for the tracked people. Not sure if this is supposed to be necessary but just incase I’ve tried this little automation, please have a look it this makes its better or just frustrates things;-)

# Call Summary after Family Presence change
- alias: 'Sense Family presence and update'
  id: '1511601478031'
  hide_entity: True
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: group.family
  condition: []
  action:
    - delay: 00:00:02
    - service: python_script.summary

Cheers,
Marius

Hi @Mariusthvdb, nice.
About the Developer mode (and other modes), has only the interface yet, lol, the core functionality is a WIP…
I recently discover too that the sensor has a 255 characters limit, I’ll fix that, like roflcoopter did here

About the badges in groups, I don’t have tried yet, but is possible with Custom UI GitHub - andrey-git/home-assistant-custom-ui: Custom UI elements for https://home-assistant.io

About the images in mode/profiles, try replace this line
‘entity_picture’: ‘/local/profiles/{}.png’.format(state.state.lower()),
by
‘entity_picture’: ‘/local/profiles/{}.png’.format(state.state.lower().replace(’ ', ‘_’)),
and configure your images in lower case with _, like_this.png

I also have issues with orders… sometimes they mixes positions, I’ll try to set a manual order, maybe fix.
You are right, the script needs to called each time a entity change state. Now i’m using a time interval automation (for update when a device is in use or ofline too): @Kem

this could be a quick one:

what about the /www/badge .png’s
if show_badges:
for badge in groups_badge:
if (badge != ‘’):
entity_id = ‘sensor.{}_badge’.format(badge.replace(’ ', ‘’).lower());
hidden = False if (groups_count[idx] > 0 or debug) else True
fname = groups_desc[idx] if debug else ’ ’
picture = ‘/local/badges/{}.png’.format(groups_badge_pic[idx]) if (groups_badge_pic[idx] != ‘’) else ‘’

and activity? Id like to show these too. Bug is only if an error/alert is present i think?, but the sensor.activitiy.badge need the underscore too.I cant show the correct png’s for the same reason as the profile selection.

Btw, Would it be cool to combine these in to one bigger Summary, import the activity into the python script and see it as one ‘package’. Might be easier to debug and not as complicated finding al references which now are spread a bit over the various files?

hmmmmm:

Log Details (ERROR)
Wed Dec 06 2017 23:48:08 GMT+0100 (CET)

Error loading script summary.py: Line 120: SyntaxError: invalid character in identifier in on statement: 'entity_picture':  '/local/profiles/{' '}.png'.format(state.state.lower().replace(’ ', ‘_’)),

never mind that one, was an wrong ’ ’ error.
Still no pictures are displayed, must try something else :wink:
Cheers,
Marius

Yes, bug is only visible when there is a problem (1+ device offline)
You need to do a replace for each set picture, the image file cannot contains space, in all other places stay with space…
I have updated my script and tested this, seems all working (I set it up to ignore space, so ‘Show off’, has a ‘showoff.png’)…

I decide to do the activity in a separate script because this funcionality will grow, and have in a separated file it will be more readible. Also I’m using datetime.now and notification (in the future) so the script need to be called only when a activity changed, not in a time interval (like summary).
For the other packages I put links to the related files inside the script :wink:

Cool!
I can confirm the developer bit to work right now and change pictures correctly, in 2 ways: in the first place does it change, and 2, it reads the right pictures with and without spaces:

Activity does not do that yet in my setting. It doesn’t change automatically, and it doesn’t read the right pictures, or doesn’t show the pictures where spaces are involved. will try to debug that.

maybe has to do with this:

Log Details (ERROR)
Thu Dec 07 2017 08:07:38 GMT+0100 (CET)

Error executing script: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "last_cmd.py", line 7, in <module>
TypeError: 'NoneType' object is not callable

Could we add and display the Activity mode in the Summary Badge, below the selected profile? Making it into a real Summary!

I was thinking to have 4 Modes:
1 Normal. speaks for itself :wink: That is to say, only load Overview, Map, logbook history and shopping list (possibly LogOut) in the left side menubar
2 Crowded: all kinds of automatics going on to have the house setup for climatic adjustments, maybe even automatically triggered by several sensors
3 Your Show-off, I’ve renamed that to Kiosk: show HA in Kiosk mode. NO left side bar at all
4 developer: with all dev tools in the left side menu bar (IDE, Terminal, NodeRed, IDE,Configurator, maybe even Hassio and the developer tools at the bottom ) this it what i would want to take out in the Normal mode.

Don’t know if thats even possible, would probably require restarts for each mode, but how cool wold that be!

Cheers,
Marius

Indeed, that’s the way i would like the updating to happen: based on state changes, not the brute force method of every 2 seconds.

Thats why I’ve tried this for the alarm-clock module, but it doesn’t call the python script, as i thought it would.
I thought to have learned not defining a specific state change, would mean the automation to hit on any state change?
alarmclock_full = alarm clock_wd+alarmclock_we

# Call Python:Summary after AlarmClock state change
  - alias: 'Sense Alarmclock state-change and update Summary'
    id: '1511601479003'
    hide_entity: True
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: group.alarmclock_full
    condition: []
    action:
      - service: python_script.summary

for what its worth,
ive debugged some of the automations, and found some listings that consisted of only one time, but still had the - in front of them.

changed that into this for example:

automation:
  - alias: 'Profile change'
    id: '1511601479005'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.ha_mode
    condition: []
    action:
#      service: script.refresh
      service: python_script.summary

And working perfectly and instantaneous now. Will try the other automations with this approach and see what happens.

—edit-----

same goes for activity now, changed it like this:

# Sensor update
hass.states.set('sensor.activity_badge', state_value, {
    'friendly_name': ' ',
    'entity_picture': '/local/activities/{' '}.png'.format(state_value.replace(' ','').lower()),
    'unit_of_measurement': 'Act'
})


Cool!

@mviezzer
HI, doing some serious training here, thanks to your wonderful scripts!
For my assurance i would love to have a thumbs up in place of the Bug, when all devices are ok. So i cn be 100% positive the scripts and automations are working as they should.
What would i need to change in the groups_badge definition to get that to show up?
Please have a look how we can do that.

thumbsup

Thanks,
Marius

1 Like

Yeah, not ideal, but I don’t want set each entity in automations… But now I think that I found a generic way, trying later on…

I’ll try this, but in a nutshell here is the idea.
groups_badge_pic = [‘’, ‘’, ‘ok|bug|error’] # a list of images to mach the count, for 0 use ok, 1 bug, 3+ error…
groups_min_show = [1, 1, 0] # a new array with minimun count to show

I’ll create a new thread for the Summary script.

That would be wise;-)

I for one would like to accomplish the text below the Badges, i cant get it to happen…

btw, adding the customization to these badges doesn’t do anything in my setup, with or without, they show as attached:

##########################################################################################
# Summary
##########################################################################################
#sensor.activity_badge:
#  state_card_mode: badges
#sensor.profile_badge:
#  state_card_mode: badges
#sensor.home_badge:
#  state_card_mode: badges
#sensor.inuse_badge:
#  state_card_mode: badges
#sensor.alert_badge:
#  state_card_mode: badges

I’ve checked again, and although in the documentation is pointed out this should work, and a state change for the entities in the group should cause the automation to trigger, it actually doesn’t. And when i check in the <> developers state inspector, that seems correct, for the group is stated Home, even when members are not home …

Hence i tried this:

# Call Summary after Family Presence change
- alias: 'Sense Family presence and update'
  id: '1511601478031'
#  hide_entity: True
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.marijn,device_tracker.wijke,device_tracker.frederike,
               device_tracker.marte,device_tracker.louise,device_tracker.hanna
  condition: []
  action:
    service: python_script.summary

And with immediate succes.

Leads to the next desire: to have an automation dynamically be created for group members of the focus groups in the Python script Summary:

definitions Summary right now:

# Entities summary by group name
groups = ['group.family', 'group.audio', 'group.imac']
groups_format = ['{} at home: {}', '{} in use: {}', '!{} offline: {}']
groups_filter = ['home', 'on|playing|home', 'off|idle|not_home']
groups_badge = ['Home', 'In use', 'Alert']
groups_badge_pic = ['', '', 'bug']
groups_desc = ['Nobody in home', '', '']
groups_count = [0, 0, 0]

So automation should be created for members of group.family, group.audio, group.imac.

Is that possible, preferably in 1 automation? That would catch all issues left.

Cheers,
Marius

New topic for the summary: Summary card and badges for people, devices and status (with python script and custom card)
I found a way to call the summary only when needed (for scripts and switchs at least). For people I set a 1 minute interval automation…

Please help
Error when restart home assistant:
the following components and platforms could not be set up python script

What this error means:

expected an indented block in on statement: state = hass.states.get(entity_id)

Hi, very good python script.
Is it possible to filter some people by the friendly_name ?

Found it by myself

for entity_id in hass.states.entity_ids('device_tracker'):
  state = hass.states.get(entity_id)
  if state.name == 'XXXXX':
   continue
  if state.state == 'home':
			home = home + 1