Integration_entities inconsistently listing entities

I’ve been using integration_entities() to list, select & order entities since reading How to enumerate entities belonging to an integration? - #35 by 123. Yesterday it stopped listing the entire set belonging to the integration - as far as I’m aware no changes were made to the system at that time. Up to that time, putting the code into developer tools said it was listening to a credible list of entities, since then, just a handful. I can’t find any relevant errors in the logs. The yaml I’m using is this:

{{integration_entities('browser_mod')|select('contains','browser_visibility')|expand|sort(attribute='last_changed')|last }}

The link above shows some other techniques, but as developer tools says it is listening to ALL state changed events, I’m worried about the drain on resources.
Is there an efficient and reliable method of finding the most recent sensor in this integration that has been updated?

I think you’re looking at the wrong function causing your problems. Integration entities simply outputs entity registry information, which is static after startup unless you’re changing entities around. If that function actually changed, then you have an issue with the integration, not the function integration_entities.

Thanks for looking at this. I don’t really understand though; it has seemed in the past to list all the entities in the browser_mod integration, whether or not they were created prior to the last startup and it is no longer behaving the same way, it appears. The dynamic information is .last_changed is obtained for each entity id in the list - this bit works, it just doesn’t get a complete list of all the integration’s entities. It appears to be behaving as if it can only find those which have been updated since last startup, maybe? I’ve just double checked, and it does list entities created since last startup.
In any event, it clearly isn’t doing what I expected, but is there another way to get all the device id’s which I can sort to find the most recently updated?
I’ve got this code which gets the most recently changed (at the expense of listening to all state changed events), but I can’t work out how to get the entity id, filter it to exclude certain ones (containing my initials) and I do worry about how much load it imposes.

         {%- set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq',None) | list -%}
         {%- set ns = namespace(entities = []) -%}
         {%- for device in devices -%}
           {%- set ids = device_attr(device, 'identifiers') | list | first | default -%}
           {%- if ids[0] == 'browser_mod' -%}
             {%- set this = ('sensor.'~ids[1] | slugify~'_browser_visibility') -%}
             {%- if states[this] not in (none,undefined) -%}
               {%- set thistime = states[this].last_changed  -%}
               {%- set ns.entities = ns.entities + [thistime] -%}
             {% endif -%}
           {%- endif -%}
         {%- endfor -%}
         {% set lastone = ns.entities|sort|last %}
{{lastone}}

It likely still does. What does {{ integration_entities('browser_mod') }} output?

Nothing immediately after startup, then an increasing list as each browser_user uses my HA, but only in developer tools. In the sensor it returns unavailable until I reload the yaml for templates. If I could overcome this behaviour I could continue to use integration_entitues.

are there error’s in your logs? If yes, correct the errors.

I’m wagering it’s the last line, add a default

         {% set lastone = ns.entities|sort|last|default %}

I now remember I’ve read that elsewhere, but what does default default to?

returns none if the list is empty

Thanks. Do you know why my sensor gets stuck as unavailable after startup, even after browser_user activity makes the same code work in developer tools?

    - name: browser_users_last_viz
      unique_id: browser_users_last_viz
      state: >
         {% set lastone = integration_entities('browser_mod')|select('contains','browser_visibility')|reject('contains','dji')|expand|sort(attribute='last_changed')|last|default %}
         {{ lastone.name.split(' Browser')[-2] ~' /'~ lastone.state[0] ~';'~lastone.last_changed.isoformat()[0:19] }}

you have no protection when the list is empty.

{% if lastone %}
  {{ lastone.name.split(' Browser')[-2] ~' /'~ lastone.state[0] ~';'~lastone.last_changed.isoformat()[0:19] }}
{% endif %}

Thanks petro. I’ve implemented that & tested it for one startup. It did not go unavailable, so a success! However it seemed to keep the last browser_id this time, so I’m still puzzled. I’ll let it run & see what happens.
I am deeply grateful to all you wonderful people who look at our problems. I’m exploring at (or beyond as it seems) the limits of my capability!

From your comments and it’s behavior, I’m thinking integration_entities is not the right way to achieve my ultimate objective, which is to trigger a sensor when any sensor which ends with “_browser_visibility” changes state (ie sensor.*_browser_visibility). Ideally the technique would allow me to retrieve the state, time and the ID of the sensor that had changed. Do you have any suggestions that I could explore?