ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
analog_threshold_binary_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace analog_threshold {
6
7static const char *const TAG = "analog_threshold.binary_sensor";
8
10 float sensor_value = this->sensor_->get_state();
11
12 // TRUE state is defined to be when sensor is >= threshold
13 // so when undefined sensor value initialize to FALSE
14 if (std::isnan(sensor_value)) {
15 this->publish_initial_state(false);
16 } else {
17 this->publish_initial_state(sensor_value >=
18 (this->lower_threshold_.value() + this->upper_threshold_.value()) / 2.0f);
19 }
20}
21
23 this->sensor_ = analog_sensor;
24
25 this->sensor_->add_on_state_callback([this](float sensor_value) {
26 // if there is an invalid sensor reading, ignore the change and keep the current state
27 if (!std::isnan(sensor_value)) {
28 this->publish_state(sensor_value >=
29 (this->state ? this->lower_threshold_.value() : this->upper_threshold_.value()));
30 }
31 });
32}
33
35 LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this);
36 LOG_SENSOR(" ", "Sensor", this->sensor_);
37 ESP_LOGCONFIG(TAG,
38 " Upper threshold: %.11f\n"
39 " Lower threshold: %.11f",
40 this->upper_threshold_.value(), this->lower_threshold_.value());
41}
42
43} // namespace analog_threshold
44} // namespace esphome
void publish_state(bool new_state)
Publish a new state to the front-end.
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
Base-class for all sensors.
Definition sensor.h:59
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:97
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:60
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7