for checking which and how many lights are on, I use this python script, with the entity selecting like this:
for entity_id in hass.states.get(group).attributes['entity_id']:
state = hass.states.get(entity_id)
which is quite the default way, nothing spectaculair here…
full script below.
But, this is done with the group.all_lights_only
, that lists all lights individually, so the entity_id’s in that group are in fact the individual lights.
I have changed my lights groups setup though, so that I nest a few groups, of which the main nestings are:
all_lights_only:
name: All lights only
icon: mdi:lightbulb-outline
entities:
- group.all_inside_lights
- group.all_outside_lights
all_inside_lights:
name: All inside lights
icon: mdi:lightbulb-outline
entities:
- group.main_inside_lights
- group.guest_inside_lights
- group.living_ceiling_spots
all_outside_lights:
name: All outside lights
icon: mdi:flood-light
entities:
- group.outside_flood_lights
- group.terrace_outdoors_spots
which of course doesn’t work with the current python_script, because it now sees one of these groups as being the entity_id. I now would need to iterate these groups, which consist of several groups, (that contain groups again), containing lights.
What would be the best way to do so in Python?
I’ve started with this, explicitly listing all groups (to make it easier and not have to iterate groups that contain yet more groups)
light_groups = ['group.main_inside_lights','group.guest_inside_lights',
'group.living_ceiling_spots','group.outside_flood_lights',
'group.terrace_outdoors_spots']
for group in light_groups:
for entity_id in hass.states.get(group).attributes['entity_id']:
light_ids.append(entity_id)
for entity_id in light_ids: #hass.states.get(group).attributes['entity_id']:
state = hass.states.get(entity_id)
this now does seem to work. But it feels inadequate… As worded in
This is not as elegant as the expand() function we now can easily use in Jinja…
Would appreciate your help here,
thanks for having a look!
dt_prevstate = None
utc_offset = hass.states.get('sensor.utc_offset').state
timeDifference = float(utc_offset)
group = 'group.all_lights_only'
fname = 'Lights'
on_format = 'Lights on: {} -- {}'
off_format = 'No lights on since '
filter = 'on'
pic = 'lamp_off|lamp'
icon = 'mdi:lightbulb'
# min_show = 0
count = 0
desc = ''
for entity_id in hass.states.get(group).attributes['entity_id']:
state = hass.states.get(entity_id)
if (state.state == filter or debug):
dt = state.last_changed + datetime.timedelta(hours= timeDifference)
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)
# check if hue lights are 'reachable' is needed because of allow_unreachable: true
binary_sensor_id = 'binary_sensor.{}_reachable'.format(state.object_id)
binary_sensor = hass.states.get(binary_sensor_id)
if binary_sensor is None or binary_sensor.state == 'on':
count = count + 1
desc = '{} {} ({}), '.format(desc,state.name,time)
else:
if (dt_prevstate is None):
dt_prevstate = state.last_changed
else:
if (not state.last_changed is None):
if (state.last_changed > dt_prevstate):
dt_prevstate = state.last_changed
# Final format for the group
picturelist = pic.split('|')
if (count == 0):
desc = off_format
picture = picturelist[0]
# If there is none 'On/Home' state in group, show 'since'
if (desc.find(' since ') > 0):
dt = dt_prevstate + datetime.timedelta(hours= timeDifference)
desc = '{} {}'.format(desc,'%02d:%02d' % (dt.hour,dt.minute))
else:
desc = on_format.format(count, desc[:-2])
picture = picturelist[1]
##########################################################################################
# Create sensor and badge
##########################################################################################
badge_id = 'sensor.{}_badge'.format(fname.replace(' ', '').lower());
sensor_id = 'sensor.{}_summary'.format(fname.replace(' ', '').lower());
picture = '/local/badges/{}.png'.format(picture)
# badge
hass.states.set(badge_id, count, {
'friendly_name': '',
fname: desc,
'unit_of_measurement': fname + ': ' + str(count),
'entity_picture': picture,
# 'icon': icon,
})
# sensor
hass.states.set(sensor_id, count, {
'friendly_name': fname,
fname: desc,
# 'unit_of_measurement': badge + ': ' + str(count),
'entity_picture': picture,
# 'icon': icon,
})