Publish PiJuice UPS information to MQTT for consumption by, for example, Node-RED and Home Assistant
The PiJuice UPS hats by PiSupply make very nice battery backups for Raspberry Pi's. I use mine so that Home Assistant (HA) continues to run during Loadshedding in South Africa, and to correctly shut down the Pi when the battery runs low.
Existing Node-RED and HA integrations assume Node-RED and HA are running on the same Pi that has the PiJuice. This doesn't work when running in Docker Swarm mode (as I do). I wanted a service that runs on each Pi and publishes PiJuice stats to MQTT. From there I can monitor battery level, and start automations when the Pi loses or regains wall power.
This installation assumes that you already have the pijuice_cli
working. To test this, on your command line type pijuice_cli
and you should be presented with the PiJuice CLI. If not, follow the Installation Guide.
- Either clone or download this repo into a folder on your Pi
git clone git@github.com:dalehumby/PiJuice-MQTT.git
- Open the folder
cd PiJuice-MQTT
- Rename the config file
mv config.yaml.example config.yaml
- Open the
config.yaml
file and change the MQTT broker IP/hostname and the broker username/password. If no username/password is required to use your broker then delete theusername
andpassword
lines - Add the required libraries
pip3 install -r requirements.txt
- To test the code is working, on the command line enter
python3 pijuicemqtt.py
. You should not see any errors or stack traces
Every 30 seconds the script published to the topic pijuicemqtt/<yourpihostname>/status
, typically pijuicemqtt/raspberrypi/status
if you use the default hostname on your Pi.
Example payload:
{
"batteryCharge": 99,
"batteryVoltage": 4.161,
"batteryCurrent": -0.427,
"batteryTemperature": 47,
"batteryStatus": "NORMAL",
"powerInput": "NOT_PRESENT",
"powerInput5vIo": "PRESENT",
"ioVoltage": 5.123,
"ioCurrent": 0.21
}
The titles and values come directly from the PiJuice Python library and are unaltered, except for changing mA and mV to A and V. For details see PiSupply's I2C Command API and their Python code.
Briefly:
batteryCharge
is percent 0 to 100 %batteryVoltage
is typically 3.2 V min and 4.2 V maxbatteryCurrent
is the charging/discharge of the battery. Negative means in to the batterybatteryTemperature
in degrees CelsiusbatteryStatus
whereNORMAL
is fully charged, andCHARGING_FROM_IN
orCHARGING_FROM_5V_IO
while charging from USB, depending on whether the source is the PiJuice USB (IN
) or Pi's USB (5V_IO
)powerInput
will bePRESENT
if USB is plugged into the PiJuice hatpowerInput5vIo
will bePRESENT
if USB is plugged into the Raspberry Pi board (as it is here), and the PiJuice is getting power via the headers on the PiioVoltage
is the Pi's 5 V rail. Should be 5.0 V to max 5.25 VioCurrent
is current flowing to/from the Raspberry Pi. Negative means from the PiJuice to Pi; Positive from the Pi to PiJuice
The stats above show that the battery is fully charged, and the boards are powered via the USB connector on the Raspberry Pi. There are no errors and all voltages look good.
To automatically add the PiJuice device and entities to Home Assistant, in config.yaml
set the homeassistant
topic
and sensor: true
. (You will also need the MQTT Integration setup in Home Assistant.)
A device called <youpihostname> PiJuice
will be added to the device registry, along with two entities:
<youpihostname> PiJuice Battery
battery charge percentage as asensor
<youpihostname> PiJuice PowerInput5vIo
status of the 5 V IO power input as abinary_sensor
, whereoff
means no power, andon
means wall power is present
All information from the MQTT payload is added as an attribute
to the sensors so you can get more detailed info like voltages and currents, if you need them.
You can then show this information to your Lovelace dashboards, add badges and automations.
To run pijuicemqtt.py
as a background service on the Pi:
- Create the service file
sudo cp pijuicemqtt.service /lib/systemd/system/pijuicemqtt.service
- Open the service file
sudo nano /lib/systemd/system/pijuicemqtt.service
- Change the paths in
WorkingDirectory
andExecStart
to the location ofpijuicemqtt.py
. Also check thepython
path is correct. Save and exit
Then on the command line:
sudo systemctl daemon-reload
sudo systemctl enable pijuicemqtt.service
sudo systemctl start pijuicemqtt.service
sudo systemctl status pijuicemqtt.service
You should see something like:
● pijuicemqtt.service - PiJuice to MQTT
Loaded: loaded (/lib/systemd/system/pijuicemqtt.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-06-06 09:03:24 UTC; 4s ago
Main PID: 1548 (python3)
Tasks: 2 (limit: 4915)
CGroup: /system.slice/pijuicemqtt.service
└─1548 /usr/bin/python3 /media/cluster/config/PiJuice-MQTT/pijuicemqtt.py
Jun 06 09:03:24 homepi systemd[1]: Started PiJuice to MQTT.
The background service is now running and will continue to publish to MQTT every 30 seconds. The script will restart if it crashes, and after Pi restart.
You can stop the script by sudo systemctl stop pijuicemqtt.service