ESPHome
2026.6.0-dev
Loading...
Searching...
No Matches
esphome
components
aqi
aqi_calculator.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <algorithm>
4
#include <cmath>
5
#include <limits>
6
#include "
abstract_aqi_calculator.h
"
7
8
// https://document.airnow.gov/technical-assistance-document-for-the-reporting-of-daily-air-quailty.pdf
9
10
namespace
esphome::aqi
{
11
12
class
AQICalculator
:
public
AbstractAQICalculator
{
13
public
:
14
uint16_t
get_aqi
(
float
pm2_5_value,
float
pm10_0_value)
override
{
15
float
pm2_5_index =
calculate_index
(pm2_5_value,
PM2_5_GRID
);
16
float
pm10_0_index =
calculate_index
(pm10_0_value,
PM10_0_GRID
);
17
float
aqi = std::max({pm2_5_index, pm10_0_index, 0.0f});
18
return
static_cast<
uint16_t
>
(std::lround(aqi));
19
}
20
21
protected
:
22
static
constexpr
int
NUM_LEVELS
= 6;
23
24
static
constexpr
int
INDEX_GRID
[
NUM_LEVELS
][2] = {{0, 50}, {51, 100}, {101, 150}, {151, 200}, {201, 300}, {301, 500}};
25
26
static
constexpr
float
PM2_5_GRID
[
NUM_LEVELS
][2] = {
27
// clang-format off
28
{0.0f, 9.1f},
29
{9.1f, 35.5f},
30
{35.5f, 55.5f},
31
{55.5f, 125.5f},
32
{125.5f, 225.5f},
33
{225.5f, std::numeric_limits<float>::max()}
34
// clang-format on
35
};
36
37
static
constexpr
float
PM10_0_GRID
[
NUM_LEVELS
][2] = {
38
// clang-format off
39
{0.0f, 55.0f},
40
{55.0f, 155.0f},
41
{155.0f, 255.0f},
42
{255.0f, 355.0f},
43
{355.0f, 425.0f},
44
{425.0f, std::numeric_limits<float>::max()}
45
// clang-format on
46
};
47
48
static
float
calculate_index
(
float
value,
const
float
array[
NUM_LEVELS
][2]) {
49
int
grid_index =
get_grid_index
(value, array);
50
if
(grid_index == -1) {
51
return
-1.0f;
52
}
53
float
aqi_lo =
INDEX_GRID
[grid_index][0];
54
float
aqi_hi =
INDEX_GRID
[grid_index][1];
55
float
conc_lo = array[grid_index][0];
56
float
conc_hi = array[grid_index][1];
57
58
return
(value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
59
}
60
61
static
int
get_grid_index
(
float
value,
const
float
array[
NUM_LEVELS
][2]) {
62
for
(
int
i = 0; i <
NUM_LEVELS
; i++) {
63
const
bool
in_range =
64
(value >= array[i][0]) && ((i ==
NUM_LEVELS
- 1) ? (value <= array[i][1])
// last bucket inclusive
65
: (value < array[i][1]));
// others exclusive on hi
66
if
(in_range) {
67
return
i;
68
}
69
}
70
return
-1;
71
}
72
};
73
74
}
// namespace esphome::aqi
abstract_aqi_calculator.h
esphome::aqi::AQICalculator
Definition
aqi_calculator.h:12
esphome::aqi::AQICalculator::PM2_5_GRID
static constexpr float PM2_5_GRID[NUM_LEVELS][2]
Definition
aqi_calculator.h:26
esphome::aqi::AQICalculator::NUM_LEVELS
static constexpr int NUM_LEVELS
Definition
aqi_calculator.h:22
esphome::aqi::AQICalculator::get_grid_index
static int get_grid_index(float value, const float array[NUM_LEVELS][2])
Definition
aqi_calculator.h:61
esphome::aqi::AQICalculator::calculate_index
static float calculate_index(float value, const float array[NUM_LEVELS][2])
Definition
aqi_calculator.h:48
esphome::aqi::AQICalculator::PM10_0_GRID
static constexpr float PM10_0_GRID[NUM_LEVELS][2]
Definition
aqi_calculator.h:37
esphome::aqi::AQICalculator::INDEX_GRID
static constexpr int INDEX_GRID[NUM_LEVELS][2]
Definition
aqi_calculator.h:24
esphome::aqi::AQICalculator::get_aqi
uint16_t get_aqi(float pm2_5_value, float pm10_0_value) override
Definition
aqi_calculator.h:14
esphome::aqi::AbstractAQICalculator
Definition
abstract_aqi_calculator.h:7
esphome::aqi
Definition
abstract_aqi_calculator.h:5
Generated by
1.12.0