5namespace absolute_humidity {
7static const char *
const TAG =
"absolute_humidity.sensor";
10 ESP_LOGCONFIG(TAG,
"Setting up absolute humidity '%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!");
43 ESP_LOGCONFIG(TAG,
"Sources");
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 temperature sensor!");
66 ESP_LOGW(TAG,
"Unable to calculate absolute humidity.");
74 const float temperature_k = temperature_c + 273.15;
90 ESP_LOGE(TAG,
"Invalid saturation vapor pressure equation selection!");
95 ESP_LOGD(TAG,
"Saturation vapor pressure %f kPa", es);
98 const float absolute_humidity =
vapor_density(es, hr, temperature_k);
101 ESP_LOGD(TAG,
"Publishing absolute humidity %f g/m³", absolute_humidity);
110 if (temperature_c >= 0) {
121 return a * expf((b - (temperature_c / c)) * (temperature_c / (d + temperature_c)));
127 if (temperature_c >= 0) {
134 return 0.61078 * expf((a * temperature_c) / (temperature_c + b));
153 const float c0 = +0.99999683e00;
154 const float c1 = -0.90826951e-02;
155 const float c2 = +0.78736169e-04;
156 const float c3 = -0.61117958e-06;
157 const float c4 = +0.43884187e-08;
158 const float c5 = -0.29883885e-10;
159 const float c6 = +0.21874425e-12;
160 const float c7 = -0.17892321e-14;
161 const float c8 = +0.11112018e-16;
162 const float c9 = -0.30994571e-19;
163 const float p = c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * (c6 + t * (c7 + t * (c8 + t * (c9)))))))));
164 return 0.61078 / pow(p, 8);
175 const float ea = hr * es * 1000;
176 const float mw = 18.01528;
177 const float r = 8.31446261815324;
178 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.
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet.
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.