Home Assistant Controlling Octoprint through MQTT - Chapter 1

I'm going to start listing software versions - sometimes I hit a wall, because of dated information. Home Assistant evolves continually...a year from now, this might have a different solution.

Components and their versions:

  • Home Assistant Core 2021.1.0 (HA)
  • Operating System 5.9 (HA)
  • Node Red 7.2.11 (HA)
  • Mosquitto Broker 5.1 (HA)
  • Octoprint 1.5.2
  • MQTT 0.8.7 (Octoprint plugin)
  • MQTT-Subscribe 0.1.6 (Octoprint plugin)
  • MQTT Explorer 0.3.5 (Linux, optional) 

My goal is to create buttons in a Lovelace dashboard, to trigger actions for my two 3D printers. Actions like "connect/disconnect" to start a connection between Octoprint and the printer, "preheat nozzle" so I can have the nozzle go to 180 degrees Celsius to change filament, "Extrude" to extrude 10mm of filament, or "Retract" to retract 10mm of filament, and maybe a "heat bed" so I can heat up the bed to remove a print.

I'll start with a "Connect" button for my Lulzbot Mini.

Step 0 - Make sure you have your Octoprint MQTT-Subscribe set up

As detailed in the previous post. If you don't have the topic, Octoprint won't listen for it. If you don't give it the Rest API, it won't know what the command needs to be applied to. REST Parameters...I'm not 100% sure on, but I'll cover that more in-depth in the next post. (This was "Step 3 - Install MQTT-Subscribe on Octoprint" in the previous blog post.)

Step 1 - Create the Boolean Input

After installing all of the plugins, I started with the first button - "connect/disconnect". We need a boolean input behind the scenes to attach the dashboard button. As this will be my first boolean input, I added a line to my configuration.yaml - a simple line of:

As an entry in config/configuration.yaml

This tells HA that it also needs to look at the input_boolean configuration file. I could have dumped the boolean inputs in the basic configuration file, but it gets messy.

Next, I use File editor to create a new file, "config/input_boolean.yaml". At this point, I don't have icons (I'll add a # to comment it out for now), so I really just need to put the boolean input...but I'll also include a friendly name.

As an entry in config/input_boolean.yaml

When I call this object in the future, it'll be "input_boolean.octoprint_mini_connect". 

If you're not familiar with checking your configuration file after changing it, go to Configuration (side panel) and scroll down to "Server Controls". Select that option, and press the big blue button that says "CHECK CONFIGURATION". It's a safety check to make sure you didn't create problems when HA restarts. (As a handy tip, if you get an error for a specific line...like including your new input_boolean.yaml - it doesn't always mean that THAT line is the problem, but that the file it refers to, can have problems.)

Go ahead and restart HA (File editor>Gear (in upper right)>Restart HASS). This will get your configuration and input_boolean file active.

Step 2 - Create a Button in Lovelace GUI

Next, we'll wire up a button that connects to that boolean input. Go to Overview in the left panel, open the Lovelace UI Panel in the upper right (three vertical dots), then Edit Dashboard. Add a button like this:


Step 3 - Wire the Button in Node-Red

This flow will consist of 3 parts:


[Events: State] - monitoring the boolean input we just made


[Template] - the message we will send to Octoprint (the REST parameters)

[MQTT Out] - this will package the message into a topic, and pass it off to HA's broker


Finally, click on "Deploy" in the upper right corner. This will save the flow, and your button should now work.  If you can have both your HA Overview (to see your button) and Octoprint open, you should see this message when you click:


And...hopefully it also clicks the "Connect" within Octoprint.




Comments

Popular posts from this blog

Setting up MQTT on HA and Octoprint

Home Assistant Controlling Octoprint through MQTT - Chapter 2