Community discussions

MikroTik App
 
djferdinad
just joined
Topic Author
Posts: 17
Joined: Wed Apr 06, 2022 5:12 am

Power outage notification

Mon Nov 27, 2023 8:42 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Power outage notification

Mon Nov 27, 2023 9:06 pm

look for existing posts from me and eldoncito2019
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Power outage notification

Mon Nov 27, 2023 11:47 pm

 
error404
just joined
Posts: 10
Joined: Sat Oct 21, 2023 1:58 pm

Re: Power outage notification

Thu Nov 30, 2023 3:29 pm

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:

Image
/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:

Image
/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 :

Image
/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
}

Who is online

Users browsing this forum: No registered users and 37 guests