ESPHome 2025.5.0
Loading...
Searching...
No Matches
ntc.cpp
Go to the documentation of this file.
1#include "ntc.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace ntc {
6
7static const char *const TAG = "ntc";
8
9void NTC::setup() {
10 this->sensor_->add_on_state_callback([this](float value) { this->process_(value); });
11 if (this->sensor_->has_state())
12 this->process_(this->sensor_->state);
13}
14void NTC::dump_config() { LOG_SENSOR("", "NTC Sensor", this) }
16void NTC::process_(float value) {
17 if (std::isnan(value)) {
18 this->publish_state(NAN);
19 return;
20 }
21
22 double lr = log(double(value));
23 double v = this->a_ + this->b_ * lr + this->c_ * lr * lr * lr;
24 auto temp = float(1.0 / v - 273.15);
25
26 ESP_LOGD(TAG, "'%s' - Temperature: %.1f°C", this->name_.c_str(), temp);
27 this->publish_state(temp);
28}
29
30} // namespace ntc
31} // namespace esphome
constexpr const char * c_str() const
Definition string_ref.h:68
sensor::Sensor * sensor_
Definition ntc.h:22
void dump_config() override
Definition ntc.cpp:14
void setup() override
Definition ntc.cpp:9
float get_setup_priority() const override
Definition ntc.cpp:15
void process_(float value)
Definition ntc.cpp:16
double b_
Definition ntc.h:24
double a_
Definition ntc.h:23
double c_
Definition ntc.h:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
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:52
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet.
Definition sensor.cpp:97
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7