Select sensors by attribute value

tell me pliz design, how I can select those sensors, the specified attribute of which (its value) corresponds to the condition. In my case, the attribute value is in integer.

I tried this:

{% set count = states | selectattr("attribute.quality", 'gt', '10') %}

, but this rolls only with string values.

Assuming quality contains an integer or float value, remove the quotes from '10' (and it’s attributes.quality not attribute.quality). However, the template is fundamentally flawed because it examines all entities and will fail when it encounters the first entity that doesn’t have a quality attribute.

UndefinedError: ‘mappingproxy object’ has no attribute ‘quality’

You have to include a filter to select the entities that have a quality attribute.

{% set count = states | selectattr('attributes.quality', 'defined') | selectattr('attributes.quality', 'gt', 10) | list | count }}

In addition, if you know that only some sensor entities have the quality attribute, replace states with states.sensor to make the template a bit more efficient.

3 Likes

Hi, thank you for yours reply. To be exact I have ~ 30 sensors of stock.
And these sensors have the “profitLossPercentage” (in integer value) attribute. I tried the following kind of code, which shows nothing :frowning:

{% set count = states.sensor | selectattr('attributes.profitLossPercentage', 'defined') | selectattr('attributes.profitLossPercentage', 'gt', 10) | list | count %}

template only says: Result type: string
This template tracks the following state change events: Domain: sensor

oh sorry, i forgot

{{ count }}
{% set count = states.sensor | selectattr('attributes.profitLossPercentage', 'defined') | selectattr('attributes.profitLossPercentage', 'gt', 10) | list | count %}
{{ count }}

now there is a result: 8

You should know that it’s the custom of this community to assign the Solution tag to the first post, or most complete post, that identifies and resolves the stated problem.

In this case, you marked your own post as the Solution yet all you did was use what I had already suggested (minus the explanation). That’s not how the Solution tag is normally used.

1 Like

I really apologize for the inattention. I missed. now everything is correct! Thank you again very much!

1 Like