ESPHome
2026.3.0-dev
Loading...
Searching...
No Matches
esphome
components
aqi
caqi_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
namespace
esphome::aqi
{
9
10
class
CAQICalculator
:
public
AbstractAQICalculator
{
11
public
:
12
uint16_t
get_aqi
(
float
pm2_5_value,
float
pm10_0_value)
override
{
13
float
pm2_5_index =
calculate_index
(pm2_5_value,
PM2_5_GRID
);
14
float
pm10_0_index =
calculate_index
(pm10_0_value,
PM10_0_GRID
);
15
16
float
aqi = std::max(pm2_5_index, pm10_0_index);
17
if
(aqi < 0.0f) {
18
aqi = 0.0f;
19
}
20
return
static_cast<
uint16_t
>
(std::lround(aqi));
21
}
22
23
protected
:
24
static
constexpr
int
NUM_LEVELS
= 5;
25
26
static
constexpr
int
INDEX_GRID
[
NUM_LEVELS
][2] = {{0, 25}, {26, 50}, {51, 75}, {76, 100}, {101, 400}};
27
28
static
constexpr
float
PM2_5_GRID
[
NUM_LEVELS
][2] = {
29
// clang-format off
30
{0.0f, 15.1f},
31
{15.1f, 30.1f},
32
{30.1f, 55.1f},
33
{55.1f, 110.1f},
34
{110.1f, std::numeric_limits<float>::max()}
35
// clang-format on
36
};
37
38
static
constexpr
float
PM10_0_GRID
[
NUM_LEVELS
][2] = {
39
// clang-format off
40
{0.0f, 25.1f},
41
{25.1f, 50.1f},
42
{50.1f, 90.1f},
43
{90.1f, 180.1f},
44
{180.1f, 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
54
float
aqi_lo =
INDEX_GRID
[grid_index][0];
55
float
aqi_hi =
INDEX_GRID
[grid_index][1];
56
float
conc_lo = array[grid_index][0];
57
float
conc_hi = array[grid_index][1];
58
59
return
(value - conc_lo) * (aqi_hi - aqi_lo) / (conc_hi - conc_lo) + aqi_lo;
60
}
61
62
static
int
get_grid_index
(
float
value,
const
float
array[
NUM_LEVELS
][2]) {
63
for
(
int
i = 0; i <
NUM_LEVELS
; i++) {
64
const
bool
in_range =
65
(value >= array[i][0]) && ((i ==
NUM_LEVELS
- 1) ? (value <= array[i][1])
// last bucket inclusive
66
: (value < array[i][1]));
// others exclusive on hi
67
if
(in_range) {
68
return
i;
69
}
70
}
71
return
-1;
72
}
73
};
74
75
}
// namespace esphome::aqi
abstract_aqi_calculator.h
esphome::aqi::AbstractAQICalculator
Definition
abstract_aqi_calculator.h:7
esphome::aqi::CAQICalculator
Definition
caqi_calculator.h:10
esphome::aqi::CAQICalculator::INDEX_GRID
static constexpr int INDEX_GRID[NUM_LEVELS][2]
Definition
caqi_calculator.h:26
esphome::aqi::CAQICalculator::PM10_0_GRID
static constexpr float PM10_0_GRID[NUM_LEVELS][2]
Definition
caqi_calculator.h:38
esphome::aqi::CAQICalculator::PM2_5_GRID
static constexpr float PM2_5_GRID[NUM_LEVELS][2]
Definition
caqi_calculator.h:28
esphome::aqi::CAQICalculator::calculate_index
static float calculate_index(float value, const float array[NUM_LEVELS][2])
Definition
caqi_calculator.h:48
esphome::aqi::CAQICalculator::get_grid_index
static int get_grid_index(float value, const float array[NUM_LEVELS][2])
Definition
caqi_calculator.h:62
esphome::aqi::CAQICalculator::NUM_LEVELS
static constexpr int NUM_LEVELS
Definition
caqi_calculator.h:24
esphome::aqi::CAQICalculator::get_aqi
uint16_t get_aqi(float pm2_5_value, float pm10_0_value) override
Definition
caqi_calculator.h:12
esphome::aqi
Definition
abstract_aqi_calculator.h:5
Generated by
1.12.0