5namespace absolute_humidity {
7static const char *
const TAG =
"absolute_humidity.sensor";
24 LOG_SENSOR(
"",
"Absolute Humidity",
this);
28 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Buck");
31 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Tetens");
34 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Wobus");
37 ESP_LOGE(TAG,
"Invalid saturation vapor pressure equation selection!");
43 " Temperature: '%s'\n"
44 " Relative Humidity: '%s'",
55 const bool no_temperature = std::isnan(this->
temperature_);
56 const bool no_humidity = std::isnan(this->
humidity_);
57 if (no_temperature || no_humidity) {
59 ESP_LOGW(TAG,
"No valid state from temperature sensor!");
62 ESP_LOGW(TAG,
"No valid state from humidity sensor!");
71 const float temperature_k = temperature_c + 273.15;
88 this->
status_set_error(LOG_STR(
"Invalid saturation vapor pressure equation selection!"));
93 const float absolute_humidity =
vapor_density(es, hr, temperature_k);
96 "Saturation vapor pressure %f kPa\n"
97 "Publishing absolute humidity %f g/m³",
98 es, absolute_humidity);
109 if (temperature_c >= 0) {
120 return a * expf((b - (temperature_c / c)) * (temperature_c / (d + temperature_c)));
126 if (temperature_c >= 0) {
133 return 0.61078 * expf((a * temperature_c) / (temperature_c + b));
152 const float c0 = +0.99999683e00;
153 const float c1 = -0.90826951e-02;
154 const float c2 = +0.78736169e-04;
155 const float c3 = -0.61117958e-06;
156 const float c4 = +0.43884187e-08;
157 const float c5 = -0.29883885e-10;
158 const float c6 = +0.21874425e-12;
159 const float c7 = -0.17892321e-14;
160 const float c8 = +0.11112018e-16;
161 const float c9 = -0.30994571e-19;
162 const float p = c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * (c6 + t * (c7 + t * (c8 + t * (c9)))))))));
163 return 0.61078 / pow(p, 8);
174 const float ea = hr * es * 1000;
175 const float mw = 18.01528;
176 const float r = 8.31446261815324;
177 return (ea * mw) / (r * ta);
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
const StringRef & get_name() const
constexpr const char * c_str() const
sensor::Sensor * humidity_sensor_
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 humidity_callback_(float state)
void dump_config() override
sensor::Sensor * temperature_sensor_
void temperature_callback_(float state)
SaturationVaporPressureEquation equation_
void publish_state(float state)
Publish a new state to the front-end.
float get_state() const
Getter-syntax for .state.
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
float state
This member variable stores the last state that has passed through all filters.
Providing packet encoding functions for exchanging data with a remote host.