diff --git a/esphome/components/analog_threshold/analog_threshold_binary_sensor.cpp b/esphome/components/analog_threshold/analog_threshold_binary_sensor.cpp index f679b9994f..8dcbb2ac4b 100644 --- a/esphome/components/analog_threshold/analog_threshold_binary_sensor.cpp +++ b/esphome/components/analog_threshold/analog_threshold_binary_sensor.cpp @@ -14,7 +14,8 @@ void AnalogThresholdBinarySensor::setup() { if (std::isnan(sensor_value)) { this->publish_initial_state(false); } else { - this->publish_initial_state(sensor_value >= (this->lower_threshold_ + this->upper_threshold_) / 2.0f); + this->publish_initial_state(sensor_value >= + (this->lower_threshold_.value() + this->upper_threshold_.value()) / 2.0f); } } @@ -24,7 +25,8 @@ void AnalogThresholdBinarySensor::set_sensor(sensor::Sensor *analog_sensor) { this->sensor_->add_on_state_callback([this](float sensor_value) { // if there is an invalid sensor reading, ignore the change and keep the current state if (!std::isnan(sensor_value)) { - this->publish_state(sensor_value >= (this->state ? this->lower_threshold_ : this->upper_threshold_)); + this->publish_state(sensor_value >= + (this->state ? this->lower_threshold_.value() : this->upper_threshold_.value())); } }); } @@ -32,8 +34,8 @@ void AnalogThresholdBinarySensor::set_sensor(sensor::Sensor *analog_sensor) { void AnalogThresholdBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this); LOG_SENSOR(" ", "Sensor", this->sensor_); - ESP_LOGCONFIG(TAG, " Upper threshold: %.11f", this->upper_threshold_); - ESP_LOGCONFIG(TAG, " Lower threshold: %.11f", this->lower_threshold_); + ESP_LOGCONFIG(TAG, " Upper threshold: %.11f", this->upper_threshold_.value()); + ESP_LOGCONFIG(TAG, " Lower threshold: %.11f", this->lower_threshold_.value()); } } // namespace analog_threshold diff --git a/esphome/components/analog_threshold/analog_threshold_binary_sensor.h b/esphome/components/analog_threshold/analog_threshold_binary_sensor.h index 619aef1075..efb8e3c90c 100644 --- a/esphome/components/analog_threshold/analog_threshold_binary_sensor.h +++ b/esphome/components/analog_threshold/analog_threshold_binary_sensor.h @@ -15,14 +15,13 @@ class AnalogThresholdBinarySensor : public Component, public binary_sensor::Bina float get_setup_priority() const override { return setup_priority::DATA; } void set_sensor(sensor::Sensor *analog_sensor); - void set_upper_threshold(float threshold) { this->upper_threshold_ = threshold; } - void set_lower_threshold(float threshold) { this->lower_threshold_ = threshold; } + template void set_upper_threshold(T upper_threshold) { this->upper_threshold_ = upper_threshold; } + template void set_lower_threshold(T lower_threshold) { this->lower_threshold_ = lower_threshold; } protected: sensor::Sensor *sensor_{nullptr}; - - float upper_threshold_; - float lower_threshold_; + TemplatableValue upper_threshold_{}; + TemplatableValue lower_threshold_{}; }; } // namespace analog_threshold diff --git a/esphome/components/analog_threshold/binary_sensor.py b/esphome/components/analog_threshold/binary_sensor.py index 775b3e6bbf..b5f87b9b5c 100644 --- a/esphome/components/analog_threshold/binary_sensor.py +++ b/esphome/components/analog_threshold/binary_sensor.py @@ -18,11 +18,11 @@ CONFIG_SCHEMA = ( { cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor), cv.Required(CONF_THRESHOLD): cv.Any( - cv.float_, + cv.templatable(cv.float_), cv.Schema( { - cv.Required(CONF_UPPER): cv.float_, - cv.Required(CONF_LOWER): cv.float_, + cv.Required(CONF_UPPER): cv.templatable(cv.float_), + cv.Required(CONF_LOWER): cv.templatable(cv.float_), } ), ), @@ -39,9 +39,11 @@ async def to_code(config): sens = await cg.get_variable(config[CONF_SENSOR_ID]) cg.add(var.set_sensor(sens)) - if isinstance(config[CONF_THRESHOLD], float): - cg.add(var.set_upper_threshold(config[CONF_THRESHOLD])) - cg.add(var.set_lower_threshold(config[CONF_THRESHOLD])) + if isinstance(config[CONF_THRESHOLD], dict): + lower = await cg.templatable(config[CONF_THRESHOLD][CONF_LOWER], [], float) + upper = await cg.templatable(config[CONF_THRESHOLD][CONF_UPPER], [], float) else: - cg.add(var.set_upper_threshold(config[CONF_THRESHOLD][CONF_UPPER])) - cg.add(var.set_lower_threshold(config[CONF_THRESHOLD][CONF_LOWER])) + lower = await cg.templatable(config[CONF_THRESHOLD], [], float) + upper = lower + cg.add(var.set_upper_threshold(upper)) + cg.add(var.set_lower_threshold(lower)) diff --git a/tests/components/analog_threshold/common.yaml b/tests/components/analog_threshold/common.yaml index b5c14dfe56..44d79756b5 100644 --- a/tests/components/analog_threshold/common.yaml +++ b/tests/components/analog_threshold/common.yaml @@ -26,3 +26,17 @@ binary_sensor: threshold: 100 filters: - invert: + - platform: analog_threshold + name: Analog Threshold 3 + sensor_id: template_sensor + threshold: !lambda return 100; + filters: + - invert: + - platform: analog_threshold + name: Analog Threshold 4 + sensor_id: template_sensor + threshold: + upper: !lambda return 110; + lower: !lambda return 90; + filters: + - invert: