ESPHome 2026.2.1
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); }
15void NTC::process_(float value) {
16 if (std::isnan(value)) {
17 this->publish_state(NAN);
18 return;
19 }
20
21 double lr = log(double(value));
22 double v = this->a_ + this->b_ * lr + this->c_ * lr * lr * lr;
23 auto temp = float(1.0 / v - 273.15);
24
25 ESP_LOGV(TAG, "'%s' - Temperature: %.1f°C", this->name_.c_str(), temp);
26 this->publish_state(temp);
27}
28
29} // namespace ntc
30} // namespace esphome
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
sensor::Sensor * sensor_
Definition ntc.h:21
void dump_config() override
Definition ntc.cpp:14
void setup() override
Definition ntc.cpp:9
void process_(float value)
Definition ntc.cpp:15
double b_
Definition ntc.h:23
double a_
Definition ntc.h:22
double c_
Definition ntc.h:24
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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:78
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:117
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7