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