Python_script: how/where to add time last_changed

rewriting some of the python scripts to keep thing smaller, simpler and more maintainable ive conjured up this little family counter. Only thing id like to add is the time last changed after each entity_id, but im a bit confused where to put the code…the earlier setup of a related script doesn’t apply since this is declared differently (function).

please have a look with me?

familyEntities = 'group.family'

def CountMyEntities(hass, entity_list, entity_state):
    cnt = 0

    for entity_id in hass.states.get(entity_list).attributes['entity_id']:
        state = hass.states.get(entity_id)
        if state.state == entity_state:
            cnt = cnt + 1

    return cnt

count_home = CountMyEntities(hass, familyEntities, 'home')
count_not_home = CountMyEntities(hass, familyEntities, 'not_home')


def NameMyEntities(hass, entity_list, entity_state):
    names = []
     for entity_id in hass.states.get(entity_list).attributes['entity_id']:
        state = hass.states.get(entity_id)
        dt = state.last_updated + datetime.timedelta(hours= 2)
        time = '%02d:%02d' % (dt.hour, dt.minute)
        if state.state == entity_state:
            names.append(hass.states.get(entity_id).attributes['friendly_name'])

    return names

names_home = NameMyEntities(hass, familyEntities, 'home')
names_not_home = NameMyEntities(hass, familyEntities, 'not_home')

familyCount = count_home + count_not_home
whichIcon = 'mdi:account-check' if count_home > 0 else 'mdi:account-off'
#status = count_home #count_home if count_home > 0 else 0

hass.states.set('sensor.family_home', count_home, {
    'friendly_name': "Family Home?",
    'Home': ', '.join(names_home),
    'Not Home': ', '.join(names_not_home),
    'icon': whichIcon,
    'at_home': count_home,
    'not_home': count_not_home,
    'family_count': familyCount
     })

id like the names to be formatted like this somehow:

names = '{} : ({}), '.format( state.name, time)

after some experimenting, found this to be working alright:

if state.state == entity_state:
    name = '{} ({})'.format(state.attributes['friendly_name'], time)
    names.append(name)

as in the other mentioned script, the difference between last_updated and last_changed seems unclear. And instead of format(state.attributes['friendly_name'], time) I can use format(state, time) without any difference in the final listing.

So here is the complete script, if corrections are to be made in the code, please don’t hesitate…;-)) Id specifically would like to know whether I could further compress the 2 functions into 1, and maybe even use a variable (filter?) for the home and not_home, which I now semi-hardcode

    ##########################################################################################
    # family_home.py, based on Whats_on.py, previously known as Anything_on...
    #
    # interrelated with the Summary.py, which doesn't show the entities per badge (yet)
    # modified and customized by https://community.home-assistant.io/u/Mariusthvdb/summary
    # big hand in Python by https://community.home-assistant.io/u/petro/summary
    # tbd: timezone correction a bit of a challenge, solved with a little hack (=2)
    ##########################################################################################

    familyEntities = 'group.family'

    def CountMyEntities(hass, entity_list, entity_state):
        cnt = 0
        for entity_id in hass.states.get(entity_list).attributes['entity_id']:
            state = hass.states.get(entity_id)
            if state.state == entity_state:
                cnt = cnt + 1

        return cnt

    count_home = CountMyEntities(hass, familyEntities, 'home')
    count_not_home = CountMyEntities(hass, familyEntities, 'not_home')


    def NameMyEntities(hass, entity_list, entity_state):
        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= 2)
            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')

familyCount = count_home + count_away
whichIcon = 'mdi:account-check' if count_home > 0 else 'mdi:account-off'
#status = count_home #count_home if count_home > 0 else 0

hass.states.set('sensor.family_home', count_home, {
    'friendly_name': "Family Home?",
    'home': ', '.join(home),
    'away': ', '.join(away),
    'icon': whichIcon,
    'count_home': count_home,
    'count_away': count_away,
    'family_count': familyCount
     })

You should be able to use show_last_changed. See an example here in my repo.

thanks, but that shows the last_changed of the sensor, and I was looking for the last_changed of the individual items, which I now have.

Strang thing though, that the sensors last_changed isn’t in line with the last_changed of the last changed entity…

another thing: Why cant we customize the sensor through a custom_ui template?

ive tried these but the icon wont change color:

sensor.family_home:
  templates:
    icon_color: >
      {% if state === '0' %} 'rgb(128, 128, 128)'
      {% elif state === '1' %} 'rgb(248, 248, 255)'
      {% elif state === '2' %} 'rgb(30, 144, 255)'
      {% elif state ==='3' %} 'rgb(0, 128, 0)'
      {% elif state === '4' %} 'rgb(255, 255, 0)'
      {% elif state === '5' %} 'rgb(255, 165, 0)'
      {% elif state === '6' %} 'rgb(255, 165, 0)'
      {% else %} 'rgb(117, 21, 120)'
      {% endif %}

#      {% if is_state('sensor.family_home', '0') %} 'rgb(128, 128, 128)'
#      {% elif is_state('sensor.family_home','1') %} 'rgb(248, 248, 255)'
#      {% elif is_state('sensor.family_home','2') %} 'rgb(30, 144, 255)'
#      {% elif is_state('sensor.family_home','3' )%} 'rgb(0, 128, 0)'
#      {% elif is_state('sensor.family_home','4') %} 'rgb(255, 255, 0)'
#      {% elif is_state('sensor.family_home','5') %} 'rgb(255, 165, 0)'
#      {% elif is_state('sensor.family_home','6') %} 'rgb(255, 165, 0)'
#      {% else %} 'rgb(117, 21, 120)'
#      {% endif %}

43

00