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'",
57 const bool no_temperature = std::isnan(this->
temperature_);
58 const bool no_humidity = std::isnan(this->
humidity_);
59 if (no_temperature || no_humidity) {
61 ESP_LOGW(TAG,
"No valid state from temperature sensor!");
64 ESP_LOGW(TAG,
"No valid state from humidity sensor!");
73 const float temperature_k = temperature_c + 273.15;
90 this->
status_set_error(LOG_STR(
"Invalid saturation vapor pressure equation selection!"));
95 const float absolute_humidity =
vapor_density(es, hr, temperature_k);
98 "Saturation vapor pressure %f kPa\n"
99 "Publishing absolute humidity %f g/m³",
100 es, absolute_humidity);
111 if (temperature_c >= 0) {
122 return a * expf((b - (temperature_c / c)) * (temperature_c / (d + temperature_c)));
128 if (temperature_c >= 0) {
135 return 0.61078 * expf((a * temperature_c) / (temperature_c + b));
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);
176 const float ea = hr * es * 1000;
177 const float mw = 18.01528;
178 const float r = 8.31446261815324;
179 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³.
float get_setup_priority() const override
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.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.