# PiJuice status to NodeRed

**URL:** <https://discourse.nodered.org/t/pijuice-status-to-nodered/14243>\
**Category:** Dashboard\
**Created:** [12 August 2019 07:49 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243 "2019-08-12T07:49:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjamingotke](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@benjamingotke](https://discourse.nodered.org/u/benjamingotke)\
**Post date:** [12 August 2019 07:49 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/1 "2019-08-12T07:49:38Z")

</div>

Hi All!  
I'm working on a project with a Raspberry Pi with Node-Red. I would like to have the dashboard showing the status of the battery of the PiJuice-hat and the informations comming there. Does anyone know how to do that?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 August 2019 08:50 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/2 "2019-08-12T08:50:47Z")

</div>

Is it possible to access that information from the Pi? If so then how?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [12 August 2019 08:56 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/3 "2019-08-12T08:56:58Z")

</div>

The PiJuice GUI app uses a python library on the inside. You could either create a python script around that library and call it from an exec, or create a JavaScript solution from it. It accesses the hat over the I2C bus.

> <https://github.com/PiSupply/PiJuice/blob/master/Software/Source/pijuice.py>

---

<div class="post-metadata">

**Author:** ![benjamingotke](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@benjamingotke](https://discourse.nodered.org/u/benjamingotke)\
**Post date:** [12 August 2019 13:37 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/4 "2019-08-12T13:37:45Z")

</div>

Thanks for the ideas, but I do not have any knowlegde about how to do that. I'm not familiar with python or JavaScript..

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [12 August 2019 14:12 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/5 "2019-08-12T14:12:57Z")

</div>

Like @afelix is saying:

Save that file somewhere on your raspberry and use node-red (inject node + exec node + debug node), specify the path of the file in the exec node, click the inject node and watch the magic happen.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 August 2019 14:27 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/6 "2019-08-12T14:27:22Z")

</div>

...and untick the append payload option...

---

<div class="post-metadata">

**Author:** ![benjamingotke](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@benjamingotke](https://discourse.nodered.org/u/benjamingotke)\
**Post date:** [13 August 2019 04:10 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/7 "2019-08-13T04:10:18Z")

</div>

I've tryed that but the debug tells me that I do not have the permissions, so i'm not getting any usefull outputs..

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [13 August 2019 08:54 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/8 "2019-08-13T08:54:25Z")

</div>

> [@bakman2](#):
>
> Save that file somewhere on your raspberry and use node-red (inject node + exec node + debug node), specify the path of the file in the exec node

Hi @bakman2, I have never used python in combination with Node-RED. But is it also possible to copy the content of the py file into e.g. the (python editor of the) [node-red-contrib-python3-function](https://flows.nodered.org/node/node-red-contrib-python3-function) node?  
Thanks!  
Bart

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [13 August 2019 11:14 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/9 "2019-08-13T11:14:28Z")

</div>

Hi Bart (@BartButenaers),  
Based on this comment on the github issues of the repository this was forked from it should in theory work: [https://github.com/arnauorriols/node-red-contrib-python-function/issues/3#issuecomment-406710989](https://github.com/arnauorriols/node-red-contrib-python-function/issues/3#issuecomment-406710989)  
However, I would personally not advise this approach, I’ve done so in the past in production to get web applications running. What this does is dependency injection at runtime, and unless you are quite familiar with all the underlying libraries, it brings risks for errors. On top of that, the initial library still comes with a big warning to not use it on production. The library you linked just appears to add the much needed python3 support to the mix. Much needed as Python 2 will be EOL in just under 4 months. But it is still unsure if this library is stable.

A better idea would be to write a small wrapper script around the PiJuice python file with command line parameters, then call it from either an exec node or a custom node wrapper.

Edit: I was answering this based on importing that script into a python-function node, rather than pasting. This script is using hardware level libraries to interact with the I2C bus on the raspberry pi. I have absolutely no clue if this node is capable in correctly running a python setup with those calls without a full debug run, and I don’t have the hat or similar hardware I can connect to an I2C bus.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [13 August 2019 11:46 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/10 "2019-08-13T11:46:01Z")

</div>

> [@afelix](#):
>
> A better idea would be to

....extend the file with mqtt support. It's a long file, haven't looked deep enough, but somewhere & somehow the status is sent. Otherwise it would not be difficult to add mqtt and publish the battery status to a broker. Provided you know enough Python

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [13 August 2019 12:31 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/11 "2019-08-13T12:31:53Z")

</div>

That works too, definitely. Is normally my first suggestion so I can’t believe I didn’t think of it now. 😂 Thanks.

> [@krambriw](#):
>
> It's a long file, haven't looked deep enough, but somewhere & somehow the status is sent.

I actually read through the entire thing beforehand. What this thing does is it defines a complete interface on connecting to the thing. At the very bottom an outer class is defined that contains interfaces to connect to all parts, from the status of the battery, to controlling wake up/shut down, to the RTC status, and so on. The sending of the status is done by calling functions on this interfaces, and the status is returned. To work around it, a small wrapper around that outer class that will send occasionally over mqtt would work, or from a command infrastructure that when a message is published on a control topic it is subscribed to, it will publish status on another.

---

<div class="post-metadata">

**Author:** ![benjamingotke](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@benjamingotke](https://discourse.nodered.org/u/benjamingotke)\
**Post date:** [15 August 2019 09:26 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/12 "2019-08-15T09:26:43Z")

</div>

This got a little to technical for me. I'm not very experience in coding. I'm mostly working with the physical products like battery-systems and automation with relays and so on. It is possible for you to help me out here?  
/Benjamin

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [20 August 2019 06:32 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/13 "2019-08-20T06:32:02Z")

</div>

So I just looked at this for fun (I do not have a pijuice, even if it looks tempting to get one, so I cannot properly test what I have done)

I wrote a small python script that can be called via the cmd line or from the exec node in NR. It will return requested data as mqtt messages that can be captured by NR. See the flow sample below. In this first version, just some status and power messages are treated.

Pre-requisites:

1. Expecting

- running Python3
- this script will run in the same pi as where the pijuice is installed

1. Install the mosquitto broker ([https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/](https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/))  
sudo apt update  
sudo apt install -y mosquitto  
sudo systemctl enable mosquitto.service

2. Install the Paho mqtt client  
sudo pip3 install paho-mqtt

3. Install PiJuice software ([https://github.com/PiSupply/PiJuice](https://github.com/PiSupply/PiJuice))  
sudo apt-get install pijuice-gui  
reboot the pi

4. I also had to install smbus separately  
sudo pip3 install smbus

5. Now copy the attached files to your Pi, I have them myself in /home/pi. Change the file types (extensions) to .py  
[pijuice4nr.txt](https://discourse.nodered.org/uploads/short-url/jv5i1ikDDGmmL0owy6Nm2jl3aSd.txt) (581 Bytes)  
[pijuice.txt](https://discourse.nodered.org/uploads/short-url/dqv50R0BVXpQfc0Qs4WHGp3k7qC.txt) (58.2 KB)

6. Create this simple flow in NR

```auto
[{"id":"c0d45a58.55b8f8","type":"mqtt in","z":"797a7c58.e82134","name":"","topic":"pijuice/#","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":970,"y":160,"wires":[["eee81c93.820e4"]]},{"id":"a7fe7e1b.d30a8","type":"debug","z":"797a7c58.e82134","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":290,"wires":[]},{"id":"a6c3311e.75d59","type":"inject","z":"797a7c58.e82134","name":"Get data","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":970,"y":80,"wires":[["dee8d33a.10619"]]},{"id":"dee8d33a.10619","type":"exec","z":"797a7c58.e82134","command":"python3 /home/pi/pijuice4nr.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1240,"y":80,"wires":[[],[],[]]},{"id":"aa7c3e9c.fb29e","type":"json","z":"797a7c58.e82134","name":"","property":"payload","action":"obj","pretty":false,"x":960,"y":290,"wires":[["a7fe7e1b.d30a8"]]},{"id":"eee81c93.820e4","type":"function","z":"797a7c58.e82134","name":"","func":"p = msg.payload;\nmsg.payload = p.replace(/'/g, '\"');\nreturn msg;","outputs":1,"noerr":0,"x":960,"y":230,"wires":[["aa7c3e9c.fb29e"]]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/09840682b3a4a79d2baeb09dbfbed700f58501c8.png)

Now try it and see what you receive (obviously I get communication error since I do not have the pijuice)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [20 August 2019 06:48 UTC](https://discourse.nodered.org/t/pijuice-status-to-nodered/14243/14 "2019-08-20T06:48:38Z")

</div>

Actually, you can use this simpler code in the function node

```auto
msg.payload = msg.payload.replace(/'/g, '"');
return msg;

```
