ESPHome 2026.5.0b1
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::ntc {
5
6static const char *const TAG = "ntc";
7
8void NTC::setup() {
9 this->sensor_->add_on_state_callback([this](float value) { this->process_(value); });
10 if (this->sensor_->has_state())
11 this->process_(this->sensor_->state);
12}
13void NTC::dump_config() { LOG_SENSOR("", "NTC Sensor", this); }
14void NTC::process_(float value) {
15 if (std::isnan(value)) {
16 this->publish_state(NAN);
17 return;
18 }
19
20 double lr = log(double(value));
21 double v = this->a_ + this->b_ * lr + this->c_ * lr * lr * lr;
22 auto temp = float(1.0 / v - 273.15);
23
24 ESP_LOGV(TAG, "'%s' - Temperature: %.1f°C", this->name_.c_str(), temp);
25 this->publish_state(temp);
26}
27
28} // namespace esphome::ntc
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
sensor::Sensor * sensor_
Definition ntc.h:20
void dump_config() override
Definition ntc.cpp:13
void setup() override
Definition ntc.cpp:8
void process_(float value)
Definition ntc.cpp:14
double b_
Definition ntc.h:22
double a_
Definition ntc.h:21
double c_
Definition ntc.h:23
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
uint32_t lr