Octoprint RestFull switch

thanks for the answer, as far as the scrollbar is concerned, I think it’s because of the iframe in homeassistant.
maybe it works with custom component card-mod ???

Yup, that could be a way to try this. I think it actually comes from the octoprint page. There are multiple layers and a lot of what is hidden is wider and higher than the prettygcode image…

I have the same problem with this error 400. How did you solve this problem ?

I ended up using rest commands I believe. I honestly quit using any of the Hass controls for Octoprint until a way is found to populate a list where you can select the .gcode files that are uploaded to it. Otherwise you still have to go to the Octopi interface to actually print them. To me that kinda defeated the purpose of bringing everything into Hass.

is there a way to turn of the log from octoprint.
I always turn of the pi running octoprint and then my logs are full of connection errors

@Johann_Edelmuller did you upgrade Octoprint recently? My prettygcode view no more works since. I get a ‘refused to connect’ error. I think it’s because of credentials no more being passed on to Octoprint. Including these in the url: ‘http://user:[email protected]’ does not work (no error message, but no load). Any tip is welcome!

Hallo @monkey-house,

i must set this option in octoprint website.

apparently this option was added to an update or whatever :wink:

Wow what a silly option! Thanks for that… Jetzt muss ich wachten 20 Stunden bis dem Druck fertig zu sein!

@Johann_Edelmuller: Not having much luck with this. Here is what is displayed in Hass. Tried with and without CSS, with a different browser, etc. I tried using either the full screen or regular link. Not sure whether this is an issue with the Hass browser, Google Chrome or Octoprint… Here is a screenshot. Can you confirm yours still displays correctly?

I managed to make a restful switch for the connect / disconnect api request in octoprint.

switch:
  - platform: rest
    name: "Brytare - Connect Ender 3 V2"
    resource: http://192.168.1.27/api/connection
    scan_interval: 5
    body_on: '{"command": "connect"}'
    body_off: '{"command": "disconnect"}'
    is_on_template: '{{ value_json.current.state == "Operational" }}'
    headers:
      Content-Type: application/json
      X-Api-Key: your_api_key

It works great - but there´s an error in the logfile:

2020-08-31 10:22:51 ERROR (MainThread) [homeassistant.components.rest.switch] Can’t turn on http://192.168.1.27/api/connection. Is resource/endpoint offline?

One strange thing is that I could NOT get the API request to work by using curl - very odd.

Hi, may I know how do you do this import the control panel and other data like filament usage and cost from octoprint? Do you mind to share me your relevant part of configuration.yaml in HA. Thank you in advance.

Hi, can someone give me a hint, why i’m getting a “bad request”, when trying to curl my octoprint?

 curl http://192.168.1.3:4000/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json'  -H 'X-Api-Key: CC9Cxxxxxxxxxxx1A1'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

Hey guys,

A little advise? If I want to put this in a script to expose to Alexa. How can I do that

shell_command:
  octoprint_pause_resume: "curl -s http://192.168.1.47/api/job -d '{\"command\":\"pause\",\"action\":{\"toggle\"}}' -H 'Content-Type: application/json' -H 'X-Api-Key: FE8Dxxxxx4DC9A8712773AC817A5D'"
  octoprint_autohome: "curl -s http://x.x.x.x/api/printer/command -d '{\"command\":\"G28 W\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: XXX'"

Do I put the above (with correct info) in the config.yaml and then call service “octoprint_pause_resume” as the entity?

Or??.. My first time dealing with calling curl commands from HA

Thanks :slight_smile:

EDIT, think I found it actually

Hi guys. Thanks for this helpful thread.

I could use some help though.
Does anyone know how to parse a number from an input_number entity into the curl commands?
I would like to be able to manually input the temperature target for the bed or nozzle.

EDIT: Never mind. I figured it out. Here’s an example of my shell_command for those who are interested:

shell_command:
set_bed_temp: “curl -s ‘http://192.168.xxx.xx/api/printer/bed’ -d ‘{\"command":\"target",\“target\”:{{states(\“input_number.3d_bed_temp\”)|int}}}’ -H ‘Content-Type: application/json’ -H ‘X-Api-Key: xx’”

2 Likes

Just wanted to thank you for this topic. Works perfectly with my Ender 5!

Hey guys,

So ive moved most of my stuff over to Node Red. Worked out how to send the shutdown but need to insert the JSON for the pause/resume and home

Anyone done that?

shell_command:
  octoprint_pause_resume: "curl -s http://192.168.1.47/api/job -d '{\"command\":\"pause\",\"action\":{\"toggle\"}}' -H 'Content-Type: application/json' -H 'X-Api-Key: FE8Dxxxxx4DC9A8712773AC817A5D'"
  octoprint_autohome: "curl -s http://x.x.x.x/api/printer/command -d '{\"command\":\"G28 W\"}' -H 'Content-Type: application/json' -H 'X-Api-Key: XXX'"
1 Like

I know this is an old comment, but is the gcode viewer still working for you? If so, could you post your yaml/setup for it?

I have the Pretty GCode plugin installed on Octoprint and it shows correctly there. Full screen URL does too, but when I visit from incognito it brings up the sign-in page. I’m using MQTT Octoprint - I’ve been able to pull in everything else (axis controls, power controls, stl viewer, etc) into HA, so this is the last thing I’m trying to get working.

I’ve added the IP to a webpage card but it shows up blank.

type: iframe
url: http://192.168.68.145/?fullscreen=1#tab_plugin_prettygcode
aspect_ratio: 100%

Have you enabled this switch in the settings:

EDIT: I’ve tried this and get the log in page but does not log in

I did try that, I haven’t rebooted Octoprint since I’m in the middle of a print, so I’m guessing that’s why I can’t see the sign-in page. I’m also going to try installing this to see if it auto logs me in once I can see it in HA: OctoPrint-AutoLoginConfig

1 Like

No that doesn’t work either. I did a search and its a known issue, something to do with samesite or reverse proxy.

While this is an old thread, having taken a lot of help from this thread (thanks all), I wanted to share my variation of configuration.yml setup in case its of help as I’ve found using the restful integration to be a cleaner experience than curl via shell_command, allowing for easy passing of the api key via secrets and having all my logic in one place.

rest_command:
  octopi_disconnect:
    url: "http://octopi.local/api/connection"
    method: post
    headers: 
        X-Api-Key: !secret octopi_key
    content_type: "application/json"
    payload: "{ \"command\": \"disconnect\" }"
    
  octopi_connect:
    url: "http://octopi.local/api/connection"
    method: post
    headers: 
        X-Api-Key: !secret octopi_key
    content_type: "application/json"
    payload: "{ \"command\": \"connect\" }"  
    
  octopi_restart_klipper:
    url: "http://octopi.local/api/system/commands/custom/octoklipper_restart"
    method: post
    headers: 
        X-Api-Key: !secret octopi_key
    content_type: "application/json"
    

secrets.yaml

octopi_key: "secret api key"

I’ve been able to use above to automate the restarting of klipper after a printer start