Setting up MQTT on HA and Octoprint

 

I'm going to start listing 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 - this is the MQTT Broker)
  • Octoprint 1.5.2
  • MQTT 0.8.7 (Octoprint plugin)
  • MQTT-Subscribe 0.1.6 (Octoprintg 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'm running Home Assistant on a Raspberry Pi 3+. The Mosquitto and Node Red are installed as plugins to Home Assistant.

MQTT will be the method of communication between HA and Octoprint. MQTT has a publish-subscribe mode, that's used in the internet-of-things space. The Broker is the "hub" of the wheel, coordinating information. The clients are the "spokes" of the wheel. They can be things like sensors telling the broker "the door is open" or a light receiving a command to turn on. (Or even a sensor telling the broker "it's dark", and then the broker having a backend turning that "it's dark" message into a message to a light to turn on.)

In this case, I'll use Node Red to create a message & send it to the MQTT Broker on a specific topic subscription. Meanwhile, Octoprint will have MQTT-Subscribe set up, so that when its client hears something on that topic, Octoprint knows how to handle it.

Step 1 - Set up the Broker

You can just use the defaults. Keep in mind, because the default is "anonymous: false" it means that our client will need to have a login id to do more powerful things to interact with the broker in HA. 

I also need to set up the mqtt broker in the base config/configuration.yaml file by adding these lines:

mqtt:
    broker: localhost
    discovery: true
    discovery_prefix: homeassistant

Step 2 - Install MQTT on Octoprint

As my broker is installed as part of Home Assistant, it'll use the same IP address. I also clicked the check box for "The broker requires username and password to connect, show options" - this allows me to put in a username/password that exists in Home Assistant as a user, or one that was created in the MQTT broker configuration file in HA.

You can change topics, status, and last will - but that's not necessary for now. Topics are how your printer will send information to the broker

Step 3 - Install MQTT-Subscribe on Octoprint

This is how Octoprint's MQTT client will receive data from the broker. You'll set up subscriptions to topics, then funnel commands received from those topics into the Octoprint REST API. 

We'll start with the basic "connect".

Topic: octoprint_mini/connection

REST API: /api/connection

REST Parameters: { "command": "connect" }

After adding the topic, REST API, and parameters

The Wrap-up

That's all you need for MQTT. So far, it's all completely invisible though. Not useful in knowing IF it works, or what commands are failing. I installed MQTT Explorer on my Ubuntu machine, and set it up to listen to HA's MQTT broker. With that on, you can see basic messages from HA's broker. If you're running your printer (and have topics turned on), you should also see MQTT messages from Octoprint.

You could even send test messages from MQTT Explorer to your Octoprint, through the broker...or wait to explore the next part. Trying this now might give you a quick troubleshooting step - you only need to enter the topic and the json (aka REST Parameters).

My next topic will be creating a boolean input, making a button on a lovelace dashboard that clicks that boolean input on & off, and then wiring a Node Red flow to take that on/off to generate a message & send it to the MQTT broker.

Bonus Information

My basic Octoprint sensor information in config.yaml:

octoprint:
    - host: !secret octo_micro_IP
      api_key: !secret octo_micro_API
      name: Octoprint_Micro
      bed: true
      number_of_tools: 1
      sensors:
        monitored_conditions:
            - 'Current State'
            - 'Job Percentage'
            - 'Time Remaining'
            - 'Time Elapsed'
            - 'Temperatures'

    - host: !secret octo_mini_IP
      api_key: !secret octo_mini_API
      name: Octoprint_Mini
      bed: true
      number_of_tools: 1
      sensors:
        monitored_conditions:
            - 'Current State'
            - 'Job Percentage'
            - 'Time Remaining'
            - 'Time Elapsed'
            - 'Temperatures'

The "!secret" part, is just a secrets.yaml file with the Octoprint host IP and the Octoprint API key for that host...basically looks like this:

octo_micro_IP: xxx.xxx.xxx.xxx

octo_micro_API: ABCDEFGHIJKLM

https://www.home-assistant.io/docs/configuration/secrets/

Comments

Popular posts from this blog

Home Assistant Controlling Octoprint through MQTT - Chapter 1

Home Assistant Controlling Octoprint through MQTT - Chapter 2