Power Outage Monitor

It’s been quite a while since my last project, so in order to gradually get things moving again, I came up with this simple project built around the PiJuice.

The PiJuice HAT is a portable power platform for the Raspberry Pi, opening the door to self-powered and portable projects. It was launched on Kickstarter over two years ago, and after the team faced various problems, the hardware is finally shipping.

The guys at PiSupply were kind enough to send me a review unit, including solar panels, to play around with.

Unfortunately, there’s not much sun in the winter, so I came up with an indoor project to tackle issues I am experiencing at home: random power cuts. As with any similar problem, being able to log the frequency and duration of such events could potentially reveal patterns, helping to find the root cause of the issue. The idea is simple: whenever a power cut would occur, the PiJuice would keep the Raspberry Pi running during the outage and keep track of the start and end of the power cut by logging everything to a simple CSV file.

Let’s take a closer look …

Useful Links

Before we start, here are some links you might find useful.


Various members of the Raspberry Pi community have posted their review of the PiJuice, check them out:


To get an estimation of how long the Pi would last on battery power, you can make use of the PiJuice Battery Discharge Time Calculator!


Alright, let’s dive in the details of my little project.

There are three components to this project:

  • Raspberry Pi A+
  • PiJuice HAT
  • PaPiRus HAT

I’ve picked the Raspberry Pi A+ for two reasons: it has the same form factor as the PiJuice, and it’s rather low power, comparable to a Raspberry Pi Zero.

The PiJuice HAT is in charge of keeping things running during the power outage. When an outage starts, the current time is stored, and a timer is started until power is restored. This data is then stored to file. Of course, the solution will not work for extremely long outages, as the battery would run out. But for smaller outages, it should work great.

Finally, the PaPiRus is used as a low power display for local reporting and the ability to trigger some actions via its buttons.

This slideshow requires JavaScript.


Raspberry Pi

Preparing the Raspberry Pi for any project, starts with the same steps: flashing the OS to an SD card, updating the packages and performing some configuration.

I’ve written a generic procedure that can be found here: Headless Raspberry Pi Setup with Raspbian Jessie

For this particular project, as the Pi won’t be connected to an HDMI screen, I’m using the Jessie Lite (no desktop) image.


Installing the base software for PiJuice is super easy, and can be done as follows:

pi@pijuice:~ $ sudo apt-get install pijuice-base python-smbus

The software can do a lot (just check the documentation, it’s impressive), but because this is a headless project, I’m using the Python API only.

There’s one value required for this project: whether the PiJuice is operating from the power supply or the battery. This information can be retrieved from the GetStatus() call:

#!/usr/bin/env python

from pijuice import PiJuice

pijuice = PiJuice(1,0x14)

print pijuice.status.GetStatus()

The parameter relevant for this project, is called “powerInput5vIo”:

{'data': {'battery': 'CHARGING_FROM_5V_IO', 'powerInput5vIo': 'PRESENT', 'isFault': True, 'isButton': False, 'powerInput': 'NOT_PRESENT'}, 'error': 'NO_ERROR'}

Isolating that value from the others, can easily be achieved as follows:

status = pijuice.status.GetStatus()
status = status['data']['powerInput5vIo']

The will return PRESENT or NOT_PRESENT.


Again, it doesn’t get any easier than this. Running the following command will set up the software for you:

pi@pijuice:~ $ curl -sSL | sudo bash

To control the display and make use of the buttons to have different screens and functions, I’ve reused some bits of code from my Pi Zero ePaper Badge.

The following functions have been assigned:

  • Button 1: display the power source status and keep track of downtime
  • Button 2: turn the wifi on and off for power saving
  • Button 3: future use
  • Button 4: future use


The PiJuice is a great add-on for the Raspberry Pi, and this simple project is merely scratching the surface of what it can do. It’s super easy to set up and does a great job at keeping the Pi running for quite some time. Works as advertised!


A demo log generated during testing:

pi@pijuice:~ $ cat poweroutage.csv
10-02-2018 09:13:11,PRESENT
10-02-2018 09:21:05,ABSENT
10-02-2018 09:21:09,PRESENT
10-02-2018 09:22:16,ABSENT
10-02-2018 09:22:25,PRESENT
10-02-2018 09:22:40,ABSENT
10-02-2018 09:22:53,PRESENT
10-02-2018 09:23:38,ABSENT
10-02-2018 10:29:31,PRESENT
10-02-2018 10:29:48,ABSENT
10-02-2018 10:30:01,PRESENT

Which can be imported in a spreadsheet program to create a chart:


Here’s the full code for this power logger. It’s not pretty, but it gets the job done and hopefully gives you some ideas for a project with your own PiJuice.

© Frederick Vandenbosch, 2014-2021. Unauthorised use and/or duplication of this material without express and written permission from this blog’s author and owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Frederick Vandenbosch with appropriate and specific direction to the original content.