Jinja selectattr() - Access state object extra attributes

Is it possible to access an entity’s defined extra attributes using the jinja selectattr() function?

I’m trying to create a sensor that will count and list open UI browser windows using sensors created by browser_mod integration.

    remote_ui_connected:
      entity_id: sensor.time
      friendly_name: Browser UI Connected
      unit_of_measurement: Browsers
      icon_template: mdi:laptop
      value_template: "{{ states.sensor|selectattr('type','eq','browser_mod')|list|count }}"
      attribute_templates:
        browsers: "{{ states.sensor|selectattr('type','eq','browser_mod')|map(attribute='entity_id')|join('\n- ') }}"
1 Like

Templates only return strings. No way around this. Your best option is to comma separate the result so you can easily parse it if you want to use it in other areas.

    remote_ui_connected:
      entity_id: sensor.time
      friendly_name: Browser UI Connected
      unit_of_measurement: Browsers
      icon_template: mdi:laptop
      value_template: "{{ states.sensor|selectattr('type','eq','browser_mod')|list|count }}"
      attribute_templates:
        browsers: "{{ states.sensor|selectattr('type','eq','browser_mod')|map(attribute='entity_id')|join(',') }}"

then whenever you want to get an entity_id out…

{% set browsers = state_attr('sensor.remote_ui_connected', 'browsers').split(',') %}

I understand the template will return a string, I don’t think that is the issue here. It just needs to return how many UI browser windows are open. It’s only for display in the UI.

This is the line that isn’t working.

It returns 0. I have verified there is a sensor with this attribute so it should return at least 1. I believe the issue here is that “type” isn’t one of the normal state object attributes (name, domain, entity_id etc.) It works as expected if I test for an entity_id or any of the normal state attributes instead of any of the “extra” attributes defined by browser_mod.

Thanks for having a look tho!

1 Like

Oops just a typo
{{ states.sensor|selectattr('attributes.type','eq','browser_mod')|list|count }}

4 Likes

Always the man with the plan. Works perfect. Thank you! :slightly_smiling_face: