ESPHome 2026.1.4
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
4namespace esphome {
5namespace absolute_humidity {
6
7static const char *const TAG = "absolute_humidity.sensor";
8
10 ESP_LOGD(TAG, " Added callback for temperature '%s'", this->temperature_sensor_->get_name().c_str());
11 this->temperature_sensor_->add_on_state_callback([this](float state) { this->temperature_callback_(state); });
12 if (this->temperature_sensor_->has_state()) {
14 }
15
16 ESP_LOGD(TAG, " Added callback for relative humidity '%s'", this->humidity_sensor_->get_name().c_str());
17 this->humidity_sensor_->add_on_state_callback([this](float state) { this->humidity_callback_(state); });
18 if (this->humidity_sensor_->has_state()) {
20 }
21}
22
24 LOG_SENSOR("", "Absolute Humidity", this);
25
26 switch (this->equation_) {
27 case BUCK:
28 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Buck");
29 break;
30 case TETENS:
31 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Tetens");
32 break;
33 case WOBUS:
34 ESP_LOGCONFIG(TAG, "Saturation Vapor Pressure Equation: Wobus");
35 break;
36 default:
37 ESP_LOGE(TAG, "Invalid saturation vapor pressure equation selection!");
38 break;
39 }
40
41 ESP_LOGCONFIG(TAG,
42 "Sources\n"
43 " Temperature: '%s'\n"
44 " Relative Humidity: '%s'",
45 this->temperature_sensor_->get_name().c_str(), this->humidity_sensor_->get_name().c_str());
46}
47
49
51 if (!this->next_update_) {
52 return;
53 }
54 this->next_update_ = false;
55
56 // Ensure we have source data
57 const bool no_temperature = std::isnan(this->temperature_);
58 const bool no_humidity = std::isnan(this->humidity_);
59 if (no_temperature || no_humidity) {
60 if (no_temperature) {
61 ESP_LOGW(TAG, "No valid state from temperature sensor!");
62 }
63 if (no_humidity) {
64 ESP_LOGW(TAG, "No valid state from humidity sensor!");
65 }
66 this->publish_state(NAN);
67 this->status_set_warning(LOG_STR("Unable to calculate absolute humidity."));
68 return;
69 }
70
71 // Convert to desired units
72 const float temperature_c = this->temperature_;
73 const float temperature_k = temperature_c + 273.15;
74 const float hr = this->humidity_ / 100;
75
76 // Calculate saturation vapor pressure
77 float es;
78 switch (this->equation_) {
79 case BUCK:
80 es = es_buck(temperature_c);
81 break;
82 case TETENS:
83 es = es_tetens(temperature_c);
84 break;
85 case WOBUS:
86 es = es_wobus(temperature_c);
87 break;
88 default:
89 this->publish_state(NAN);
90 this->status_set_error(LOG_STR("Invalid saturation vapor pressure equation selection!"));
91 return;
92 }
93
94 // Calculate absolute humidity
95 const float absolute_humidity = vapor_density(es, hr, temperature_k);
96
97 ESP_LOGD(TAG,
98 "Saturation vapor pressure %f kPa\n"
99 "Publishing absolute humidity %f g/m³",
100 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) {
112 a = 0.61121;
113 b = 18.678;
114 c = 234.5;
115 d = 257.14;
116 } else {
117 a = 0.61115;
118 b = 18.678;
119 c = 233.7;
120 d = 279.82;
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) {
129 a = 17.27;
130 b = 237.3;
131 } else {
132 a = 21.875;
133 b = 265.5;
134 }
135 return 0.61078 * 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 const float c0 = +0.99999683e00;
155 const float c1 = -0.90826951e-02;
156 const float c2 = +0.78736169e-04;
157 const float c3 = -0.61117958e-06;
158 const float c4 = +0.43884187e-08;
159 const float c5 = -0.29883885e-10;
160 const float c6 = +0.21874425e-12;
161 const float c7 = -0.17892321e-14;
162 const float c8 = +0.11112018e-16;
163 const float c9 = -0.30994571e-19;
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.61078 / pow(p, 8);
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; // vapor pressure of the air (Pa)
177 const float mw = 18.01528; // molar mass of water (g⋅mol⁻¹)
178 const float r = 8.31446261815324; // molar gas constant (J⋅K⁻¹)
179 return (ea * mw) / (r * ta);
180}
181
182} // namespace absolute_humidity
183} // namespace esphome
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
const StringRef & get_name() const
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 publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:76
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:123
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.cpp:89
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:116
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7