diff --git a/esphome/components/template/datetime/template_date.cpp b/esphome/components/template/datetime/template_date.cpp index 40d0e2729a..3f6626e847 100644 --- a/esphome/components/template/datetime/template_date.cpp +++ b/esphome/components/template/datetime/template_date.cpp @@ -37,6 +37,9 @@ void TemplateDate::setup() { } void TemplateDate::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->year_ = val->year; diff --git a/esphome/components/template/datetime/template_datetime.cpp b/esphome/components/template/datetime/template_datetime.cpp index acf6dd5ea4..62f842a7ad 100644 --- a/esphome/components/template/datetime/template_datetime.cpp +++ b/esphome/components/template/datetime/template_datetime.cpp @@ -40,6 +40,9 @@ void TemplateDateTime::setup() { } void TemplateDateTime::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->year_ = val->year; diff --git a/esphome/components/template/datetime/template_time.cpp b/esphome/components/template/datetime/template_time.cpp index b27d6fc414..dab28d01cc 100644 --- a/esphome/components/template/datetime/template_time.cpp +++ b/esphome/components/template/datetime/template_time.cpp @@ -37,6 +37,9 @@ void TemplateTime::setup() { } void TemplateTime::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->hour_ = val->hour; diff --git a/esphome/components/template/number/template_number.cpp b/esphome/components/template/number/template_number.cpp index b912dc415d..145a89a2f7 100644 --- a/esphome/components/template/number/template_number.cpp +++ b/esphome/components/template/number/template_number.cpp @@ -27,6 +27,9 @@ void TemplateNumber::setup() { } void TemplateNumber::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->publish_state(*val); diff --git a/esphome/components/template/select/template_select.cpp b/esphome/components/template/select/template_select.cpp index 053f3a83fd..3ea34c3c7c 100644 --- a/esphome/components/template/select/template_select.cpp +++ b/esphome/components/template/select/template_select.cpp @@ -28,6 +28,9 @@ void TemplateSelect::setup() { } void TemplateSelect::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { if (!this->has_option(*val)) { diff --git a/esphome/components/template/sensor/template_sensor.cpp b/esphome/components/template/sensor/template_sensor.cpp index 43dd447a02..1558ea9b15 100644 --- a/esphome/components/template/sensor/template_sensor.cpp +++ b/esphome/components/template/sensor/template_sensor.cpp @@ -8,6 +8,9 @@ namespace template_ { static const char *const TAG = "template.sensor"; void TemplateSensor::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->publish_state(*val); diff --git a/esphome/components/template/text/template_text.cpp b/esphome/components/template/text/template_text.cpp index edef97ae06..a917c72a14 100644 --- a/esphome/components/template/text/template_text.cpp +++ b/esphome/components/template/text/template_text.cpp @@ -24,6 +24,9 @@ void TemplateText::setup() { } void TemplateText::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->publish_state(*val); diff --git a/esphome/components/template/text_sensor/template_text_sensor.cpp b/esphome/components/template/text_sensor/template_text_sensor.cpp index 7d38e4b0b7..024d0093a2 100644 --- a/esphome/components/template/text_sensor/template_text_sensor.cpp +++ b/esphome/components/template/text_sensor/template_text_sensor.cpp @@ -7,6 +7,9 @@ namespace template_ { static const char *const TAG = "template.text_sensor"; void TemplateTextSensor::update() { + if (!this->f_.has_value()) + return; + auto val = this->f_(); if (val.has_value()) { this->publish_state(*val);