# Calculating AQI with Node-Red?

**URL:** <https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001>\
**Category:** General\
**Created:** [4 July 2021 09:31 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001 "2021-07-04T09:31:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt.L](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Matt.L](https://discourse.nodered.org/u/Matt.L)\
**Post date:** [4 July 2021 09:31 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/1 "2021-07-04T09:31:12Z")

</div>

I'm trying to see if there is either an existing node or example flow that can help me calculate the US AQI value from PM2.5 and PM10 readings.

I have an SDS011 particulate sensor connected to a NodeMCU that is then publishing the PM2.5 and PM10 values to my MQTT broker. I can subscribe to those in Node-Red. I'd like to turn those numbers into an AQI number.

Has anyone else done this? If so, is there a node I can add to my palette? Or does someone have an example flow I can adapt for my situation?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 July 2021 09:47 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/2 "2021-07-04T09:47:39Z")

</div>

To save us searching for that can you provide a link to how to calculate it?

---

<div class="post-metadata">

**Author:** ![Matt.L](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Matt.L](https://discourse.nodered.org/u/Matt.L)\
**Post date:** [5 July 2021 04:32 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/3 "2021-07-05T04:32:17Z")

</div>

That was one of the issues I was having when I first made my post, the equation(s) weren't posted at any of the air quality sites that Google kept turning up.

But it turns out that the wikipedia page is reasonably good on the subject.

> **[Air quality index](https://en.wikipedia.org/wiki/Air_quality_index)**
>
> Category (By country)
> An air quality index (AQI) is used by government agencies to communicate to the public how polluted the air currently is or how polluted it is forecast to become. Public health risks increase as the AQI rises. Different countries have their own air quality indices, corresponding to different national air quality standards. Some of these are the Air Quality Health Index (Canada), the Air Pollution Index (Malaysia), and the Pollutant Standards Index (Singapore).
> Computation ...

After some more thought, I figure I don't necessarily need to calculate the AQI. Instead, I think a gauge that has the 6 quality categories on it mapped to the PPM values on that wikipedia page will work provided I can find a gauge that supports 6 sectors and I can set a scale for each sector (though maybe something on a log scale might work too).

---

<div class="post-metadata">

**Author:** ![Matt.L](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Matt.L](https://discourse.nodered.org/u/Matt.L)\
**Post date:** [5 July 2021 07:53 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/4 "2021-07-05T07:53:20Z")

</div>

I have a solution using the wikipedia info to create some custom function nodes.  
The code isn't well formatted but it seems to be working OK.

```auto
//Calculate AQI from 2.5ppm data
//Formula taken from https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
var Aqi25mmBreakpoints = [0.0, 12.1, 35.5, 55.5, 150.5, 250.5, 350.5, 500.5];
var AqiIndexBreakpoints = [0, 51, 101, 151, 201, 301, 401, 500]
var ppm = parseFloat(msg.payload);
var AQI;

for (let i = 0; i < Aqi25mmBreakpoints.length; i++) {
    if(ppm < Aqi25mmBreakpoints[i]) {
        AQI = (((AqiIndexBreakpoints[i] - AqiIndexBreakpoints[i-1])/(Aqi25mmBreakpoints[i] - Aqi25mmBreakpoints[i-1]))*(ppm-Aqi25mmBreakpoints[i-1])) + AqiIndexBreakpoints[i-1]; 
        break;
    }
}

msg.payload = Math.trunc(AQI).toString();
return msg;

```

and

```auto
//Calculate AQI from 10ppm data
//Formula taken from https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
var Aqi10mmBreakpoints = [0.0, 55, 155, 255, 355, 425, 505, 604];
var AqiIndexBreakpoints = [0, 51, 101, 151, 201, 301, 401, 500]
var ppm = parseFloat(msg.payload);
var AQI;

for (let i = 0; i < Aqi10mmBreakpoints.length; i++) {
    if(ppm < Aqi10mmBreakpoints[i]) {
        AQI = (((AqiIndexBreakpoints[i] - AqiIndexBreakpoints[i-1] + 1)/(Aqi10mmBreakpoints[i] - Aqi10mmBreakpoints[i-1] + .1))*(ppm-Aqi10mmBreakpoints[i-1] + .1)) + AqiIndexBreakpoints[i-1] + 1; 
        break;
    }
}

msg.payload = Math.trunc(AQI).toString();
return msg;

```

I still need to get proper error handling into the code.

---

<div class="post-metadata">

**Author:** ![trlafleur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trlafleur/32/47662_2.png) [@trlafleur](https://discourse.nodered.org/u/trlafleur)\
**Post date:** [24 August 2021 16:21 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/5 "2021-08-24T16:21:18Z")

</div>

To calculate AQI correctly, you need to do it over time.... here is a reference on how to do it per EPA standard.

[https://forum.airnowtech.org/t/daily-and-hourly-aqi-pm2-5/171](https://forum.airnowtech.org/t/daily-and-hourly-aqi-pm2-5/171)

> **[GitHub - ThangLeQuoc/aqi-bot: Node Module Air Quality Index Calculator using...](https://github.com/ThangLeQuoc/aqi-bot)**
>
> Node Module Air Quality Index Calculator using US EPA Formula, which support AQI calculation for pollutant, and also with Nowcast AQI - GitHub - ThangLeQuoc/aqi-bot: Node Module Air Quality Index C...

---

<div class="post-metadata">

**Author:** ![trlafleur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trlafleur/32/47662_2.png) [@trlafleur](https://discourse.nodered.org/u/trlafleur)\
**Post date:** [7 September 2021 19:46 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/6 "2021-09-07T19:46:45Z")

</div>

See my NodeRed AQI test flow...  
[https://flows.nodered.org/flow/a51d11c82ba800a8183c630d57d02be0](https://flows.nodered.org/flow/a51d11c82ba800a8183c630d57d02be0)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 November 2021 19:47 UTC](https://discourse.nodered.org/t/calculating-aqi-with-node-red/48001/7 "2021-11-06T19:47:18Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
