Struggling to write HA rest-command for OctoPrint

I’ve tried googling and searching around and I can’t find an answer to my query.

I’ve got a couple of rest commands setup (few webhooks, and WLED payload one following a tutorial).

I’ve stumbled on the fact that Octoprint has a API list and I’d love to update my existing automation to:

  1. Turn on 3D printer plug
  2. Instruct Octoprint to connect to said 3D printer.

Point 2 is not possible via the entities and Octoprint integration. Looking at the Ocotprint documentation there is a rest command to do point 2 and they provide an example which I list below. I know where I need to tailor to my setup (IP and API key).

The bit I’m struggling with is how do I rewite / structure the Octoprint example to go into the HA config.yml?

The Octoprint API example command is:

POST /api/connection HTTP/1.1
Host: example.com
Content-Type: application/json
X-Api-Key: abcdef…

{
“command”: “connect”,
“port”: “/dev/ttyACM0”,
“baudrate”: 115200,
“printerProfile”: “my_printer_profile”,
“save”: true,
“autoconnect”: true
}

Source Octoprint API docs is: Connection handling — OctoPrint master documentation

You want a restful command

Hi, yeah I know I need the restful command :slight_smile: . Its how to re-write the example to work with restful structure.

I’ve got as far as in my config.yml - but no idea if I’m on the right track or not.

url: http://##########/api/connection HTTP/1.1
method: POST
content_type: “application/json”
# X-Api-Key: “###########”
payload: ‘{“command”: “connect”,“port”: “/dev/ttyUSB0”,“baudrate”: 250000,“printerProfile”: “Ender 3 Max”,“save”: false,“autoconnect”: true}’

Well, just try it, check HA logs / check on octoprint what is happening and report back if there is an issue. There are plenty of examples.

Not sure what you were expecting from this post, really.

@koying

My HA restful entry is:

url: http://#########/api/connection
    method: POST
    headers:
      x-api-key: "###########"
      content_type: "application/json"
    payload: '[{"command": "connect"}]'

Error returned in HA is:

Status code 400. Payload: b’[{“command”: “connect”}]’

I can see entry in Octoprint matching error code 400. From what I can work out something is wrong around the payload set-up. Any suggestions?
I don’t know where this randon ‘b’ is coming in from as you can see its not in the rest command.

More investigation using hoppscotch I can get the api call working with the following curl (hashed IP and api key). How do I write this curl into a rest_command in yml?

curl --request POST \
  --url http://#####/api/connection \
  --header 'content-type: application/json' \
  --header 'x-api-key: #########' \
  --data '{
  "command": "connect",
  "port": "/dev/ttyUSB0",
  "baudrate": 250000,
  "save": false,
  "autoconnect": true
}'

Exactly the same as you did before, replacing the payload with the working one from your curl :roll_eyes:

It’s unclear what’s blocking you, here…

Is that written as 1 single line or a line for each part of the command in the yml?

If I write it out as 1 line in the payload I get the Payload error.

If I write it out one line for each part of the command e.g.

‘{
“command”: “connect”,
“port”: “/dev/ttyUSB0”,
“baudrate”: 250000,
“save”: false,
“autoconnect”: true
}’

It fails the syntax checking even if I indent it the the same place under payload

Easiest is single line

payload: '{"command":"connect","port":"/dev/ttyUSB0","baudrate":250000,"save":false,"autoconnect":true}'

Also, please use the “format text” button of the forum when you post any type of code.
Besides the fact that code is not aligned if you don’t (of extreme importance for YAML), you also get “typographical” double quotes rather than plain ASCII, which makes copy/paster a PITA.

Code_format

Thanks for the help but even when using one line which I’ve used before it will would fail. It feels like its 99% there but I’m not experienced enough to work it out.

Rethinking what I wanted to do, I tried using a HA shell-command in yml and send a curl command over and that works.