Octoprint RestFull switch

Hello everyone!

I wonder if someone could help me creating a rest switch for my Octoprint server to connect to the printer. I’ve found the documentation here: http://docs.octoprint.org/en/master/api/connection.html

Could not get it to work, here is my config:

- platform: rest
  name: "Conectar Octoprint"
  resource: 'http://192.168.1.69/api/connection'
  body_on: '{"command":"connect"}'
  body_off: '{"command":"disconnect"}'
  is_on_template: "{% if is_state('sensor.octoprint_current_state', 'unknown') %}off{% else %}on{% endif %}"
  headers:
    Content-Type: application/json
    X-Api-Key: !secret octoprint_api

I get this error on log:

No route to resource/endpoint: http://192.168.1.69/api/connection

I just want HA to issue a connect or disconnect command to my Octoprint server.

Thanks a lot!

3 Likes

Have you made an progress? Based on the error message it looks like the machine running HA simply can’t communicate with the server using that address. Have you tried using a command line / terminal from that machine and sending the POST command using curl?

1 Like

Hello @pnbruckner! Thanks for your time.

No, I did not made any progress.

It is strange because I have OctoPrint component configured and running:

I can access the server over SSH, works great.

I will have to take some time and learn CURL, beginner here :slight_smile:

Documentation is very clear and simple, I really tough I could do it without much knowledge…

Thanks a lot for your help!

I think this is the curl command you might want to try:

curl -v http://192.168.1.69/api/connection -d '{"command":"connect"}' -H 'Content-Type: application/json' -H 'X-Api-Key: !secret octoprint_api'

The -v is for verbose output which should tell you some details about the lower level connection process. Obviously adjust for the real details (like key, etc.)

1 Like

Amazing! Will try using command line Switch with those settings, I will let you know the results. Thanks again!

If you can you might want to try it from the command line (outside of HA, but on the same machine that HA is running on) first. Assuming you do try it within HA, then you probably want to change the -v option to -s.

Great, thanks!

This config is not working, I get and error checking:

- platform: command_line
  switches:
    conectar_octoprint:
      friendly_name: "Conectar Octoprint"
      command_on: 'curl -v http://192.168.1.69/api/connection -d '{"command":"connect"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'
      command_off: 'curl -v http://192.168.1.69/api/connection -d '{"command":"disconnect"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'
      value_template: "{% if is_state('sensor.octoprint_current_state', 'unknown') %}off{% else %}on{% endif %}"

I think you have the quoting wrong. Try:

- platform: command_line
  switches:
    conectar_octoprint:
      friendly_name: "Conectar Octoprint"
      command_on: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"
      command_off: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"disconnect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"
      value_template: "{{ not is_state('sensor.octoprint_current_state', 'unknown') }}"

I also think this simpler value_template will work.

1 Like

Thanks again! Just waiting for the print to be done. I’ll share my results. Thanks!

Hello my friend!

The commands works like a charm, thanks for you time! Here is the final code:

- platform: command_line
  switches:
    conectar_octoprint:
      friendly_name: "Conectar Octoprint"
      command_on: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F30FDB884F528964F137A56544FD'"
      command_off: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"disconnect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F30FDB884F528964F137A56544FD'"
      value_template: "{% if is_state('sensor.octoprint_current_state', 'unknown') %}false{% else %}true{% endif %}"

The only problem now is that the value_template is not working for me. I had to change it back to this because there are more the one on state (Operational and Printing). Tested on the HA tool for templates and works great but no update is shown in UI when manually disconnects to the OctoPrint server… If server is connected the switch turns on but nothing happens when server disconnects.

Maybe I’ll have to create a couple of scripts for the commands and make a template switch with this template to work.

Thanks again for your help!

I suspect that this is because you didn’t specify the command_state configuration variable. Without that, I don’t think that value_template is used.

I was thinking you might have to do that. Or maybe you can find a way to use the command line sensor’s command_state & value_template variables to make it work.

1 Like

Hello again!

So yes, creating a couple of shell commands and a template switch solved my problem.

Here is the final code:

Shell commands:

octoprint_connect: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"
octoprint_disconnect: "curl -s http://192.168.1.69/api/connection -d '{\"command\":\"disconnect\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"

Switch:

- platform: template
  switches:
    conectar_octoprint:
      friendly_name: "Conectar Octoprint"
      value_template: "{{ not is_state('sensor.octoprint_current_state', 'unknown') }}"
      turn_on:
        service: shell_command.octoprint_connect
      turn_off:
        service: shell_command.octoprint_disconnect

Automation (some ideas I had):

- alias: Auto-connect to printer
  trigger:
    platform: state
    entity_id: switch.ender3
    to: 'on'
  action:
    - delay: 00:00:05
    - service: switch.turn_on
      entity_id: switch.conectar_octoprint

- alias: High temperatures - shut down (Sonoff relay)
  trigger:
    platform: template
    value_template: "{% if (states.sensor.octoprint_actual_bed_temp.state | int >= states.sensor.octoprint_target_bed_temp.state | int * 1.2 or states.sensor.octoprint_actual_tool0_temp.state | int >= states.sensor.octoprint_target_tool0_temp.state | int * 1.2) and is_state('sensor.octoprint_current_state', 'Printing') %}true{% else %}false{% endif %}"
  action:
    - service: notify.ios_iphone_de_antonio
      data_template:
        title: "Impressora"
        message: "ATENÇÃO! Temperaturas passaram dos limites, impressora foi desligada da energia!"
    - service: persistent_notification.create
      data:
        title: "Impressora ({{now().strftime('%H:%M')}})"
        message: "ATENÇÃO! Temperaturas passaram dos limites, impressora foi desligada da energia!"
    - service: switch.turn_off
      entity_id:
        - switch.ender3

Thanks @pnbruckner for your help!

1 Like

Hello @pnbruckner! How are you today?

Sorry to bother you again but I was not able to get my next step on Octoprint integrations: preheat the bed or/and tool shell commands.

I was reading the documentation about the tool and bed commands and came up with this, but it not worked:

octoprint_preheat_bed: "curl -s http://192.168.1.69/api/printer/bed -d '{\"command\":\"target\",\"target\":\"60\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"
octoprint_preheat_tool: "curl -s http://192.168.1.69/api/printer/tool -d '{\"command\":\"target\",\"targets\":{\"tool0\":\"100\"}}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"

Any idea how to fix this? These commands does not do anything, I believe there is something wrong with the JSON format.

Thanks!

Have you tried running the commands in a shell? Adding curl’s -v option can help give some insight.

1 Like

Hello! A friend of mine helped me and now it works like a charm. Here is the result:

octoprint_preheat_bed: "curl -s http://192.168.1.69/api/printer/bed -d '{\"command\":\"target\",\"target\":60}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"
octoprint_preheat_tool: "curl -s http://192.168.1.69/api/printer/tool -d '{\"command\":\"target\",\"targets\":{\"tool0\":100}}' -H 'Content-Type: application/json' -H 'X-Api-Key: 2BF5F70FDB884F528464F137A26544FD'"

Thanks!

Ah, the numbers have to be numbers, not strings. Makes sense.

1 Like

Here is the HA panel:

I believe this thread could help many people trying to integrate Octoprint with HA.

Thanks.

1 Like

How did you get the relay control from octoprint to show in home assistant? I am using the enclosure plugin, and octoprint is configured in HA, everything shows including the temps and status but not the relays

Hello sir.

I believe this question was for me, right?

I am using a sonoff basic as the main power switch of my Ender 3, I don’t have anything else connected to my RPIZW, other than the PiCam…