Building Template Based on Attributes

Context:
I’m running Home Assistant (as well as a number of other apps) on TrueNAS Scale.
The TrueNAS integration surfaces a binary_sensor for each deployed app in Home Assistant with several extended attributes, such as “Update available”.
I’m trying to build a single, dynamic, binary sensor to flag when any deployed app has an update available.

Code
For a single app, the following:
{{ states.binary_sensor.truenas_apps_home_assistant.attributes["Update available"] }}
Returns True/False based on the value of “Update available” (great!)

I’ve then expanded this logic out to search for any binary sensor where the entity_id starts with “truenas_apps_” as follows:

{{
states.binary_sensor
 | selectattr('entity_id', 'search', 'truenas_apps_')
 | selectattr('Update available', 'eq', true)
 | list
 | count > 0
}}

This always returns False

Further testing explains why:

{{
states.binary_sensor
 | selectattr('entity_id', 'search', 'truenas_apps_')
 | map(attribute='Update available')
 | list
}}

This simply returns the following array: [Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined]

It seems that the issue relates to referencing an attribute with a space in the name. I’ve tried replacing the space with an _ but it makes no difference.

I’ve done some digging around and can’t seem to find a solution. Any suggestions for how I might get this working would be gratefully received.

Try this version.

{{ states.binary_sensor
  | selectattr('entity_id', 'search', 'truenas_apps_')
  | selectattr('attributes["Update available"]', 'eq', false)
  | list | count > 0
}}

I’ve just given that a try and still getting the same response.

I updated the “test” template as follows and still receiving an array full of “Undefined”

{{
states.binary_sensor
 | selectattr('entity_id', 'search', 'truenas_apps_')
 | map(attribute='attributes["Update available"]')
 | list
}}

Then there’s something else wrong.

That’s invalid because you have specified a non-existent attribute for map filter. Don’t confuse Jinja2’s use of the word “attribute” with Home Assistant’s “attributes”.

Go to Developer Tools → States, find one of binary_sensors whose attributes has an Update available member, post a screenshot of it (a screenshot containing the Entity, State, and Attributes column).

I have discovered the error in what I had suggested. Surprisingly, it appears that selectattr doesn’t work with bracket notation.

The first example works whereas the second one doesn’t.

{{ states.automation
| selectattr('attributes.current', 'eq', 0)
| list | count }}

{{ states.automation
| selectattr('attributes["current"]', 'eq', 0)
| list | count }}

I’ll investigate another way to do it.

1 Like
{% set ns = namespace(qty=0) -%}
{% for x in states.binary_sensor | selectattr('entity_id', 'search', 'truenas_apps_')
    if x.attributes['Update available'] == true -%}
  {% set ns.qty = ns.qty + 1 -%}
{% endfor -%}
{{ ns.qty }}

For your specific application, replace the last line with:

{{ ns.qty > 0 }}

Here’s an alternative that doesn’t use a for-loop. I don’t have any entities whose attributes contain a space in their name therefore I can’t test it on my system. I know it works for names with no spaces.

{{ states.binary_sensor
   | selectattr('entity_id', 'search', 'truenas_apps_')
   | map(attribute='attributes')
   | map(attribute='Update available')
   | select('eq', true)
   | list | count > 0 }}

I’ve just tested with the alternative option you suggested and it works absolutely perfectly.
I guess it kind of makes sense as it needs to map the attributes collection first so that you can then query an individual attribute.

My NOC dashboard is now functioning exactly as I’d hoped so thank you for the assistance, really appreciated. Virtual beers on me :beers:

1 Like

Glad to hear it!

Before you go, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that this topic has been solved. This helps other users find answers to similar questions.

You should start an issue on the TrueNAS integration’s git so the devs know that their use of non-slugified attribute IDs is problematic.