Maintaining a list of recent users with browser_mod

I’ve created an entity that keeps track of who is using my HA instance but want to maintain a list of the most recent users. This lists users currently logged on together with when their entity was last updated:

          {% set devices = integration_entities('browser_mod')|select('contains','browser_useragent')|list -%}
          {% set ns= namespace(output = '') -%}
          {% for dev in devices -%}
            {% set out = dev.split('agent')[-2] -%}
            {% if states(out) != 'unavailable' -%}
              {% set ns.output = dev.split(".")[1].split("_browser")[-2] ~'/'~ states(out) ~ states[out].last_updated.strftime('(%d%b%H:%M)') ~ ', ' ~ ns.output -%}
            {% endif -%}
          {% endfor -%}  
          {{ ns.output|truncate }}

… which gives:-

dji_laptop_firefox2/RNLI(11Nov16:32), dji_xps15_chrome/David Inwood(11Nov16:36),

I had imagined that I could add new users to the list (ie ones that were not already in the list). However I just get “null” from this:

          {% set devices = integration_entities('browser_mod')|select('contains','browser_useragent')|list %}
          {% set ns= namespace(output = '') %}
          {% for dev in devices -%}
            {% set out = dev.split('agent')[-2] -%}
            {% set inst, existing = states(out), this.attibutes.recents -%}
            {% if inst != 'unavailable' -%}
              {% if inst not in existing -%}
                {% set ns.output = ns.output ~ dev.split(".")[1].split("_browser")[-2]~states[out].last_updated.strftime('(%d%b%H:%M)')~', ' -%}
              {% endif -%}
            {% endif -%}
          {% endfor -%}  
          {{ (ns.output~this.attibutes.recents)|truncate}}

Can anyone help me understand what is going wrong?

It’s probably bad form to answer your own question, but anyway… It was caused by this.attibutes.recents looking like an array so inst not in this.attibutes.recents was trying to iterate over the array, which isn’t allowed. Solved by turning it explicitly into a string:

{% set inst, existing = states(out), this.attibutes.recents|string -%}
1 Like

It is not, and good job for coming back and posting a solution to your own problem!