Hi Community,
I would like to setup a notification by telegram bot. When there was a power outage to tell me:
1. Last time seen
2. for how long the unit was powered off
For example: Last seen at [Date/time] and the router was off for $hours, $minutes. Actual [date/time].
Something like it, but the most important thing is to have the off time.
I have the Telegram Bot already sending some other things. Just need the off time.
How could i reach this?
Thanks
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.
:local filename "usb1-part1/blackout/time"
:local blackoutTimestamp [:timestamp]
:local blackoutTime [:pick $blackoutTimestamp 0 [:find $blackoutTimestamp "."]]
:if ([:len [/file find name=$filename]] = 0) do={
/file print file=$filename
delay 1
}
/file set $filename contents=$blackoutTime
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:
:global textSent
:local telegramUsers {yourchatid_1;yourchatid_2}
:local botID "yourbotid"
:foreach chatID in $telegramUsers do={
/tool fetch url="https://api.telegram.org/bot$botID/sendmessage?chat_id=$chatID&text=$textSent&parse_mode=HTML" keep-result=no
}