had to edit a bit, but cant find the reason for this error appearing in the log, neither can PythonLint… still HA complains:
Error executing script: list index out of range
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 37, in <module>
File "/usr/lib/python3.6/site-packages/RestrictedPython/Eval.py", line 35, in default_guarded_getitem
return ob[index]
IndexError: list index out of range
check my summary.py please:
## https://github.com/maattdiy/home-assistant-config
## Resources:
## https://home-assistant.io/components/python_script/
## https://home-assistant.io/docs/configuration/state_object/
debug = False
show_badges = True
show_card = True
group_count = 0
group_desc = ""
summary = ""
idx = 0
##################################################
## Group count (people and devices)
##################################################
# Entities summary by group name
groups = ['group.family', 'group.audio', 'group.video', 'group.media_player' ]
groups_format = ['{} at home: {}', '{} in use: {}', '!{} offline: {}']
groups_filter = ['home', 'on|playing', 'off|not_home']
groups_badge = ['Home', 'In use', 'Alert']
groups_badge_pic = ['', '', 'bug']
groups_desc = ['Nobody in home', '', '']
groups_count = [0, 0, 0]
for group in groups:
group_count = 0
group_desc = ''
for entity_id in hass.states.get(group).attributes['entity_id']:
state = hass.states.get(entity_id)
filter = groups_filter[idx]
if (state.state in filter.split('|') or debug):
dt = state.last_changed
dt = dt + datetime.timedelta(hours=-2) # For time zone :( How to do native?
time = '%02d:%02d' % (dt.hour, dt.minute)
# If state changed in the past days show the date too
if dt.date() < datetime.datetime.now().date():
time = '{} {}'.format('%02d/%02d' % (dt.day, dt.month), time)
group_count = group_count + 1
group_desc = '{} {} ({}), '.format(group_desc, state.name, time)
# Final format for this group
if group_count > 0:
group_desc = groups_format[idx].format(group_count, group_desc[:-2])
groups_desc[idx] = group_desc
groups_count[idx] = group_count
idx = idx + 1
#hass.states.set('sensor.count_people_home', home_count, {
# 'unit_of_measurement': state.last_changed,
# 'friendly_name': home_desc
#})
##################################################
## Badges updates
##################################################
idx = 0
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 ''
hass.states.set(entity_id, groups_count[idx], {
'friendly_name': fname,
'unit_of_measurement': badge,
'entity_picture': picture,
'hidden': hidden
})
idx = idx + 1
##################################################
## Devices in use count
##################################################
domains = ['switch', 'media_player']
for domain in domains:
for entity_id in hass.states.entity_ids(domain):
show = False
state = hass.states.get(entity_id)
# Media players
if (state.state == 'playing'):
show = True
# Switchs with icons
if (state.state == 'on'):
## Only switchs with icons are relevants (ignore internal switchs). Find by tag "icon" in dictionary because "state.attributes.icon" didn't work
if (str(state.attributes).find("'icon'")) >= 0:
show = True
if (show):
if (inuse_desc.find(state.name + ', ') == -1):
#logger.info("state.attributes = " + str(state.attributes))
inuse_count = inuse_count + 1
inuse_desc = inuse_desc + state.name + ', '
if inuse_count > 0:
inuse_desc = str(inuse_count) + ' in use: ' + inuse_desc[:-2]
summary = summary + '\n ' + inuse_desc
##################################################
## Alarm clock
##################################################
alarms_prefix = ['alarmclock_wd', 'alarmclock_we']
alarms_wfilter = ['1|2|3|4|5', '6|7']
alarms_desc = ''
idx = 0
for entity_id in alarms_prefix:
state = hass.states.get('input_boolean.{}_enabled'.format(entity_id))
if (not state is None):
if (state.state == 'on'):
# Show the alarm for the next day
if (str(datetime.datetime.now().isoweekday()) in alarms_wfilter[idx].split('|')):
state = hass.states.get('sensor.{}_time_template'.format(entity_id))
alarms_desc = '{}{}, '.format(alarms_desc, state.state)
idx = idx + 1
if (alarms_desc == ''):
alarms_desc = '!Alarm clock is disabled'
else:
alarms_desc = 'Alarm clock at ' + alarms_desc[:-2]
##################################################
## Profile/mode
##################################################
#state = hass.states.get('input_select.ha_mode')
#if (state.state != 'Normal'):
# summary = summary + '\n ' + '* ' + state.state + ' profile is activated'
# hass.states.set('sensor.profile_badge', '', {
# 'entity_picture': '/local/profiles/{}.png'.format(state.state.lower()),
# 'friendly_name': ' ',
# 'unit_of_measurement': 'Mode'
# })
##################################################
## Summary updates
##################################################
for group_desc in groups_desc:
if (group_desc != ''):
summary = '{}{}\n'.format(summary, group_desc)
summary = '{}\n{}\n{}'.format(summary, alarms_desc, profile_desc)
if show_card:
hass.states.set('sensor.summary', summary, {
'custom_ui_state_card': 'state-card-value_only'
})
# People badge update
#hass.states.set('sensor.people_badge', group_count, {
# 'friendly_name': ' ',
# 'unit_of_measurement': 'Home',
#})
# In use badge update
#hass.states.set('sensor.inuse_badge', inuse_count, {
# 'friendly_name': ' ',
# 'unit_of_measurement': 'In use'
#})
# Summary sensors update
#hass.states.set('sensor.summary', summary, {
# 'custom_ui_state_card': 'state-card-value_only'
#})
took out some empty spaces in that line, and now the error shifts one up:
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 36, in <module>
File "/usr/lib/python3.6/site-packages/RestrictedPython/Eval.py", line 35, in default_guarded_getitem
return ob[index]
IndexError: list index out of range