Levin Rainey
Posts: 2
Joined: Wed Sep 05, 2018 9:57 am

I need help please - power cut notification

Wed Sep 05, 2018 10:06 am

Thank you very much for reading this!
Anyway, I program just for fun, but I don't really do any physical projects (except I did the 'Pi Music Box' thing in a past Magpi).
A few weeks ago we had a holiday for two weeks and when we came back we found out we had had a power cut. When we checked the fridge, everything was rotten!
Please help on how to program something so that when we have a power cut it automatically sends a notification to a phone. My initial ideas are:
Something to do with a UPS (uninterruptible power supply/source) and send notification the via the internet??
Thanks alot,
Levin :)

User avatar
Posts: 1388
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website


Wed Sep 05, 2018 1:22 pm

First, the obvious question: if the power is out how will you reach the internet? No power = no router = no internet.

One option is a UPS that can power both the Pi and your router. There others but all of them depnd on how much you want to speend.

Whatever you use for off grid power, it needs to be able to signal the Pi that mains power is out.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

Posts: 3536
Joined: Fri Jun 08, 2012 6:03 pm


Wed Sep 05, 2018 1:45 pm

I'd propose a 3G phone module like the Adafruit FONA.

With this you could either call your phone with a pre-recorded message, send a text message or access the internet to send a message.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
Posts: 635
Joined: Mon Nov 18, 2013 3:11 am

Re: I need help please - power cut notification

Fri Sep 07, 2018 2:05 pm

Look at the script that emails you the IP address of your PI on power up. When i get a chance I plan on modifying it to send me an email that my power has come back on since my PI will boot up when power is restored. Right now if my power goes out I just try to log into my pivpn every so often and when it connects i know my power is back on.
Two heads are better than one, unless one's a goat head.

User avatar
Posts: 2
Joined: Fri Sep 07, 2018 5:07 am

Re: I need help please - power cut notification

Fri Sep 07, 2018 9:12 pm

Why not write status information to a file, and check it on startup?

It could be as simple as create a file ('running') at startup, and delete it at normal shutdown.
At startup, test if it's there; if it is, you had an abnormal shutdown (so assume power failure).
At abnormal shutdown, send a notification to you.

That's not very flexible, and rather ugly, though.

You could write it as a INI file using configparser (Python 3.x) or ConfigParser (Python 2.x).
That way it's a simple text file, easily updated, and you can change values yourself to test different conditions.
You can add other settings to it as you want them (e.g., start time, notification methods, phone number, email address, etc)
(I prefer ConfigObj because it preserves comments and is more powerful, but it's more difficult to use; the documentation could be better. Google usually finds answers though.)

For example:

Code: Select all

    running = True
    started = '2018-0907-1628'
    qtyNotifications = 2

    active = True
    method = text
    address = +1-123-555-1234

    active = False
    method = email
    address = [email protected]
Like before, when you start, if 'running' is True, you had an abnormal shutdown (assume it's a power failure; send notifications)
If it's False, you start up normally, writing the started (date and time) and running (True) values.
If running is missing, it's the first time you've started, so same as running = False; normal startup.
When you shut down, set running to False.

You might wrap your code in a try/except so you can write running = False in the event of a code bug. Bugs are abnormal shutdowns, but they're not power failures. You could even send notifications of the exception.
Wrap all that in a while loop to restart it again. (use a flag; set to True at startup, set to False for normal shutdown:

Code: Select all

    restart = True

    while restart:
            # do my stuff
        except Exception as e:
            # handle the exception - at least print() it, or log it somewhere
            # leave restart True, so we *will* restart
            # treat other exceptions (KeyboardInterrupt, SystemExit, etc) as normal shutdown
            restart = False
            # write False to INI file  (so we won't send an 'abnormal shutdown'
            # notification if we restart)
            # do other cleanup

Levin Rainey
Posts: 2
Joined: Wed Sep 05, 2018 9:57 am

Re: I need help please - power cut notification

Sat Mar 16, 2019 11:33 am

Thank you very much everyone for replying. I will take a look and make further developments. Thanks again,

Return to “Other projects”