ESPHome  2022.8.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:85
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
std::string name_
Definition: entity_base.h:54
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:133
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:72
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
Definition: a4988.cpp:4
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:130
sensor::Sensor * sensor_
Definition: ntc.h:22
void dump_config() override
Definition: ntc.cpp:14