ESPHome  2024.11.0
ntc.cpp
Go to the documentation of this file.
1 #include "ntc.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace ntc {
6 
7 static const char *const TAG = "ntc";
8 
9 void 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 }
14 void NTC::dump_config() { LOG_SENSOR("", "NTC Sensor", this) }
16 void 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
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
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
float get_setup_priority() const override
Definition: ntc.cpp:15
double a_
Definition: ntc.h:23
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
double c_
Definition: ntc.h:25
double b_
Definition: ntc.h:24
void process_(float value)
Definition: ntc.cpp:16
void setup() override
Definition: ntc.cpp:9
constexpr const char * c_str() const
Definition: string_ref.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
sensor::Sensor * sensor_
Definition: ntc.h:22
void dump_config() override
Definition: ntc.cpp:14