How to know when there is a power cut ?
It all started when last week I finally decided to build myself a new Gaming PC (A computer with a high amount of computing power) because my MacBook Pro 2019 doesn’t come with a graphics card pre-installed. And the lack of it was constantly hitting me in several cases like- training a machine learning model, designing a 3d character, editing a 4K video or anything that requires or works better with parallel processing. Also, I wanted to get started with game development, so having a powerful PC always has many advantages 💪
So I finally designed the setup and placed the order for all the components on Amazon. While doing so, I also came along with some problems and drawbacks (All of them are fixed now). I was designing my setup in a way so that I can access it remotely anytime I wanted to (BTW, I used Parsec for remote controlling). But one of the most amazing challenges was to figure out when there is a power cut and the house is running on the backup power supply. Because in that case, I would simply shut down the power-hungry PC and won’t let it drain the backup batteries too soon.
So after searching a lot everywhere for a device or a solution that can notify me whenever there is a power cut, I found nothing. Also, I wanted to get notified only when I am using the PC (I hate constant notification alerts 😑). Suddenly, an idea struck my brain 😀.
There are IoT devices all around my house. And one of them is my smart water pump, which is the only device that doesn’t utilize the power backup and works only when there is power from the mains. So lets write a script that would constantly check if the pump is online or offline by simply pinging it. And at last, just make sure that the script starts as soon as the computer boots up.
So here’s the script that I wrote for Windows OS. This simply sends you a toast alert every two minutes when there is a power cut and alerts you again as soon as the power is restored.
How to use ?
Great, so let me guide you through the super simple process of setting this up. And believe me, you need no coding experience to set this up. It’s just super easy 😉
1. Install Python
Since this script is written in python, you should make sure that your PC has Python installed on it. So go ahead and download Python from here and install it by the simple next-next-finish process of Windows OS.
2. Install Libraries
Don’t get scared. You just need to paste two commands one by one in your windows command prompt.
pip install win10toastpip install ping3
3. Download Files
Download the script and the notification icons and place all of them inside any folder you want.
4. Customize Script
There are a few things that you would like to change in the script. So lets explore the customization options.
- Find the device’s IP address from the router page which you would like to ping to and change it inside this parameter- device_ip_address = “xxx.xxx.x.xxx”
- Change the notification_icon_online & notification_icon_offline, icon paths.
notification_icon_online = “C:\Users\username\downloads\power_restored.ico”notification_icon_offline = “C:\Users\username\downloads\power_down.ico”
5. Launch on Boot
Now we just need to make sure that the script runs automatically whenever you log in. For this, create a shortcut file for the script and place it inside this folder- “C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup”
BTW, this is a hidden folder. So make sure that you unhide it first 👁️
6. Congratulations ! You made it 🎉
Thanks for reading through this article! If you enjoyed it, please click the 👏 button & feel free to leave your response below.