ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
absolute_humidity.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "absolute_humidity.h"
3
5
6static const char *const TAG{"absolute_humidity.sensor"};
7
10 this->temperature_ = state;
11 this->enable_loop();
12 });
13 ESP_LOGD(TAG, " Added callback for temperature '%s'", this->temperature_sensor_->get_name().c_str());
14 // Get initial value
15 if (this->temperature_sensor_->has_state()) {
17 }
18
19 this->humidity_sensor_->add_on_state_callback([this](float state) {
20 this->humidity_ = state;
21 this->enable_loop();
22 });
23 ESP_LOGD(TAG, " Added callback for relative humidity '%s'", this->humidity_sensor_->get_name().c_str());
24 // Get initial value
25 if (this->humidity_sensor_->has_state()) {
26 this->humidity_ = this->humidity_sensor_->get_state();
27 }
28}
29
31 LOG_SENSOR("", "Absolute Humidity", this);
32
33 switch (this->equation_) {
34 case BUCK:
35 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Buck");
36 break;
37 case TETENS:
38 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Tetens");
39 break;
40 case WOBUS:
41 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Wobus");
42 break;
43 default:
44 ESP_LOGE(TAG, "Invalid saturation vapor pressure equation selection!");
45 break;
46 }
47
48 ESP_LOGCONFIG(TAG,
49 "Sources\n"
50 " Temperature: '%s'\n"
51 " Relative Humidity: '%s'",
52 this->temperature_sensor_->get_name().c_str(), this->humidity_sensor_->get_name().c_str());
53}
54
56 // Only run once
57 this->disable_loop();
58
59 // Ensure we have source data
60 const bool no_temperature{std::isnan(this->temperature_)};
61 const bool no_humidity{std::isnan(this->humidity_)};
62 if (no_temperature || no_humidity) {
63 if (no_temperature) {
64 ESP_LOGW(TAG, "No valid state from temperature sensor!");
65 }
66 if (no_humidity) {
67 ESP_LOGW(TAG, "No valid state from humidity sensor!");
68 }
69 this->publish_state(NAN);
70 this->status_set_warning(LOG_STR("Unable to calculate absolute humidity."));
71 return;
72 }
73
74 // Convert to desired units
75 const float temperature_c{this->temperature_};
76 const float temperature_k{temperature_c + 273.15f};
77 const float hr{this->humidity_ / 100.0f};
78
79 // Calculate saturation vapor pressure
80 float es;
81 switch (this->equation_) {
82 case BUCK:
83 es = es_buck(temperature_c);
84 break;
85 case TETENS:
86 es = es_tetens(temperature_c);
87 break;
88 case WOBUS:
89 es = es_wobus(temperature_c);
90 break;
91 default:
92 this->publish_state(NAN);
93 this->status_set_error(LOG_STR("Invalid saturation vapor pressure equation selection!"));
94 return;
95 }
96
97 // Calculate absolute humidity
98 const float absolute_humidity{vapor_density(es, hr, temperature_k)};
99
100 ESP_LOGD(TAG, "Saturation vapor pressure %f kPa, absolute humidity %f g/m³", es, absolute_humidity);
101
102 // Publish absolute humidity
103 this->status_clear_warning();
104 this->publish_state(absolute_humidity);
105}
106
107// Buck equation (https://en.wikipedia.org/wiki/Arden_Buck_equation)
108// More accurate than Tetens in normal meteorologic conditions
109float AbsoluteHumidityComponent::es_buck(float temperature_c) {
110 float a, b, c, d;
111 if (temperature_c >= 0.0f) {
112 a = 0.61121f;
113 b = 18.678f;
114 c = 234.5f;
115 d = 257.14f;
116 } else {
117 a = 0.61115f;
118 b = 18.678f;
119 c = 233.7f;
120 d = 279.82f;
121 }
122 return a * expf((b - (temperature_c / c)) * (temperature_c / (d + temperature_c)));
123}
124
125// Tetens equation (https://en.wikipedia.org/wiki/Tetens_equation)
126float AbsoluteHumidityComponent::es_tetens(float temperature_c) {
127 float a, b;
128 if (temperature_c >= 0.0f) {
129 a = 17.27f;
130 b = 237.3f;
131 } else {
132 a = 21.875f;
133 b = 265.5f;
134 }
135 return 0.61078f * expf((a * temperature_c) / (temperature_c + b));
136}
137
138// Wobus equation
139// https://wahiduddin.net/calc/density_altitude.htm
140// https://wahiduddin.net/calc/density_algorithms.htm
141// Calculate the saturation vapor pressure (kPa)
143 // THIS FUNCTION RETURNS THE SATURATION VAPOR PRESSURE ESW (MILLIBARS)
144 // OVER LIQUID WATER GIVEN THE TEMPERATURE T (CELSIUS). THE POLYNOMIAL
145 // APPROXIMATION BELOW IS DUE TO HERMAN WOBUS, A MATHEMATICIAN WHO
146 // WORKED AT THE NAVY WEATHER RESEARCH FACILITY, NORFOLK, VIRGINIA,
147 // BUT WHO IS NOW RETIRED. THE COEFFICIENTS OF THE POLYNOMIAL WERE
148 // CHOSEN TO FIT THE VALUES IN TABLE 94 ON PP. 351-353 OF THE SMITH-
149 // SONIAN METEOROLOGICAL TABLES BY ROLAND LIST (6TH EDITION). THE
150 // APPROXIMATION IS VALID FOR -50 < T < 100C.
151 //
152 // Baker, Schlatter 17-MAY-1982 Original version.
153
154 constexpr float c0{+0.99999683e+00f};
155 constexpr float c1{-0.90826951e-02f};
156 constexpr float c2{+0.78736169e-04f};
157 constexpr float c3{-0.61117958e-06f};
158 constexpr float c4{+0.43884187e-08f};
159 constexpr float c5{-0.29883885e-10f};
160 constexpr float c6{+0.21874425e-12f};
161 constexpr float c7{-0.17892321e-14f};
162 constexpr float c8{+0.11112018e-16f};
163 constexpr float c9{-0.30994571e-19f};
164 const float p{c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * (c6 + t * (c7 + t * (c8 + t * (c9)))))))))};
165 return 0.61078f / powf(p, 8.0f);
166}
167
168// From https://www.environmentalbiophysics.org/chalk-talk-how-to-calculate-absolute-humidity/
169// H/T to https://esphome.io/cookbook/bme280_environment/
170// H/T to https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
171float AbsoluteHumidityComponent::vapor_density(float es, float hr, float ta) {
172 // es = saturated vapor pressure (kPa)
173 // hr = relative humidity [0-1]
174 // ta = absolute temperature (K)
175
176 const float ea{hr * es * 1000.0f}; // vapor pressure of the air (Pa)
177 const float mw{18.01528f}; // molar mass of water (g⋅mol⁻¹)
178 const float r{8.31446261815324f}; // molar gas constant (J⋅K⁻¹)
179 return (ea * mw) / (r * ta);
180}
181
182} // namespace esphome::absolute_humidity
void enable_loop()
Enable this component's loop.
Definition component.h:258
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:306
const StringRef & get_name() const
Definition entity_base.h:71
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
static float es_buck(float temperature_c)
Buck equation for saturation vapor pressure in kPa.
static float es_tetens(float temperature_c)
Tetens equation for saturation vapor pressure in kPa.
static float vapor_density(float es, float hr, float ta)
Calculate vapor density (absolute humidity) in g/m³.
static float es_wobus(float temperature_c)
Wobus equation for saturation vapor pressure in kPa.
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float get_state() const
Getter-syntax for .state.
Definition sensor.h:98
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138