Control OctoPrint in HomeAssistant

Hi,
what is the configuration (configuration.yaml) to control OctoPrint (not a Docker-Version!!!) in Home Assistant (also not a Docker-Version).
Both is installed on Raspberry Pi 4.

The integration working fine:
octoprint:
host: 192.168.178.102
api_key: my API
bed: true
number_of_tools: 1

Is it possible to rotate the PiCam?
configuration.yaml:

Raspberry Cam

Thanks a lot!

1 Like

Hi
Instead of the OctoPrint Integration I’m using MQTT.
There are 2 Plugins for Octoprint.

The MQTT Plugin is used to connect to your Broker.
The HomeAssistant Discovery Plugin is used to publish all the topics that are automatically discovered by Home Assistant.
The Plugin has a very good description.

For the Camera I us a shell command that connects to my OctoPrint using ssh and runs the command to either start or Stop the camera.
The Status is checked using curl.

I also have switch to connect or disconnect the printer using the OctoPrint API.

If you’re interested I can send you the configs later.

1 Like

OK… so with MQTT it’s possible to control the printer (pause, home, temperature, …)?
I’m interested in you config! :slight_smile: Thanks!!!

It is a mix with MQTT, API Calls and Shell commands :slightly_smiling_face:
In Home Assistant I’m using the Mosquitto MQTT Broker Add-on but this will work with any other MQTT broker you might have in use in your environment.

In OctoPrint I installed this two plugins:

Once installed you “just” need to configure the MQTT plugin so it connects to your broker.
Here’s a screenshot of my configuration:

The tabs “Topics” and “Status & Last Will” are unchanged.
The Plugin “HomeAssistant Discovery” does not need any configuration. It just uses the previously configured MQTT Plugin.

The MQTT Autodiscovery in Home Assistant will then show you a bunch of new Entities:

As you might see in the screenshot above there are already a lot of switches and sensors created automatically and you can start using them in your dashboard.

I created the file “camera.yaml” within my /config directory and added the following code in it:

  - platform: mjpeg
    name: OctoPrint Kamera
    still_image_url: http://add-your-octoprint-ip-here:8080/?action=snapshot
    mjpeg_url: http://add-your-octoprint-ip-here:8080/?action=stream

And to load the new entitie I also had to add this line to my configuration.yaml file:

camera: !include camera.yaml

I did this because of the possiblity that I might add other cameras in my environment in the future and I try to keep my configuration.yaml file as clean as possible.

To activate the new configuration you have to restart Home Assistant as usual.
I always hit the “Check Configuration” button fist in the Server Management before restarting, just to be shure there’s no typo in any of my changes.

So now I wanted to be able to start and stop the camera service on the Pi. The only way I could find to do this is running a shell command.
The command will connect to the pi where OctoPrint is installed and executes the start or stop command for the camera service.

The first step to make this work is to create a RSA key pair.

  1. Login to your Home Assistant using SSH
  2. Run the command “ssh-keygen -t rsa”
  3. Copy the ID to the authorized_keys file on your OctoPrint Server using
    the command “ssh-copy-id USERNAME@IP-ADDRESS”

Still logged in to the SSH Terminal you can run the Command “ls .ssh”. You shloud see a list of files then. The File “id_rsa” is the one we need to copy to our /config directory now.
Run the Command “cp .ssh/id_rsa /config/.ssh/”.

This is necessairy becaus the frontend needs to have access to this file and therefore it neest to be placed within the /config directory.
It took me some time to figure this out :slightly_smiling_face:

To check the status of the Camera I created a file called octocam_status.sh within the /config directory and added the following lines:

#!/bin/bash
echo $(curl -s -o /dev/null  -w "%{http_code}\n" http://your-octoprint-ip)

I tested with adding the command directly to the configuration below but I sometimes got some error messages in the log becaus when the camera is turned off, the curl comamnd fails.

I created a new file called switches.yaml and pastet the following lines in it:

- platform: command_line
  switches:
    octoprint_camera:
      command_on: "/usr/bin/ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no pi@your-octoprint-ip '/home/pi/scripts/webcam start'; sleep 5"
      command_off: "/usr/bin/ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no pi@your-octoprint-ip '/home/pi/scripts/webcam stop'"
      command_state: "/config/octocam_status.sh"
      value_template: '{{ value == "200" }}'
      friendly_name: OctoPrint Kamera

In my configuration.yaml I had to add this line:

switch: !include switches.yaml

Because Home Assistant will add warnings to the log when the camera is offline I changed the log-level for this entity type to critical by adding this lines to my configuration.yaml file:

logger:
  default: warning
  logs:
    homeassistant.components.mjpeg.camera: critical

Checked the config in the Server Management and restartet Home Assistant to get the new config loaded.

Now I have a switch to start and stop the camera service on the OctoPrint Server.
The delay of 5 seconds at the end of the command_on line is needed because the service needs some time to start properly.

So far so good.

Now I wanted to be able to connect and disconnect the printer as if I would press the Connect Button on the OctoPrint web-interface.
I have found the solution in this post:

So I created the file shell_commands.yaml and added the follwoing 2 lines to it:

octoprint_connect: "curl -s http://our-octoprint-ip/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: YOUR-API-KEY'"
octoprint_disconnect: "curl -s http://our-octoprint-ip/api/connection -d '{\"command\":\"disconnect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: YOUR-API-KEY'"

Then I edited my switches.yaml file and added this lines:

- platform: template
  switches:
    connect_octoprint:
      friendly_name: "Drucker verbinden"
      value_template: "{{is_state('binary_sensor.sidewinder_x1_connected', 'on') }}"
      turn_on:
        service: shell_command.octoprint_connect
      turn_off:
        service: shell_command.octoprint_disconnect

After checking the Configuration and restarting Home Assistant we have a switch to connect and disconnect the Printer from OctoPrint.

I could not yet figure out how to set a temperature or send the printer to the home position yet.
But it should be possible using MQTT and/or an API call.

The API Documentation from OctoPrint is very helpful but at the moment I don’t have the time to tinker around with it.
https://docs.octoprint.org/en/master/api/index.html

For my Printer Dashboard I was using a lot of the work from the guy who created the OctoPrint-HomeAssistant Plugin, this saved me a lot of time creating a nice looking dashboard.

Recently I have found the custom card from this guy and because I really liked it I just added it to my HACS Repository list and insatlled the card.

My Printer Dashboard looks like this at the moment:

That was a lot of text :slight_smile: but I hope you can use some of it for your configuration.

9 Likes

great job guy :slight_smile: that’s amazing! Thanks a lot for the description!!!
I use mqtt already for more than a year and I like it. It’s better than manuelly integration.
I see all the OctoPrint-Information already in HA. My piCam is working too.

I will try some other configs in your post!! - command_line, warnings, curl, …

Thank you so much!
Vielen Dank :wink:

You’re welcome
Writing this all down was a good recap for me aas well.

Have a nice day,
Ich wünsch dir nen schönen Tag :sunny::wink:

Maybe you already figured it out your self but ayway here is how to send the printer to the home position using MQTT:

All you have to donis create a button with this config:

type: button
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: octoPrint/hassControl/home
    payload: '["x", "y", "z"]'
  target: {}
icon: hass:home
name: Home

thanks a lot!

Here my code for all directions:
Grid Card Configuration:

type: grid
cards:
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/home
        payload: '["x", "y", "z"]'
      target: {}
    icon: hass:home-remove
    name: Ender 5 home
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"x": 10, "speed": 0.5}'
    icon: hass:arrow-left-bold-outline
    name: left
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"x": -10, "speed": 0.5}'
    icon: hass:arrow-right-bold-outline
    name: right
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"y": 10, "speed": 0.5}'
    icon: hass:arrow-down-bold-outline
    name: front
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"y": -10, "speed": 0.5}'
    icon: hass:arrow-up-bold-outline
    name: back
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"z": 10, "speed": 0.5}'
    icon: hass:arrow-down-bold-outline
    name: z-Down
    show_state: true
  - type: button
    tap_action:
      action: call-service
      service: mqtt.publish
      service_data:
        topic: octoPrint/hassControl/jog
        payload: '{"z": -10, "speed": 0.5}'
    icon: hass:arrow-up-bold-outline
    name: z-Up
    show_state: true
square: true
columns: 7
1 Like

3 Likes

I’m looking for the solution how to pause and stop the print.
In my own marlin code it works fine:
pause: the z axis goes down und x & y go to the home position.
resume: printing continues.
stop / abbort: the same as a pause-command but without the possibility to resume the job.

how to realize it? Is it necessary to configure something in OctoPrint or in HA only?

Hi Ryci,

Thanks for such a detailed write up on this…I’ve just used it to get my Octoprint into HA via MQTT.

I was wondering if you are using the Threedy card with the MQTT entities? When I try to set it up with the MQTT instance of Octoprint I get errors in the card.

However I have just set up the Octoprint integration in HA and the card works.

Just curious if you were able to use MQTT or if you’re using the HA integration for the Threedy card.

Thanks!

Hi Marc
Yes I’m using the Threedy card, you have to switch to the yaml Editor and add the Line “use_mqtt: true”
Then it will work with MQTT :slight_smile:

1 Like

Hi Dshaesp
Sorry for my very late reply, I’ve had some trouble with my printer and after that I went in holyday. So I just forgot to reply :frowning:

First of all; Your dashboard looks amazing! Thanks for sharing :slight_smile:

Have you found a solution for your question already?

Hi Ryci,

Thanks very much for that!..I missed that option in the documents.

I’ve just added it and now all’s good with MQTT!

Thanks again! :smiley:

Hey,
thanks! I like my dashboard, too ;-). I optimized it…

I couldn’t find the solution for the problems with pause / stop, …

I’ not sure if it is possible with octoprint, maybe the Octoprint community can help here. I just started with 3D Printing and Octoprint about 4 month ago :slight_smile:

In the Documentation I found this configuration example:
https://docs.octoprint.org/en/master/features/gcode_scripts.html#examples

Hey everyone. I’ve multiple printers each one running octoprint so I ask how to configure the mqtt for receiving and sending values and command separately on each octoprint. Have anyone done it yet? Thanks

Hi
I have only one printer so I could not test it.
In the MQTT Settings on each OctoPrint you can define a base topic. The default is “octoPrint/”.
If you change this to an individual topic for each printer you shoild be good.

I confirm, you need to change base topics as GitHub - cmroche/OctoPrint-HomeAssistant: Home Assistant plugin for OctoPrint, enabling MQTT discovery of you device and sensors clearly says (check multiple instances section). Important thing (at least for my case) is to shutdown and restart both octoprints. After that HA will discover devices and entities correctly.

Hi everyone, it’s not exactly this topic but many of you maybe have these hacs card. From Hassio version 11.0 thereby card has some problem with the definition of the card. If you turn on the monitored condition like ETA, elapsed, remaining the card gives an error. I found on the GitHub page of the component a workaround which define two sensors for the elasped and remaining time. I defined those sensor but I can’t understand how to recall them into the card. Did you make it? thanks