How to add an extra filter in python script to rule out a subset of lights

using this python

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:

binary_sensor.backdoor_outdoors_reachable  light.backdoor_outdoors
binary_sensor.bureau_marte_reachable  light.bureau_marte
binary_sensor.driveway_reachable light.driveway
binary_sensor.garden_backyard_reachable 
binary_sensor.garden_terrace_reachable
binary_sensor.gate_outdoors_reachable
binary_sensor.mobile_reachable
binary_sensor.porch_outdoors_reachable

Please help me adding the logic to rule those out in the ‘on’ selection.

dont add to the list if

'binary_sensor.{}_reachable'.format(state.object_id) != 'on' 

to give you an idea of what I am looking for.
thanks for having a look and time if you would

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…

there you go, forgot about this:

if binary_sensor is None or binary_sensor.state == 'off':

cool! almost …:wink:

this does the trick:

        if binary_sensor is None or binary_sensor.state == 'on':

thanks a lot!

dude… it doesn’t play well with your initial request

code with binary_sensor.state == 'on' adds lights that have their binary_sensors on, but I’m afraid it’s not what you requested originally :wink:

Yes it is, I wanted to show the lights that are on an have reachable = ‘on’

I asked to not add lights with != ‘on’ …

double negation will kill you one day :wink:
and great that you sorted it out

1 Like

Thanks again. Working perfectly .
My night lights have gone ‘reachable’ now and are added to the list as desired !

Cool.

1 Like

just in time… :wink: