mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[analog_threshold] Make thresholds templatable (#8452)
This commit is contained in:
		| @@ -14,7 +14,8 @@ void AnalogThresholdBinarySensor::setup() { | |||||||
|   if (std::isnan(sensor_value)) { |   if (std::isnan(sensor_value)) { | ||||||
|     this->publish_initial_state(false); |     this->publish_initial_state(false); | ||||||
|   } else { |   } 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) { |   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 there is an invalid sensor reading, ignore the change and keep the current state | ||||||
|     if (!std::isnan(sensor_value)) { |     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() { | void AnalogThresholdBinarySensor::dump_config() { | ||||||
|   LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this); |   LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this); | ||||||
|   LOG_SENSOR("  ", "Sensor", this->sensor_); |   LOG_SENSOR("  ", "Sensor", this->sensor_); | ||||||
|   ESP_LOGCONFIG(TAG, "  Upper threshold: %.11f", this->upper_threshold_); |   ESP_LOGCONFIG(TAG, "  Upper threshold: %.11f", this->upper_threshold_.value()); | ||||||
|   ESP_LOGCONFIG(TAG, "  Lower threshold: %.11f", this->lower_threshold_); |   ESP_LOGCONFIG(TAG, "  Lower threshold: %.11f", this->lower_threshold_.value()); | ||||||
| } | } | ||||||
|  |  | ||||||
| }  // namespace analog_threshold | }  // namespace analog_threshold | ||||||
|   | |||||||
| @@ -15,14 +15,13 @@ class AnalogThresholdBinarySensor : public Component, public binary_sensor::Bina | |||||||
|   float get_setup_priority() const override { return setup_priority::DATA; } |   float get_setup_priority() const override { return setup_priority::DATA; } | ||||||
|  |  | ||||||
|   void set_sensor(sensor::Sensor *analog_sensor); |   void set_sensor(sensor::Sensor *analog_sensor); | ||||||
|   void set_upper_threshold(float threshold) { this->upper_threshold_ = threshold; } |   template<typename T> void set_upper_threshold(T upper_threshold) { this->upper_threshold_ = upper_threshold; } | ||||||
|   void set_lower_threshold(float threshold) { this->lower_threshold_ = threshold; } |   template<typename T> void set_lower_threshold(T lower_threshold) { this->lower_threshold_ = lower_threshold; } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   sensor::Sensor *sensor_{nullptr}; |   sensor::Sensor *sensor_{nullptr}; | ||||||
|  |   TemplatableValue<float> upper_threshold_{}; | ||||||
|   float upper_threshold_; |   TemplatableValue<float> lower_threshold_{}; | ||||||
|   float lower_threshold_; |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace analog_threshold | }  // namespace analog_threshold | ||||||
|   | |||||||
| @@ -18,11 +18,11 @@ CONFIG_SCHEMA = ( | |||||||
|         { |         { | ||||||
|             cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor), |             cv.Required(CONF_SENSOR_ID): cv.use_id(sensor.Sensor), | ||||||
|             cv.Required(CONF_THRESHOLD): cv.Any( |             cv.Required(CONF_THRESHOLD): cv.Any( | ||||||
|                 cv.float_, |                 cv.templatable(cv.float_), | ||||||
|                 cv.Schema( |                 cv.Schema( | ||||||
|                     { |                     { | ||||||
|                         cv.Required(CONF_UPPER): cv.float_, |                         cv.Required(CONF_UPPER): cv.templatable(cv.float_), | ||||||
|                         cv.Required(CONF_LOWER): 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]) |     sens = await cg.get_variable(config[CONF_SENSOR_ID]) | ||||||
|     cg.add(var.set_sensor(sens)) |     cg.add(var.set_sensor(sens)) | ||||||
|  |  | ||||||
|     if isinstance(config[CONF_THRESHOLD], float): |     if isinstance(config[CONF_THRESHOLD], dict): | ||||||
|         cg.add(var.set_upper_threshold(config[CONF_THRESHOLD])) |         lower = await cg.templatable(config[CONF_THRESHOLD][CONF_LOWER], [], float) | ||||||
|         cg.add(var.set_lower_threshold(config[CONF_THRESHOLD])) |         upper = await cg.templatable(config[CONF_THRESHOLD][CONF_UPPER], [], float) | ||||||
|     else: |     else: | ||||||
|         cg.add(var.set_upper_threshold(config[CONF_THRESHOLD][CONF_UPPER])) |         lower = await cg.templatable(config[CONF_THRESHOLD], [], float) | ||||||
|         cg.add(var.set_lower_threshold(config[CONF_THRESHOLD][CONF_LOWER])) |         upper = lower | ||||||
|  |     cg.add(var.set_upper_threshold(upper)) | ||||||
|  |     cg.add(var.set_lower_threshold(lower)) | ||||||
|   | |||||||
| @@ -26,3 +26,17 @@ binary_sensor: | |||||||
|     threshold: 100 |     threshold: 100 | ||||||
|     filters: |     filters: | ||||||
|       - invert: |       - 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: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user