ESP32 Power Failure - State Retention

Hi guys,
Since the past year, I have been using Home Assistant to automate my lights and fan. I have installed several esp32 along with relay boards to bring the lights online. Recently, the area I reside in has started facing power failures (the power comes back within 30 mins or so). It is alright when this happens during daytime, but during night, I want my fan to kick in (restore its previous state before power failure) immediately upon power restore.
A sample YAML of esp32 being used is posted below:

esphome:
  name: behind-wardrobe
esp32:
  board: nodemcu-32s
  framework:
    type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
  encryption:
    key: "7adEoY6NdYLVUxSraGTgDS16+xvk="
ota:
  password: "fcxe751a096886"
wifi:
  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_password
      manual_ip:
        static_ip: 192.168.1.221
        gateway: 192.168.1.1
        subnet: 255.255.255.0
    - ssid: !secret wifi_ssid1
      password: !secret wifi_password1
      manual_ip:
        static_ip: 192.168.1.221
        gateway: 192.168.1.1
        subnet: 255.255.255.0
    - ssid: !secret wifi_ssid2
      password: !secret wifi_password2
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Behind-Wardrobe Fallback Hotspot"
    password: "dpWxxTC6o"
captive_portal:
   
esp32_touch:
  setup_mode: false

output:
  - platform: gpio
    id: "Switch_1"
    pin: 18
    inverted: true
  - platform: gpio
    id: "Switch_2"
    pin: 19
    inverted: true
  - platform: gpio
    id: "Switch_3"
    pin: 25
    inverted: true
   
  - platform: gpio
    id: "Switch_4"
    pin: 26
    inverted: true
   
  - platform: gpio
    id: "Switch_5"
    pin: 17
    inverted: true
   
  - platform: gpio
    id: "Switch_6"
    pin: 16
    inverted: true
   
  - platform: gpio
    id: "Switch_7"
    pin: 23
    inverted: true
   
  - platform: gpio
    id: "Switch_8"
    pin: 22
    inverted: true
   
light:
  - platform: binary
    output: "Switch_1"
    name: "Fan"
    id: switch_1_light
   
  - platform: binary
    output: "Switch_2"
    name: "Behind wall Plug"
    id: switch_2_light
    restore_mode: restore_default_on
    internal: True
   
  - platform: binary
    output: "Switch_3"
    name: "Tubelight"
    id: switch_3_light
   
  - platform: binary
    output: "Switch_4"
    name: "Side LED"
    id: switch_4_light
   
  - platform: binary
    output: "Switch_5"
    name: "Switch 5"
    id: switch_5_light
    disabled_by_default: true
   
  - platform: binary
    output: "Switch_6"
    name: "Four Spotlights"
    id: switch_6_light
   
  - platform: binary
    output: "Switch_7"
    name: "Led Strip"
    id: switch_7_light
    internal: false
   
  - platform: binary
    output: "Switch_8"
    name: "Side Mirror LED"
    id: switch_8_light
    internal: True
    restore_mode: restore_default_on

binary_sensor:
  - platform: esp32_touch
    name: "ESP32 Touch Pad PIN 13"
    pin: 13
    internal: true
    threshold: 500
    on_press:
      then:
        - light.turn_on:
            id: switch_1_light
    on_release:
      then:
        - delay: 1000ms
        - light.turn_off:
            id: switch_1_light
  - platform: esp32_touch
    name: "ESP32 Touch Pad PIN 14"
    pin: 14
    internal: true
    threshold: 500
    on_press:
      then:
        - light.turn_on:
            id: switch_3_light
    on_release:
      then:
        - delay: 1000ms
        - light.turn_off:
            id: switch_3_light
  - platform: esp32_touch
    name: "ESP32 Touch Pad PIN 27"
    pin: 27
    internal: true
    threshold: 500
    on_press:
      then:
        - light.turn_on:
            id: switch_4_light
    on_release:
      then:
        - delay: 1000ms
        - light.turn_off:
            id: switch_4_light
  - platform: esp32_touch
    name: "ESP32 Touch Pad PIN 33"
    pin: 33
    internal: true
    threshold: 500
    on_press:
      then:
        - light.turn_on:
            id: switch_6_light
    on_release:
      then:
        - delay: 1000ms
        - light.turn_off:
            id: switch_6_light

Could someone kindly guide me through this. Thanks in advance

Setting restore_mode I think will get you what you’re after.

It only works to an extent. Upon power failure, as my home assistant unit also reboots, it has no way to know the previous state and it always switches on. How to make it check the previous state once the home assistant boots up completely.