Turn on Fan when the AC goes offline with sonoff Mini R2

Hi HA Folks,
I am trying to do an automation with Sonoff Mini R2 & HA runs on Rpi 3B+

what i have done so far:
I have followed this guide and have completed till the section before ESPHome Firmware

My Automation Intent:
When this automations is activated, When my AC goes down ie (Main’s power outage), I would like to wait for few minutes and switch on my ceiling fan, and again turn off my fan when the AC comes back online.
I have added an automation in my HomeAssistant using Hall Effect Sensor to detect power outage

help i seek:
Now i am about flash my Sonoff Mini R2 with the script given in the above guide which is made for light, now my question is can i go ahead and flash that by changing from light to fan or would i need some extra configurations to make my required automation work ? Please advice

Using postman i have already made changes to my sonoff mini to startup by default, so that standard functionality of Fan when switch is turned on is not affected.

"switch": "on",
"startup": "on",
"pulse": "off",
"pulseWidth": 500,
"ssid": "",
"otaUnlock": true,
"fwVersion": "3.6.0",
"deviceid": "",
"bssid": "",
"signalStrength": -48

posted here the info i get when i run check on zeroconf/info


I have enabled sonoff to work has a seperate entitty.
Now i would like to write an automation such that

  1. When i turn on the helper toggle button in the dashboard, an automation should get enabled
  2. It should start listening to the main’s power sensor state change and do its respective conditions ie
    1. if the power state turn from on to off,
      a) wait for a delay of 3 minutes
      b) check again if the power state is still off
      c) turn on the sonoff to switch on the fan
    2. If the power state turns from off to on
      a) wait for a delay of 5 minutes
      b) check again if the power is still on
      c) turn off the sonoff to switch off the fan

when the helper toggle button is turned off this listeners have to be cancelled and the sonoff should be set back to on state to allow the fan to work directly by switching on and off the switch.

Note: The AC is not connected to the inverter and thus in the middle of the night if power goes off, i would like to turn on the fan when the main’s power goes down and turn off the fan when the Main’s power and AC comes on.