ESPHome 2026.3.0
Loading...
Searching...
No Matches
dew_point.cpp
Go to the documentation of this file.
1
2#include "dew_point.h"
3
5
6static const char *const TAG = "dew_point.sensor";
7
9 // Register callbacks for sensor updates
10 if (this->temperature_sensor_ != nullptr) {
13 this->enable_loop();
14 });
15 // Get initial value
16 if (this->temperature_sensor_->has_state()) {
18 }
19 }
20
21 if (this->humidity_sensor_ != nullptr) {
22 this->humidity_sensor_->add_on_state_callback([this](float state) {
23 this->humidity_value_ = state;
24 this->enable_loop();
25 });
26 // Get initial value
27 if (this->humidity_sensor_->has_state()) {
29 }
30 }
31}
32
34 LOG_SENSOR("", "Dew Point", this);
35 ESP_LOGCONFIG(TAG,
36 "Sources\n"
37 " Temperature: '%s'\n"
38 " Humidity: '%s'",
39 this->temperature_sensor_->get_name().c_str(), this->humidity_sensor_->get_name().c_str());
40}
41
43
45 // Only run once
46 this->disable_loop();
47
48 // Check if we have valid values for both sensors
49 if (std::isnan(this->temperature_value_) || std::isnan(this->humidity_value_)) {
50 ESP_LOGW(TAG, "Temperature or humidity value is NaN, skipping calculation");
51 this->publish_state(NAN);
52 return;
53 }
54
55 // Check for valid humidity range
57 ESP_LOGW(TAG, "Humidity value out of range (0-100): %.2f", this->humidity_value_);
58 this->publish_state(NAN);
59 return;
60 }
61
62 // Magnus formula constants
63 const float a{17.625f};
64 const float b{243.04f};
65
66 // Calculate dew point using Magnus formula
67 // Td = (b * alpha) / (a - alpha)
68 // where alpha = ln(RH/100) + (a * T) / (b + T)
69
70 const float alpha{std::log(this->humidity_value_ / 100.0f) +
71 (a * this->temperature_value_) / (b + this->temperature_value_)};
72
73 const float dew_point{(b * alpha) / (a - alpha)};
74
75 // Publish the calculated dew point
76 this->publish_state(dew_point);
77
78 ESP_LOGD(TAG, "'%s' >> %.1f°C (T: %.1f°C, RH: %.1f%%)", this->get_name().c_str(), dew_point, this->temperature_value_,
79 this->humidity_value_);
80}
81
82} // namespace esphome::dew_point
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
float get_setup_priority() const override
Definition dew_point.cpp:42
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:118
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:82
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:125
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Definition component.h:31