Hey everyone! I wanted to share a project I’ve been working on to improve the air quality monitoring in my smart home. I’ve been using the AirNow API to get real-time AQI data, but I wanted a more visual way to represent the data. After some research and experimentation, I came up with a template sensor that converts AQI values into color-coded alerts. Here’s how I did it:
The Goal
I wanted a sensor that would display a color corresponding to the AQI level. Green for good air quality, yellow for moderate, orange for unhealthy for sensitive groups, and so on. This way, I can quickly glance at my dashboard and know the air quality status without having to check the exact AQI number.
The Setup
I used the template sensor platform in Home Assistant. The sensor uses the AirNow API to get the AQI value and then applies a series of conditional statements to assign the appropriate color. Here’s the code I used:
yaml
- platform: template
sensors:
aqi_color:
friendly_name: AQI Color
value_template: >
{% set aqi = states(‘sensor.airnow_aqi’)|int %}
{% if aqi <= 50 %}
Green
{% elif aqi <= 100 %}
Yellow
{% elif aqi <= 150 %}
Orange
{% elif aqi <= 200 %}
Red
{% elif aqi <= 300 %}
Purple
{% else %}
Maroon
{% endif %}
How It Works
- The sensor pulls the AQI value from my AirNow sensor.
- It converts the value to an integer.
- It assigns a color based on the AQI range.
- The color is displayed on my dashboard.
The Result
Now, whenever the AQI changes, the sensor updates and displays the corresponding color. It’s been really helpful for quickly assessing the air quality, especially during wildfire season when the AQI can change rapidly.
Tips for Others
- Make sure your AQI sensor is properly configured and returning accurate readings.
- Test the sensor with known AQI values to ensure the color assignments are working correctly.
- Consider adding a notification rule to alert you when the AQI reaches a certain level.
I hope this helps someone looking to enhance their air quality monitoring setup! Let me know if you have any questions or suggestions for improvement. ![]()