List all attributes that have a particular value

I have an entity that has these attributes:

{"moisture_status": "ok",
 "temperature_status": "ok",
  "conductivity_status": "Low",
  "illuminance_status": "ok",
  "humidity_status": null,
  "dli_status": "ok"}

I want to get all attributes that have value Low

I’ve tried {{states.plant.mint.attributes|selectattr('value', 'eq','Low')|list}}
but it didn’t work

i’m not adept enough to know how to do this w/o a for loop. feels like there’s got to be a way. hopefully someone smarter than me will jump in. however until then, this should do what you want.

{% set matching_keys = namespace(match=[]) %}
{% for key, value in states.plant.mint.attributes.items() %}
  {% if value == 'Low' %}
    {% set matching_keys.match = matching_keys.match + [key] %}
  {% endif %}
{% endfor %}
{{ matching_keys.match}} 

now someone please school us on how to do it w/ a filter…

You can use the items() method on a dictionary to turn it into a list of tuples then use the indices as attributes to select by and map by:

{{ (states.plant.mint.attributes).items()
| selectattr(1, 'eq', 'Low') | map(attribute=0) | list }}
1 Like