5namespace absolute_humidity {
7static const char *
const TAG =
"absolute_humidity.sensor";
10 ESP_LOGCONFIG(TAG,
"Running setup for '%s'", this->
get_name().c_str());
26 LOG_SENSOR(
"",
"Absolute Humidity",
this);
30 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Buck");
33 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Tetens");
36 ESP_LOGCONFIG(TAG,
"Saturation Vapor Pressure Equation: Wobus");
39 ESP_LOGE(TAG,
"Invalid saturation vapor pressure equation selection!");
45 " Temperature: '%s'\n"
46 " Relative Humidity: '%s'",
59 const bool no_temperature = std::isnan(this->
temperature_);
60 const bool no_humidity = std::isnan(this->
humidity_);
61 if (no_temperature || no_humidity) {
63 ESP_LOGW(TAG,
"No valid state from temperature sensor!");
66 ESP_LOGW(TAG,
"No valid state from temperature sensor!");
68 ESP_LOGW(TAG,
"Unable to calculate absolute humidity.");
76 const float temperature_k = temperature_c + 273.15;
92 ESP_LOGE(TAG,
"Invalid saturation vapor pressure equation selection!");
97 ESP_LOGD(TAG,
"Saturation vapor pressure %f kPa", es);
100 const float absolute_humidity =
vapor_density(es, hr, temperature_k);
103 ESP_LOGD(TAG,
"Publishing absolute humidity %f g/m³", absolute_humidity);
112 if (temperature_c >= 0) {
123 return a * expf((b - (temperature_c / c)) * (temperature_c / (d + temperature_c)));
129 if (temperature_c >= 0) {
136 return 0.61078 * expf((a * temperature_c) / (temperature_c + b));
155 const float c0 = +0.99999683e00;
156 const float c1 = -0.90826951e-02;
157 const float c2 = +0.78736169e-04;
158 const float c3 = -0.61117958e-06;
159 const float c4 = +0.43884187e-08;
160 const float c5 = -0.29883885e-10;
161 const float c6 = +0.21874425e-12;
162 const float c7 = -0.17892321e-14;
163 const float c8 = +0.11112018e-16;
164 const float c9 = -0.30994571e-19;
165 const float p = c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * (c6 + t * (c7 + t * (c8 + t * (c9)))))))));
166 return 0.61078 / pow(p, 8);
177 const float ea = hr * es * 1000;
178 const float mw = 18.01528;
179 const float r = 8.31446261815324;
180 return (ea * mw) / (r * ta);
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
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.