Templates - select attributes

Hi out there:

I want to display all entities that have a “power” and “measurement” attribute so I can list the entities with power consumption.

´´ - name: “Energi consumption”
state: >
{%- set ns = namespace(sensors=) -%}
{%- for state in states
|selectattr(‘attributes.device_class’, ‘eq’, ‘power’)
|selectattr(‘attributes.state_class’, ‘eq’, ‘measurement’) -%}
{% set ns.sensors = ns.sensors + [ {‘name’: state.name , ‘W’: state.state} ] %}
{%- endfor -%}
{% set energi = ns.sensors | sort(attribute=‘name’) %}
{%- set ns = namespace(energi=‘’) -%}
{%- for state in energi -%}
{% set ns.energi= ns.energi + (state.name ~ ‘’ ~ state ~ “\n”) %}
{%- endfor -%}
{% if ns.energi | count > 0 %}
{{ ns.energi | truncate(255, true, ‘…’) }}
{% else %}
{{ ‘No energi’ }}
{% endif %}´´´´

it seems only to find 6 out of 18 and I have an issue with the name / power in the

´´´{% set ns.sensors = ns.sensors + [ {‘name’: state.name , ‘W’: state.state} ] %}´´´

Actually I only want state.name and state.state, but without the ‘name’: and the ‘W’: I get an error

Thks
Marinus

Please read this How to help us help you - or How to ask a good question

And reformat the YAML as described.

Is it because you’re trying to put more than 255 characters into the Template Sensor’s state value?

Your template is already designed to eliminate anything that exceeds 255 characters. Eighteen entity names and values might exceed 255 characters.

{{ ns.energi | truncate(255, true, ‘…’) }}
               ^^^^^^^^

I do not recommend using a Template Sensor for this application. I suggest you consider using a Markdown Card to display the list of entities instead of attempting to stuff them into a Template Sensor’s state value.

Copy-paste the following template into the Template Editor and confirm it reports the desired information.

{% set entities = states
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.state_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'power')
  | selectattr('attributes.state_class', 'eq', 'measurement')
  | sort(attribute='name') | list -%}
{% for e in entities -%}
{{ e.name}} {{ e.state }} W
{% endfor %}

If it’s satisfactory, use the template in a Markdown card.

1 Like

@123
wouv, you did not only solve my issue with finding all entities, you also solved my next issue, why it only shows some of the enteties and not all. I thought the truncate, was to make sure, that the lines would not become to long, had not understood, it was total lines.

Great thanks a lot, learned another good thing today :slight_smile:
Marinus

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Thks, i though I had pressed solution, but will do it again

You marked your own post with the Solution tag (again).

The purpose of the tag is to direct other users to instructions that solve the problem. That would be my post here which explains why you aren’t seeing all the data and provides with you with a solution.