ESPHome 2025.6.3
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
4namespace esphome {
5namespace resistance {
6
7static const char *const TAG = "resistance";
8
10 LOG_SENSOR("", "Resistance Sensor", this);
11 ESP_LOGCONFIG(TAG,
12 " Configuration: %s\n"
13 " Resistor: %.2fΩ\n"
14 " Reference Voltage: %.1fV",
15 this->configuration_ == UPSTREAM ? "UPSTREAM" : "DOWNSTREAM", this->resistor_,
16 this->reference_voltage_);
17}
18void ResistanceSensor::process_(float value) {
19 if (std::isnan(value)) {
20 this->publish_state(NAN);
21 return;
22 }
23 float res = 0;
24 switch (this->configuration_) {
25 case UPSTREAM:
26 if (value == 0.0f) {
27 res = NAN;
28 } else {
29 res = (this->reference_voltage_ - value) / value;
30 }
31 break;
32 case DOWNSTREAM:
33 if (value == this->reference_voltage_) {
34 res = NAN;
35 } else {
36 res = value / (this->reference_voltage_ - value);
37 }
38 break;
39 }
40
41 res *= this->resistor_;
42 ESP_LOGD(TAG, "'%s' - Resistance %.1fΩ", this->name_.c_str(), res);
43 this->publish_state(res);
44}
45
46} // namespace resistance
47} // namespace esphome
constexpr const char * c_str() const
Definition string_ref.h:69
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7