ESPHome 2026.2.4
Loading...
Searching...
No Matches
caqi_calculator.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <limits>
7
8namespace esphome::aqi {
9
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
static constexpr int INDEX_GRID[NUM_LEVELS][2]
static constexpr float PM10_0_GRID[NUM_LEVELS][2]
static constexpr float PM2_5_GRID[NUM_LEVELS][2]
static float calculate_index(float value, const float array[NUM_LEVELS][2])
static int get_grid_index(float value, const float array[NUM_LEVELS][2])
static constexpr int NUM_LEVELS
uint16_t get_aqi(float pm2_5_value, float pm10_0_value) override