Octoprint RestFull switch

I have tried numerous variations of this and still can’t seem to get it to work.
No matter what I do, the sensor still has “unknown” as it’s state. I checked the json output from the api and it contains the correct fields. Below is my config file code.

- platform: rest
  resource: http://10.0.0.59:5000/api/job
  name: "STL_file"
  value_template: '{{ value_json.job.file.name }}'

json output:

{
  "job": {
    "averagePrintTime": 4556.359313346, 
    "estimatedPrintTime": 3773.5218414710844, 
    "filament": {
      "tool0": {
        "length": 3118.2641500000045, 
        "volume": 0.0
      }
    }, 
    "file": {
      "date": 1573708087, 
      "display": "Filamentsensor.gcode", 
      "name": "Filamentsensor.gcode", 
      "origin": "local", 
      "path": "Filamentsensor.gcode", 
      "size": 2120744
    }, 
    "lastPrintTime": 4556.359313346, 
    "user": "makeit"
  }, 
  "progress": {
    "completion": null, 
    "filepos": null, 
    "printTime": null, 
    "printTimeLeft": null, 
    "printTimeLeftOrigin": null
  }, 
  "state": "Operational"
}

And this?

value_template: ‘{{ value_json.file.name }}’

Your json is Code is differently to my json

I tried that also. Same results.
I copied the JSON data into the template developer and tried the value_template and it worked correctly.
Just not making sense. It seems like the JSON data is not being captured by HA… still chasing it. I’ll see what I can figure out.

hmmm very strange…

what does your JSON output look like?

Your JSON looks basically the same as mine. I just can’t seem to make the “REST” sensor to work. I seems like HA is not reading the data…
I’ve tried about everything I can think of. I can’t tell if HA is even receiving any data back.
Guess I need to get away for a while and then maybe a fresh look at it will point something out.

After much aggrevation, I finally gave up on the REST sensor and switched to a Command Line sensor with:

- platform: command_line
    command: "curl http://10.0.0.59:5000/api/job -H 'X-Api-Key: <your octoprint api>'"
    name: "Octo Job File Name"
    value_template: '{{ value_json.job.file.name }}'

That seems to work just fine. Never could get the REST sensor to work. It’s probably something to do with the API key, but I didn’t get it figured out.

Thanks for this. Working well. Only change I made was to the value template for the octoprint connection switch. When printing the ready value is set to false which turns the switch off in the frontend.

- platform: command_line
  scan_interval: 5
  switches:
    connection_octoprint:
      friendly_name: "Octoprint Connection"
      command_on: "curl http://192.168.1.102/api/connection -d '{\"command\":\"connect\"}' -H 'Content-Type: application/json'"
      command_off: "curl http://192.168.1.102/api/connection -d '{\"command\":\"disconnect\"}' -H 'Content-Type: application/json'"
      command_state: "curl http://192.168.1.102/api/printer"
#      value_template: "{{ '\"ready\": true' in value }}"
      value_template: "{{ not is_state('sensor.aneta8_current_state', 'unknown') }}"

1 Like

Will this work if you are running Home Assistant in Docker?

I have Home Assistent and OctoPrint in Docker!

Thanks for sharing your setup! I have a question:

I noticed you have a few filament sensors and a filament switch in your lovelace configuration and I also see a reference to “sensor.filament_menge_api” in your config, but I can’t figure out where that’s coming from. Am I missing something?

Edit: found the source of the sensors :slight_smile: It was right where I was looking at :wink: Still curious about the filament switch though!

the filament sensor is self-made.
it is an esp8266-01 with a micro switch on it.
Firmware is installed tasmota.

automation:

- id: 3D - Drucker Pause Filamentende
  alias: 3D Drucker Pause Filamentende
  initial_state: 'on'
  hide_entity: false
  trigger:
    platform: state
    entity_id: switch.filament
    to: 'off'
  condition:
    condition: numeric_state
    entity_id: sensor.octoprint_creality_3d_ender_3_job_percentage
    above: 1
  action:
    - service: switch.turn_on
      entity_id: switch.pause_octoprint
    - delay: 00:00:10
    - service: telegram_bot.send_message
      data:     
        message: "Filament Rolle scheint am ENDE zu sein, oder ist abgerissen! Bitte überprüfen!!! \n
                 3D Drucker ist in ---> {{states.sensor.octoprint_creality_3d_ender_3_current_state.state}}"
    - service: camera.snapshot 
      data:
       entity_id: camera.3d_drucker
       filename: '/Kamera/snapshot1.jpg'
    - delay: 00:00:05
    - service: telegram_bot.send_photo
      data:
        file: '/Kamera/snapshot1.jpg'
        caption: Snapshot vom 3D Drucker
1 Like

I’m still trying to get commands setup in HA for Octoprint. I am running HA in Docker. If I open an SSH terminal on the host system Docker is running on and run this command:

curl -v http://x.x.x.x/api/connection -d '{"command":"connect"}' -H 'Content-Type: application/json' -H 'X-Api-Key: <key>'

I can see Octprint connect to my printer. I can also run the disconnect verion for this. However if I run the follwing command to set the nozzle temp:

curl -s http://x.x.x.x/api/printer/tool -d '{\"command\":\"target\",\"targets\":{\"tool0\":215}}' -H 'Content-Type: application/json' -H 'X-Api-Key: <key>'

I get an error like this:

> <!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>

I fixed my issue, I went back and got Rest Commands working. Now, however I can’t get my sensors that display the STL File name, size, and filament length to work. They all say “unknown”. I feel if I get one figured out, the other two will work also. Here is what I’ve tried:

- platform: command_line
  command: "curl http://10.0.0.33:5000/api/job -H 'X-Api-Key: <octoprint_api>'"
  name: "STL Name"
  value_template: '{{ value_json.job.file.name }}'

I can see the correct JSON in a browser, for some reason, the sensor just isn’t pulling the data. Someone else had that issue with a Rest Sensor and got this command line working, however I’m not having luck with either.

Thanks for sharing. Managed to get both the command line and rest to work. Here is the code:

sensor:
  - platform: rest
    resource: 'http://X.X.X.X/api/job'
    name: "STL file"
    value_template: '{{ value_json.job.file.name }}'
    headers:
      X-Api-Key: XXX
  - platform: rest
    resource: 'http://X.X.X.X/api/job'
    name: "STL Datei"
    value_template: '{{value_json.job.file.display}}'
    headers:
      X-Api-Key: XXX
  - platform: command_line
    command: "curl http://X.X.X.X/api/job -H 'X-Api-Key: XXX'"
    name: "Octo Job File Name"
    value_template: '{{ value_json.job.file.name }}'

Trust this will help someone eventually…

1 Like

Here is my octoprint page as I finally upgraded to Lovelace:


As with everything else, it’s a work in progress. Things I want to add/consider include:

  1. Palette integration (splices and pings)
  2. Printer Speed sensor (template based on assumed 100% at start).
  3. Fan speed
  4. +/- 2C buttons for nozzle and bed
  5. Extruder Load / Unload filament. Extrude / Retract 5mm
    This will do for now as I need to make cards for other devices…
2 Likes

have now added a gcode preview of octoprint in an iframe, and power consumption

4 Likes

Woohoo, gcode preview! Me like… What service are you using for that? Not from Octoprint, right?

This is a octoprint plugin – pretty Gcode, and i use the FullScreen Link from this plugin

1 Like