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_updated + 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)
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
works fine. and shows me all light that are ‘on’, with their respective last_changed time.
However, Ive had to turn on allow_unreachable: true for my Hue lights, which makes unreachable lights show as ‘on’ if they are ‘on’ in the Hub. That is a bit silly, because now all my outside night lights show ‘on’ in my setup, while in fact they are ‘unreachable’.
So I’d like to extend the above python with an if clause for a sub set of lights for which the reachable attribute is set in a binary_sensor and if that binary_sensor is ‘off’, it should be added to the list (desc in the python)
the binaries follow the naming of the lights of course:
try this (unchecked! posting the whole script to avoid any copy/paste issues - changed only desc bit)
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_updated + 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)
binary_sensor_id = 'binary_sensor.{}_reachable'.format(state.object_id)
binary_sensor = hass.states.get(binary_sensor_id)
if binary_sensor is not None and binary_sensor.state == 'off':
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
thanks!
however, it should also count = count + 1 for those lights that are not checked in the list of binary_sensors. As said, that is only a subset of the lights.
This only adds if there is a binary_sensor set…