From c4539e10fb10ba6a9464a78df8febd956ce42343 Mon Sep 17 00:00:00 2001 From: Chris Nussbaum Date: Wed, 3 May 2023 17:16:00 -0500 Subject: [PATCH] Revert "Template sensors always publish on update interval (#2224)" (#4774) --- .../components/template/sensor/template_sensor.cpp | 13 ++++++------- .../template/text_sensor/template_text_sensor.cpp | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/esphome/components/template/sensor/template_sensor.cpp b/esphome/components/template/sensor/template_sensor.cpp index b28eb3fed2..f2d0e7363e 100644 --- a/esphome/components/template/sensor/template_sensor.cpp +++ b/esphome/components/template/sensor/template_sensor.cpp @@ -8,13 +8,12 @@ namespace template_ { static const char *const TAG = "template.sensor"; void TemplateSensor::update() { - if (this->f_.has_value()) { - auto val = (*this->f_)(); - if (val.has_value()) { - this->publish_state(*val); - } - } else if (!std::isnan(this->get_raw_state())) { - this->publish_state(this->get_raw_state()); + if (!this->f_.has_value()) + return; + + auto val = (*this->f_)(); + if (val.has_value()) { + this->publish_state(*val); } } float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; } diff --git a/esphome/components/template/text_sensor/template_text_sensor.cpp b/esphome/components/template/text_sensor/template_text_sensor.cpp index 83bebb5bcf..885ad47bbf 100644 --- a/esphome/components/template/text_sensor/template_text_sensor.cpp +++ b/esphome/components/template/text_sensor/template_text_sensor.cpp @@ -7,13 +7,12 @@ namespace template_ { static const char *const TAG = "template.text_sensor"; void TemplateTextSensor::update() { - if (this->f_.has_value()) { - auto val = (*this->f_)(); - if (val.has_value()) { - this->publish_state(*val); - } - } else if (this->has_state()) { - this->publish_state(this->state); + if (!this->f_.has_value()) + return; + + auto val = (*this->f_)(); + if (val.has_value()) { + this->publish_state(*val); } } float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; }