Skip to content

Instantly share code, notes, and snippets.

@ussjoin
Last active November 4, 2025 21:08
Show Gist options
  • Select an option

  • Save ussjoin/8b130c3ff05b6f5bf55bbdcd8c7c6195 to your computer and use it in GitHub Desktop.

Select an option

Save ussjoin/8b130c3ff05b6f5bf55bbdcd8c7c6195 to your computer and use it in GitHub Desktop.
# AQI Calculation from https://www.airnow.gov/sites/default/files/2020-05/aqi-technical-assistance-document-sept2018.pdf p14
# And updated by https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
# This also shows how to use references in HA's templating language to include this on other PM2.5 sensors
- name: "Nursery AQI"
attributes:
particulates: "{{ states('sensor.pm2_5_2') }}"
unique_id: "sensor.nursery_aqi"
<<: &aqi_from_pm25
unit_of_measurement: "aqi"
icon: mdi:blur
state: >
{% set pm = ((this.attributes.particulates| default(-1, false)) | float) %}
{% if pm <= 9.0 %}
{% set low = 0.0 %}
{% set high = 50.0 %}
{% set lowpm = 0.0 %}
{% set highpm = 9.0 %}
{% elif pm <= 35.4 %}
{% set low = 50.0 %}
{% set high = 100.0 %}
{% set lowpm = 9.1 %}
{% set highpm = 35.4 %}
{% elif pm <= 55.4 %}
{% set low = 101.0 %}
{% set high = 150.0 %}
{% set lowpm = 35.5 %}
{% set highpm = 55.4 %}
{% elif pm <= 125.4 %}
{% set low = 151.0 %}
{% set high = 200.0 %}
{% set lowpm = 55.5 %}
{% set highpm = 125.4 %}
{% elif pm <= 225.4 %}
{% set low = 201.0 %}
{% set high = 300.0 %}
{% set lowpm = 125.5 %}
{% set highpm = 225.4 %}
{% else %}
{% set low = 301.0 %}
{% set high = 500.0 %}
{% set lowpm = 225.5 %}
{% set highpm = 500.0 %}
{% endif %}
{% set i_diff = high-low %}
{% set pm_diff = highpm-lowpm %}
{% set conc = pm-lowpm %}
{% if pm == -1.0 %}
{{ -1 }}
{% else %}
{{ ((conc * ((i_diff) / pm_diff)) + low) | round(0) }}
{% endif %}
- name: "Family Room AQI"
unique_id: "sensor.family_room_aqi"
<<: *aqi_from_pm25
attributes:
particulates: "{{ states('sensor.ikea_of_sweden_vindstyrka_particulate_matter_2') }}"
- name: "Living Room AQI"
unique_id: "sensor.living_room_aqi"
<<: *aqi_from_pm25
attributes:
particulates: "{{ states('sensor.ikea_of_sweden_vindstyrka_particulate_matter') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment