I use multiple scripts, scheduler and Netwatch scripts to monitor electricity status. Mikrotik that runs scripts and ISP’s ONU are connected to dumb UPS (powerbank with 5,9,12v outputs), so I can get time of power loss.
My Netwatch script which checks availability of another Mikrotik not connected to UPS, so I use this script as a trigger of power outage:
DOWN:
/system script run FuncBlackoutTime
/system script run FuncDeviceStatus
:global FuncStatus
:global textSent "$[$FuncStatus]%0A\E2\9D\8C\F0\9F\94\8B <b>Electricity is down. UPS is working</b>"
:log warning "[ELECTRICITY] Blackout-hotspot enabled. Entering power saving mode"
/interface wireless cap set enabled=yes
/system routerboard settings set cpu-frequency=448MHz
/system script run SendToTelegram
UP:
/system script run FuncDeviceStatus
:global FuncStatus
:local poweronTimestamp [:timestamp]
:local poweronTime [:pick $poweronTimestamp 0 [:find $poweronTimestamp "."]]
:local blackoutTime [/file get usb1-part1/blackout/time.txt contents]
:local blackoutDuration ($poweronTime - $blackoutTime)
:global textSent "$[$FuncStatus]%0A\E2\9A\A1\F0\9F\94\8B <b>Electricity is back on. UPS is working%0A Blackout duration: $blackoutDuration</b>"
:log warning "[ELECTRICITY] Blackout-hotspot disabled. Exiting power saving mode"
/interface wireless cap set enabled=no
/system routerboard settings set cpu-frequency=auto
/system script run SendToTelegram
FuncDeviceStatus script is used to get first line of Telegram message (date,time,uptime), so I’ll omit it until request
FuncBlackoutTime script writes timestamp of power outage into file, so even on restart of Mikrotik with UPS this data is obtainable.
I also have script TelegramElectricityNotification that runs on schedule (every 30min/1 or 4 hours depending on blackout season), so I’ll see that UPS’s battery is dead and the Mikrotik turned off :
/system script run FuncDeviceStatus
:global FuncStatus
:global textSent "$[$FuncStatus]%0A\E2\9C\85 <b>Power is still here</b>"
/system script run SendToTelegram
SendToTelegram script sends messages through Telegram bot to me and another user: