Home Assistant Controlling Octoprint through MQTT - Chapter 2

What I didn't mention in the previous post, was the struggle to get it working. I'm not a programmer or a savant that can pick up anything.

The next part is wiring up other buttons for different commands, and that means using more of the Octoprint API, as of this writing, here: https://docs.octoprint.org/en/master/api/

Current issues

Ongoing problem #1: I mentioned how I got connect to work. Using the same pseudo-code, it should be super easy to execute {"command":"disconnect"}. It even pops up the same area as the command: connect did...but it doesn't disconnect.

Ongoing problem #2: Extrude/Retract - this happens frequently when changing out filament, or in some cases...the print fails to start after homing on the corners. Before the homing occurred, it retracted. If I were to just start again, it would retract again! (The nozzle needs to be preheated, extruded, and then turned off.)

Ongoing problem #3: Preheat to 230/turn off - as I just mentioned in #2, necessary for changing filament over.

Turns out, I got them all to work by the end of this post!

Disconnecting Octoprint from the Printer

Probably not even necessary, but I did it. As before, in OctoPrint Settings, under MQTT Subscribe, you need to add this:

The three entries are "Topic", Rest API, and "REST Parameters"

Topic can literally be anything. I matured my topics a little more, just in case I use a lot of MQTT in the future. First is the device, then the "plugins/mqttsubscribe" for the service, and then the action. /api/connection comes from the Octoprint API information, as does the disconnect format.

We'll use the same boolean input and button from the "Connect" in the previous post, as "Disconnect" is just the other side of things. Next, we'll head over to Node Red, to wire it up.


You'll notice we keep the original Connect path - I just copy those same orange and purple boxes, wire them to the "false" side of the Connect Mini. The orange "Disconnect" button has the same formatting that Connect, and the MiniDisconnect just needs to have its Topic switched over to whatever you used in Octoprint.

Painful reminder: if you do not match the formatting in your orange Disconnect, or the Topic in the purple MiniDisconnect, it will never work.

I say this, only because I blocked myself before today. I experimented trying to send a disconnect message over the same Topic (fail). I thought my buttons didn't work at times. Then, I saw my disconnect message come through on Octoprint's web page - but it didn't trigger. (The command is YAML - must match completely. No brackets on different lines.)

This was actually a big breakthrough for the rest of the commands. I defaulted to unique topics for each command, copy-pasted the "REST Parameter" directly into the orange box in Node Red - everything just worked.

Setting up Extrude/Retract in Octoprint MQTT Subscribe

Extrude has two lines for parameter, broken up by the comma

Retract uses the same Extrude, but -10 to pull it back

I don't know how to rig a button to *only* deliver one result. So I cheated, but it works...so...that's maybe okay.

Note the True/false go to the same place

The blue template is going to use similar input_booleans. You'll need to create one each for Extrude and Retract in the input_boolean.yaml you made before. 

From the /config/input_boolean.yaml

You will also need to add a button that references the input_boolean.octoprint_mini_extrude, and one for retract - I didn't make them toggle, as you might want to extrude multiple times in a row, or retract multiple times.

Remove "Show State" as it will always just be "Extrude more", and not on/off

I changed it to "Default Action" instead of Toggle, but...I don't think it really matters.

The blue template will look like this:

Event State Module for the Extrude function

The orange template is going to be identical, except for the 10 vs. -10.

This should be the same, exact 4 lines in the Octoprint Subscribe REST Parameter

The purple buttons are boring - the same format you used for the Connect/Disconnect. Just make sure you use the unique topics, and the same topic that you made for it in Octoprint MQTT Subscribe.

The Purple Module for Extrude 10

The Preheat Step

I'm going to take a shortcut here - you've already seen the Connect/Disconnect Mini flow.


The orange "Preheat 230" button, sets the nozzle to 230 degrees Celsius...ideal for my PETG filament.

For the "Preheat Off", Octoprint API needs a 0 instead of 230.

Over in Octoprint Settings, for MQTT Subscribe, it'll look like this:


Done!


And that's it. It works on any computer/tablet/phone that is plugged into my Home Assistant, saving me time in the future. (I can use my cellphone, instead of walking around with a computer or walking back to my desktop for each step.)

The buttons in Lovelace are...ugly. My next step is learning HACS, and looking into making the buttons smaller/prettier.
Large buttons, taking up lots of space. Ugly!

Special Bonus Feature: Node Red Flow Overview

The last 5 blue buttons are ugly

I'll include my job complete notification flow, when I figure out why it doesn't work anymore.






Comments

Popular posts from this blog

Home Assistant Controlling Octoprint through MQTT - Chapter 1

Setting up MQTT on HA and Octoprint