OctoPi/OctoPrint monitoring in Node Red

Ger_W
3 min readFeb 5, 2018

--

This is my Prusa i3 (It’s a BQ Hephaestus, Mark 1), and this is a short write-up - how I started monitoring print job status with my Node Red home automation system. I’m running OctoPi v0.13, on a Pi 1 Model B+ connected to WiFi.

In the web interface — I’m going to presume that you have this much working before starting — navigate toSettings > API , you need to generate an API Key. And you must enable “cross-origin resource sharing” (CORS). With that complete, you can construct a url in the format <hostname>/api/job?apikey=<api-key>, and this is available to you in your browser. If your system supports Bonjour, you may use octopi.local as hostname, I was using the Pi’s local IP, like so:

(There are many other URL configurations & the full details of API features and usage can be found here: http://docs.octoprint.org/en/master/api/general.html)

The response, in JSON format, is shown:

JSON response

I have Node Red construct the URL as follows:

var APIKey = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
var ext = “job”;
msg.method = “GET”;
msg.url = “octopi.local/api/” + ext + “?apikey=” + APIKey;
return msg;

And I format the result just a little bit…

var sec_num = parseInt(msg.payload.progress.printTimeLeft, 10); // don’t forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num — (hours * 3600)) / 60);
var seconds = sec_num — (hours * 3600) — (minutes * 60);
if (hours < 10) {hours = “0”+hours;}
if (minutes < 10) {minutes = “0”+minutes;}
if (seconds < 10) {seconds = “0”+seconds;}
var tString = hours+’:’+minutes+’:’+seconds;msg.payload = “Printing: \”” + msg.payload.job.file.name + “\” (“ + tString + “ remaining)”;
msg.timeleft = tString;
return msg;
This is the overall flow.

With a little formatting and some help from a post by Powtac and Austin Pray to make the time a little nicer — here’s my debug tab, and a little dashboard using the Node Red Dashboard:

The Format node in the above flow, named “if printing”, continually monitors the string received on msg.payload.status and as soon as it changes from “Printing” to “Operational”, etc. initiates a further message, which you can use to show a dashboard notification, send a text/email, sound a buzzer, or whatever else.

(I will publish the code of this function shortly!)

It works really well with an ESP-8266 Scrolling Text Notifier, one of which I built last Summer. Any String sent to the topic “/frontroom/display”, like so:

Thanks for reading! The API goes into more detail, and you can build much more functionality, read logs, update things, even send commands — leave a comment to let me know if you’re building something similar, if you’re adding to the above, or you have any comments.

--

--