# Calculating AQI with Node-Red?

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?

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

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.

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).

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.

``````//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

``````//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.

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

See my NodeRed AQI test flow...
https://flows.nodered.org/flow/a51d11c82ba800a8183c630d57d02be0

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