ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
resistance_sensor.cpp
Go to the documentation of this file.
1#include "resistance_sensor.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "resistance";
7
9 LOG_SENSOR("", "Resistance Sensor", this);
10 ESP_LOGCONFIG(TAG,
11 " Configuration: %s\n"
12 " Resistor: %.2fΩ\n"
13 " Reference Voltage: %.1fV",
14 this->configuration_ == UPSTREAM ? "UPSTREAM" : "DOWNSTREAM", this->resistor_,
15 this->reference_voltage_);
16}
17void ResistanceSensor::process_(float value) {
18 if (std::isnan(value)) {
19 this->publish_state(NAN);
20 return;
21 }
22 float res = 0;
23 switch (this->configuration_) {
24 case UPSTREAM:
25 if (value == 0.0f) {
26 res = NAN;
27 } else {
28 res = (this->reference_voltage_ - value) / value;
29 }
30 break;
31 case DOWNSTREAM:
32 if (value == this->reference_voltage_) {
33 res = NAN;
34 } else {
35 res = value / (this->reference_voltage_ - value);
36 }
37 break;
38 }
39
40 res *= this->resistor_;
41 ESP_LOGV(TAG, "'%s' - Resistance %.1fΩ", this->name_.c_str(), res);
42 this->publish_state(res);
43}
44
45} // namespace esphome::resistance
constexpr const char * c_str() const
Definition string_ref.h:73
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68