diff --git a/esphome/components/modbus_controller/switch/modbus_switch.cpp b/esphome/components/modbus_controller/switch/modbus_switch.cpp index dd12e2867f..3a679fbeb8 100644 --- a/esphome/components/modbus_controller/switch/modbus_switch.cpp +++ b/esphome/components/modbus_controller/switch/modbus_switch.cpp @@ -6,7 +6,17 @@ namespace modbus_controller { static const char *const TAG = "modbus_controller.switch"; -void ModbusSwitch::setup() {} +void ModbusSwitch::setup() { + optional initial_state = Switch::get_initial_state_with_restore_mode(); + if (initial_state.has_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 ModbusSwitch::dump_config() { LOG_SWITCH(TAG, "Modbus Controller Switch", this); } void ModbusSwitch::parse_and_publish(const std::vector &data) {