OctoPrint - Faster Events from the Horses Mouth Using MQTT

Issue

I had the idea to generate notifications on OctoPrint events, and I wanted to accomplish this with NodeRed and Pushbullet.

However, I wasn’t too satisfied with how long the gap was between the OctoPrint UI relaying a change and the change being detected over on HA (and downstream to Node Red).

I was counting a difference of about 30 seconds between seeing the UI change status and seeing Lovelace (and HA sensor) changing state.

TL:DR; Event messages off OctoPrint could be faster


Solution - OctoPrint Events

Looking to see if someone else had created event driven notifications for OctoPrint I came across the source documentation that stated there is built in event messaging.


Assumptions

  • Have OctoPrint on a Raspberry Pi (Linux)
    • You don’t need to run OctoPi, so long as you have the OctoPrint running somewhere
  • Already have an MQTT broker set up on your network
    • If you do not, you can run any other terminal command, such as Pushbullet API, Growl, Slack, or whatever you like

Setting Up Events

OctoPrint Config

Simply open up the OctoPrint config.yaml file.

On OctoPi it’s located at /home/pi/.octoprint/config.yaml (user may be different if you installed OctoPrint manually.

Paste this at the end of the file:

events:
    enabled: true
    subscriptions:
    -   command: mosquitto_pub -m "{__eventname}"
        event:
        - Startup
        - Shutdown
        - Connected
        - Connecting
        - Disconnecting
        - Upload
        - Disconnected
        - PrintDone
        - PrintStarted
        - PrintFailed
        - PrintDone
        - PrintCancelled
        - FileAdded
        - FileRemoved
        - PrintCancelling
        - PrintPaused
        - PrintResumed
        type: system

You can change - command: to be any terminal command. On the Octoprint documentation they suggest Growl with:

- command: python ~/growl.py -t mygrowlserver -d "Lost connection to printer" -a OctoPrint -i http://raspi/Octoprint_logo.png

Note: you can feel free to delete any rows of information you don’t need.
Or add any events if you look at the source document.

You can also do cool things like:

  - event:
    - PrintStarted
    command: python ~/growl.py -t mygrowlserver -d "Event {__eventname} ({name})" -a OctoPrint -i http://raspi/Octoprint_logo.png
    type: system

Which will print the name of the event and the name of the file to print.

MQTT

If you decide to use MQTT like I have, here’s a tip, you can create a file called /home/pi/.config/mosquitto_pub for your standard flags:

-h 192.168.0.2
-t home/office/printer/status
-p 1883

That’s it. Very quick and easy way to gather events directly from Octoprint to where ever you would rather they be!