(Still struggle with) How to Show all Attributes

I have lots of entities that have lots of attributes each and I continue to add more such entities.

I’ve learned about creating template sensors for attributes, but there’s got to be an easier way.

I found this:

How to wildcard template entities? - Configuration - Home Assistant Community (home-assistant.io)

but I can’t get it to work.

I am able to make this line of code work in a card:

{{ states.binary_sensor | selectattr('object_id', 'match', 'mikrotik_255_')
   | map(attribute='entity_id') | list }}

but what it does is list all the entities that begin with “mikrotik_255_”

Interestingly, the above code shows (among other entities):

binary_sensor.mikrotik_255_hex_s_ups

But, this code:

{{ states.binary_sensor | selectattr('object_id', 'match', 'mikrotik_255_s_')
   | map(attribute='entity_id') | list }}

does not produce any results. Adding the “s_” at the end of “mikrotik_255_” breaks it.

Regardless, what I really want is some simple (and simple enough for me to understand) code that I can use in a card that will show all the attribute’s names and their values by identifying a single entity.

Something like the following (I hope my pretend code doesn’t hurt the brains of real programmers too much):

{{ code.code.code.code."mikrotik_255_s_ups".code.code.code }}

attribution: Data provided by Mikrotik
name: ups1
offline_time: 0s
min_runtime: never
alarm_setting: immediate
model: CP1500PFCLCDa
serial: CXXLV2003186
manufacture_date: unknown
nominal_battery_voltage: unknown
runtime_left: 1h1m15s
battery_charge: 100
battery_voltage: 0
line_voltage: 124
load: 12
hid_self_test: unknown
device_class: power
friendly_name: Mikrotik-255 hEX S UPS

Thank you.

Wouldn’t you use entity_id instead of object_id in the match?
Hmm if @123 says it’s object_id, then it’s object_id - they know WAY more about Jinja and the Home Assistant state system than I do…

Copy-paste the following into the Template Editor and confirm it reports what you want.

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() -%}
{{ k }}: {{ v }}
{% endfor %}

You can use it in a Markdown card.

attributes is a python dict (dictionary) and so it has methods and properties that can be used to view/manipulate its contents.

1 Like

The reason is that the example you gave adds hex_s_ not just s_ behind it.

That is the tightest code ever!

Thank you so much.

Possible to include conditional code that only displays attributed that have a value (i.e., something other than “unknown”)?

1 Like

Good catch – thank you.

{% set entity_id_variable = 'climate.ecobee_c_thermostat' %}

{% for k, v in states[entity_id_variable].attributes.items() -%}
{% if v != 'unknown' %}{{ k }}: {{ v }}{% endif %}
{% endfor %}

How does this work? I can’t find any entities with attribute unknown to test it against.

Simply add the test to the line containing for like this:

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if v != 'unknown' -%}
{{ k }}: {{ v }}
{% endfor %}

Wow, works great.

I played a little with using the concepts and failed.

I tried:

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if k != 'line_voltage' -%}
{{ k }}: {{ v }}
{% endfor %}

and it reported everyone EXCEPT the attributed ‘line_voltage’

but, when I try:

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if k = 'line_voltage' -%}
{{ k }}: {{ v }}
{% endfor %}

It gives me an error.

My hope was that the conditional “if k =” would limit the attributes showing to the one named “line_voltage”

And from there I was hoping to use a wildcard, like:

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if k = '*voltage*' -%}
{{ k }}: {{ v }}
{% endfor %}

But (of course) that doesn’t work either.

Use two equal signs.

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if k == 'line_voltage' -%}
{{ k }}: {{ v }}
{% endfor %}

Ugh.

Thank you.

[Slinking away…]

Yes!

This also works.

And I can use TWO EQUAL signs and the ‘k’ variable as well.

Thank you!

Use the search filter.

{% for k,v in states.binary_sensor.mikrotik_255_hex_s_ups.attributes.items() if k is search('voltage') -%}
{{ k }}: {{ v }}
{% endfor %}

The search filter support regular expressions. Explaining regular expressions is beyond the scope of this topic but here are a few examples of its usage:

search('voltage') will match any string containing the string voltage.

search('^voltage') will match any string starting with the string voltage.

search('voltage$') will match any string ending with the string voltage.

2 Likes

I am so grateful!

Thank you.

1 Like