diff --git a/esphome/components/template/switch/template_switch.cpp b/esphome/components/template/switch/template_switch.cpp index b3e545d3e9..5db346b99f 100644 --- a/esphome/components/template/switch/template_switch.cpp +++ b/esphome/components/template/switch/template_switch.cpp @@ -43,15 +43,16 @@ void TemplateSwitch::setup() { if (!this->restore_state_) return; - auto restored = this->get_initial_state(); - if (!restored.has_value()) - return; + optional initial_state = this->get_initial_state_with_restore_mode(); - ESP_LOGD(TAG, " Restored state %s", ONOFF(*restored)); - if (*restored) { - this->turn_on(); - } else { - this->turn_off(); + if (initial_state.has_value()) { + ESP_LOGD(TAG, " Restored state %s", ONOFF(initial_state.value())); + // if it has a value, restore_mode is not "DISABLED", therefore act on the switch: + if (initial_state.value()) { + this->turn_on(); + } else { + this->turn_off(); + } } } void TemplateSwitch::dump_config() {