o dear this has been a long time…
evolved from the summary script quite extensively, and, using the people counter has become this in my setup:
familyEntities = 'group.family_home'
daughterEntities = 'group.daughters_home'
familyColor = ['grey', # count_home = 0 #808080
'steelblue', # = 1
'saddlebrown', # = 2 #00f
'gold', # = 3 #fbd229
'darkorange', # = 4 #ff8700
'maroon', # = 5 #ff0f00
'green'] # = 6
familyIcon = ['mdi:account-off', # count_home = 0
'mdi:account', # = 1
'mdi:account-multiple', # = 2
'mdi:account-multiple-check', # = 3
'mdi:account-group'] # > 3
utc_offset = hass.states.get('sensor.utc_offset').state
timeDifference = float(utc_offset)
def CountMyEntities(hass, entity_list, entity_state=None, not_state=None):
cnt = 0
for entity_id in hass.states.get(entity_list).attributes['entity_id']:
state = hass.states.get(entity_id)
if entity_state is not None and state.state == entity_state:
cnt = cnt + 1
if not_state is not None and state.state != not_state:
cnt = cnt + 1
return cnt
count_home = CountMyEntities(hass, familyEntities, 'home')
count_away = CountMyEntities(hass, familyEntities, not_state='home')
daughter_count_home = CountMyEntities(hass, daughterEntities, 'home')
daughter_count_away = CountMyEntities(hass, daughterEntities, not_state='home')
def NameMyEntities(hass, entity_list, entity_state=None, not_state=None):
global timeDifference
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= timeDifference)
time = '%02d:%02d' % (dt.hour, dt.minute)
if entity_state is not None and state.state == entity_state:
name = '{} ({})'.format(state.attributes['friendly_name'], time)
names.append(name)
if not_state is not None and state.state != not_state:
name = '{} ({})'.format(state.attributes['friendly_name'], time)
names.append(name)
return names
home = NameMyEntities(hass,familyEntities,'home')
away = NameMyEntities(hass,familyEntities,not_state='home')
daughter_home = NameMyEntities(hass,daughterEntities,'home')
daughter_away = NameMyEntities(hass,daughterEntities,not_state='home')
familyCount = count_home + count_away
daughterCount= daughter_count_home + daughter_count_away
daughterIcon = familyIcon[daughter_count_home] if daughter_count_home <= 3 else familyIcon[-1]
whichIcon = familyIcon[count_home] if count_home <= 3 else familyIcon[-1]
# the % symbol is mod, that means it will cycle through the list and never get 'Index out of range'
icon_color = familyColor[count_home%len(familyColor)]
daughter_icon_color = familyColor[daughter_count_home%len(familyColor)]
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,
'icon_color':icon_color
})
hass.states.set('sensor.daughters_home',daughter_count_home,{
'friendly_name':'Daughters Home?',
'home':', '.join(daughter_home),
'away':', '.join(daughter_away),
'icon':daughterIcon,
'count_home':daughter_count_home,
'count_away':daughter_count_away,
'daughter_count':daughterCount,
'icon_color':daughter_icon_color
})
does a few more things, guards for None entities, checks the correct time using sensor.utc_offset
and customizes (because python created entities cant be customized using regular ha customize:
)