More complex attributes check than home/not_home in Python script

HI,

im using this script to see if my family members are home:

familyEntities = 'group.family'

and

def NameMyEntities(hass, entity_list, entity_state):
    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 state.state == entity_state:
            name = '{} ({})'.format(state.attributes['friendly_name'], time)
            names.append(name)

    return names

home = NameMyEntities(hass, familyEntities, 'home')
away = NameMyEntities(hass, familyEntities, 'not_home')

and the creation of the sensor:

hass.states.set('sensor.family_home', count_home, {
    'custom_ui_state_card': 'state-card-custom-ui',
    '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,
    'show_last_changed': 'true'
     })

which has worked perfectly for entities in the group.family with only states ‘home’ and ‘not_home’.

I am moving to use more complete/complex device_trackers in the group.family, such as @pnbruckner 's life360, which has more states than these 2 of course.

So I need to adapt the line

away = NameMyEntities(hass, familyEntities, 'not_home')

to something like

away = NameMyEntities(hass, familyEntities, not is 'home')

how can I do that? Obviously this doesn’t work. Please have a look?

You won’t be able to do it if this is inside a python_script. You’d need to be able to import operators.

So with that being said, you need to get crafty with your arguments.

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 state is not None and state.state != not_state:
            name = '{} ({})'.format(state.attributes['friendly_name'], time)
            names.append(name)

    return names
away = NameMyEntities(hass, familyEntities, 'not_home')
away = NameMyEntities(hass, familyEntities, not_state='home')

thank you!

i think im starting to understand what your doing, but it lists all family members in home, while the not_home members seem listed correctly.

stil have this:

home = NameMyEntities(hass, familyEntities, 'home')

elaborating on what your are suggesting, why would I need a ‘not_home’ if I already have a not_state=‘home’ ? the latter should include the former? If I take out one or the other, that doesn’t make a difference in the away listing

away = NameMyEntities(hass, familyEntities, 'not_home')

would return every entity that has the state ‘not_home’.

home = NameMyEntities(hass, familyEntities, 'home')

would return every entity that has the state ‘home’.

away = NameMyEntities(hass, familyEntities, not_state='home')

would return every state that is not equal to ‘home’.

home = NameMyEntities(hass, familyEntities, not_state='not_home')

would return every state that is not equal to ‘not_home’.


You specifically asked for the ability to do this:

The only way to do that is to use the not_state=‘home’

away = NameMyEntities(hass, familyEntities, not_state='home')

You also want to maintain the ability to use the normal function right?

home = NameMyEntities(hass, familyEntities, 'home')

this seems to work, only the away or not_home entities are listed under away.

well, not perse, but I wouldn’t know how the script would know what to list under home, if I didn’t use this? btw, taking it out errors out: name 'home' is not defined

right now, all entities are listed under home, (so including the ones that are not home)

I’m so confused as to what your goal is. You have this in your original script, in the first post on this topic:

So, one would think you want home and away. So with one function:

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 state is not None and state.state != not_state:
            name = '{} ({})'.format(state.attributes['friendly_name'], time)
            names.append(name)

    return names

You can now use these to lines to replace your original lines:

home = NameMyEntities(hass, familyEntities, 'home')
away = NameMyEntities(hass, familyEntities, not_state='home')

Of course you’ll get errors if you remove the home line if other things later on in the script require ‘home’. That’s why the changes were made to maintain the previous ability and the new ability.

that is correct, up to that moment, I was using entities with only home and away states. I was hoping I could use my entities that have more than these 2 states, namely their respective zones. These zones don’t fit into these states, and thus are left out when testing for home/not_home.

Hence my wish to somehow check for !=home…

I think I did what you repeated just now, but let me try again.

update

as before this lists all entities under Home, and the correct not_home entities under away

Yes, the not_state should get you that.

well, what can I say. Other than it’s not the not_home entities that are incorrect, its the fact that all entities, including the ones not_home, are listed as 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

typo

yes!
should have seen that myself of course, so sorry.

thank you!

(must confess none of the entities are in a zone right now, they’re all home or away :wink: but that will soon change, so I’ll be certain to report in a bit.)

update

all went well! thanks a bunch
if its all the same to you, please have a look at Expand on this notification filer automation