Save & restore Relay state

Hello,
I created a Relay node using the new ESPHome (v 1.10). The node itself does not have the functionality to save and restore the state of the relay when the power to NodeMCU cycles (this happens at least a few times in my apartment, I live in India).
Is there a way, I can do this through HomeAssistant. Home Assistant itself is powered through a UPS and never looses power. Here is the config for my ESPHome node.
binary_sensor:

  • platform: gpio
    pin:
    number: D5
    inverted: true
    mode: INPUT_PULLUP
    id: main_geyser_button
    name: “Main Geyser Power Button”
    on_press:
    then:
    • switch.toggle: main_geyser_relay
      switch:
  • platform: gpio
    name: “Main Geyser Relay”
    pin: D2
    id: main_geyser_relay
    restore_mode: RESTORE_DEFAULT_OFF
    sensor:
  • platform: dht
    pin: D1
    temperature:
    name: “Main Bathroom Temperature”
    humidity:
    name: "Main Bathroom Hum
1 Like

Add MQTT to the node and set the retain flag for the state. (you do have to have the MQTT broker set up)