rewriting some of the python scripts to keep thing smaller, simpler and more maintainable ive conjured up this little family counter. Only thing id like to add is the time last changed after each entity_id, but im a bit confused where to put the code…the earlier setup of a related script doesn’t apply since this is declared differently (function).
please have a look with me?
familyEntities = 'group.family'
def CountMyEntities(hass, entity_list, entity_state):
cnt = 0
for entity_id in hass.states.get(entity_list).attributes['entity_id']:
state = hass.states.get(entity_id)
if state.state == entity_state:
cnt = cnt + 1
return cnt
count_home = CountMyEntities(hass, familyEntities, 'home')
count_not_home = CountMyEntities(hass, familyEntities, 'not_home')
def NameMyEntities(hass, entity_list, entity_state):
names = []
for entity_id in hass.states.get(entity_list).attributes['entity_id']:
state = hass.states.get(entity_id)
dt = state.last_updated + datetime.timedelta(hours= 2)
time = '%02d:%02d' % (dt.hour, dt.minute)
if state.state == entity_state:
names.append(hass.states.get(entity_id).attributes['friendly_name'])
return names
names_home = NameMyEntities(hass, familyEntities, 'home')
names_not_home = NameMyEntities(hass, familyEntities, 'not_home')
familyCount = count_home + count_not_home
whichIcon = 'mdi:account-check' if count_home > 0 else 'mdi:account-off'
#status = count_home #count_home if count_home > 0 else 0
hass.states.set('sensor.family_home', count_home, {
'friendly_name': "Family Home?",
'Home': ', '.join(names_home),
'Not Home': ', '.join(names_not_home),
'icon': whichIcon,
'at_home': count_home,
'not_home': count_not_home,
'family_count': familyCount
})
id like the names to be formatted like this somehow:
names = '{} : ({}), '.format( state.name, time)
after some experimenting, found this to be working alright:
if state.state == entity_state:
name = '{} ({})'.format(state.attributes['friendly_name'], time)
names.append(name)
as in the other mentioned script, the difference between last_updated and last_changed seems unclear. And instead of format(state.attributes['friendly_name'], time)
I can use format(state, time)
without any difference in the final listing.
So here is the complete script, if corrections are to be made in the code, please don’t hesitate…;-)) Id specifically would like to know whether I could further compress the 2 functions into 1, and maybe even use a variable (filter?) for the home and not_home, which I now semi-hardcode
##########################################################################################
# family_home.py, based on Whats_on.py, previously known as Anything_on...
#
# interrelated with the Summary.py, which doesn't show the entities per badge (yet)
# modified and customized by https://community.home-assistant.io/u/Mariusthvdb/summary
# big hand in Python by https://community.home-assistant.io/u/petro/summary
# tbd: timezone correction a bit of a challenge, solved with a little hack (=2)
##########################################################################################
familyEntities = 'group.family'
def CountMyEntities(hass, entity_list, entity_state):
cnt = 0
for entity_id in hass.states.get(entity_list).attributes['entity_id']:
state = hass.states.get(entity_id)
if state.state == entity_state:
cnt = cnt + 1
return cnt
count_home = CountMyEntities(hass, familyEntities, 'home')
count_not_home = CountMyEntities(hass, familyEntities, 'not_home')
def NameMyEntities(hass, entity_list, entity_state):
names = []
for entity_id in hass.states.get(entity_list).attributes['entity_id']:
state = hass.states.get(entity_id)
dt = state.last_changed + datetime.timedelta(hours= 2)
time = '%02d:%02d' % (dt.hour, dt.minute)
if state.state == entity_state:
name = '{} ({})'.format(state.attributes['friendly_name'], time)
names.append(name)
return names
home = NameMyEntities(hass, familyEntities, 'home')
away = NameMyEntities(hass, familyEntities, 'not_home')
familyCount = count_home + count_away
whichIcon = 'mdi:account-check' if count_home > 0 else 'mdi:account-off'
#status = count_home #count_home if count_home > 0 else 0
hass.states.set('sensor.family_home', count_home, {
'friendly_name': "Family Home?",
'home': ', '.join(home),
'away': ', '.join(away),
'icon': whichIcon,
'count_home': count_home,
'count_away': count_away,
'family_count': familyCount
})