Hello,
I am currently creating a template sensor that converts pollutant concentrations into an European Air Quality Index level. Since the levels are defined per pollutant, I wanted to have the level descriptions as an attribute in a template. However, this doesn’t work as I want due to an attribute error.
This is the specific code that is not working:
- sensor:
- name: European Air Quality Index
state: >-
{{ this.attributes.level_descriptions[this.attributes.eaqi_value_overall] }}
attributes:
level_descriptions: >-
{{ [ 'Good', 'Fair', 'Moderate', 'Poor', 'Very poor', 'Extremely poor'] }}
This is what I see in the logs:
Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:329
TemplateError('UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'level_descriptions'') while processing template 'Template("{{ this.attributes.level_descriptions[this.attributes.eaqi_value_overall] }}")' for attribute '_attr_native_value' in entity 'sensor.european_air_quality_index'
I don’t understand why there is an issue with one attribute (level_descriptions
) but not the other (eaqui_value_overall
) since the code below does work:
- sensor:
- name: European Air Quality Index
state: >-
{{ [ 'Good', 'Fair', 'Moderate', 'Poor', 'Very poor', 'Extremely poor']
[this.attributes.eaqi_value_overall] }}
Also, when looking at the developer template tool, the following code works perfectly fine as well and gives me the right result:
{{ states.sensor.european_air_quality_index.attributes.level_descriptions
[states.sensor.european_air_quality_index.attributes.eaqi_value_overall] }}
Do you have an idea what might cause this issue?
Thanks in advance,
Best regards,
Raphaël