Octoprint -- Enclosure Plugin -- GPIO state in HA?

Hello.
I have Octprint installed on a Raspberry Pi. The integration into HA worked wonderfully and the different states are also displayed. Now I have installed the “Enclosure Plugin” in Octprint and I can handle it super well.
Now my question: Can I display the status of a GPIO pin of the Octopi in HA as a sensor?
I have seen that you can set up a function for IFTTT in the “Enclosure Plugin”, in “Advanced Settings”. But I’m not such a big friend of IFTTT and wanted to know if there is another, more direct way.

Have not found a thread here in the forum to my question, so this one here.

Thank you in advance.

greetings

I would be very interested in this as well. Bonus points if you could control the pins directly from HA.

“There is also an API that can be used to interact with the plugin with other software”

To expand a little further on this, I can get the following command to turn on the lights in my Printer Enclosure using the Enclosure plug-in API documentation.

https://192.xxx.x.xxx/plugin/enclosure/setIO?status=true&index_id=4&apikey=XXXXXXXXXXXXXXXXX

However, I’m not sure how to best integrate these with input booleans, switches, etc in Hassio.

I worked on this a bit and the following Node-Red flow is allowing me to control my Octoprint Enclosure GPIO from within HA and has a 15 second sync to HA if things are changed via Octoprint.

I am more than open to suggestions on how to do cleaner job on this if anyone has any pointers. Otherwise, I thought someone else might benefit from my effort.

[{"id":"b9cb942f.daadd8","type":"tab","label":"Clipboard","disabled":false,"info":""},{"id":"3ed38ed.97a43f2","type":"http request","z":"b9cb942f.daadd8","name":"Printer on","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.xxx/plugin/enclosure/setIO?status=true&index_id=3&apikey=XXXXXXXXXX","tls":"","proxy":"","authType":"","x":520,"y":60,"wires":[[]]},{"id":"6b450123.249df","type":"http request","z":"b9cb942f.daadd8","name":"Printer off","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/setIO?status=false&index_id=3&apikey=xxxxxxxxx","tls":"","proxy":"","authType":"","x":520,"y":100,"wires":[[]]},{"id":"3c2832d6.4691fe","type":"comment","z":"b9cb942f.daadd8","name":"3D Printer Enclosure Plugin","info":"","x":160,"y":42,"wires":[]},{"id":"f098717b.607a8","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":82,"wires":[["3ed38ed.97a43f2"],["6b450123.249df"]]},{"id":"921cf66.1815208","type":"change","z":"b9cb942f.daadd8","name":"change","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":82,"wires":[["f098717b.607a8"]]},{"id":"592024ca.bbee6c","type":"server-state-changed","z":"b9cb942f.daadd8","name":"Printer","server":"d9f08b8a.14825","version":1,"entityidfilter":"input_boolean.printer_power","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":90,"y":82,"wires":[["921cf66.1815208"]]},{"id":"2ae7b438.a6729c","type":"server-state-changed","z":"b9cb942f.daadd8","name":"Lights","server":"d9f08b8a.14825","version":1,"entityidfilter":"input_boolean.printer_lights","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":90,"y":162,"wires":[["f92aaa00.36dd1"]]},{"id":"f92aaa00.36dd1","type":"change","z":"b9cb942f.daadd8","name":"change","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":162,"wires":[["d9f7aa83.1c3d6"]]},{"id":"d9f7aa83.1c3d6","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":162,"wires":[["e14fd881.edd74"],["c943f381.a52728"]]},{"id":"e14fd881.edd74","type":"http request","z":"b9cb942f.daadd8","name":"Lights on","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.xxx.xxx/plugin/enclosure/setIO?status=true&index_id=4&apikey=xxxxxxxx","tls":"","proxy":"","authType":"","x":520,"y":140,"wires":[[]]},{"id":"c943f381.a52728","type":"http request","z":"b9cb942f.daadd8","name":"Lights off","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.xxx.xxx/plugin/enclosure/setIO?status=false&index_id=4&apikey=xxxxxx","tls":"","proxy":"","authType":"","x":520,"y":180,"wires":[[]]},{"id":"17130f0a.7c3029","type":"server-state-changed","z":"b9cb942f.daadd8","name":"Dryer","server":"d9f08b8a.14825","version":1,"entityidfilter":"input_boolean.printer_dryer","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":90,"y":242,"wires":[["e07e503e.a6edd8"]]},{"id":"e07e503e.a6edd8","type":"change","z":"b9cb942f.daadd8","name":"change","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":242,"wires":[["4ab0956.67b66ec"]]},{"id":"4ab0956.67b66ec","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":242,"wires":[["6f2fc6a.42960b8"],["4b89c7fa.f8554"]]},{"id":"6f2fc6a.42960b8","type":"http request","z":"b9cb942f.daadd8","name":"Dryer on","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/setIO?status=true&index_id=5&apikey=xxxxx","tls":"","proxy":"","authType":"","x":520,"y":220,"wires":[[]]},{"id":"4b89c7fa.f8554","type":"http request","z":"b9cb942f.daadd8","name":"Dryer off","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/setIO?status=false&index_id=5&apikey=xxxxxxxxxx","tls":"","proxy":"","authType":"","x":520,"y":260,"wires":[[]]},{"id":"b7a13c7f.4bbb08","type":"server-state-changed","z":"b9cb942f.daadd8","name":"Relay","server":"d9f08b8a.14825","version":1,"entityidfilter":"input_boolean.printer_relay","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":90,"y":322,"wires":[["68271197.8b44a"]]},{"id":"68271197.8b44a","type":"change","z":"b9cb942f.daadd8","name":"change","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":322,"wires":[["b49fa1b2.86538"]]},{"id":"b49fa1b2.86538","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":322,"wires":[["fa41a7dd.3f907"],["cf0c88f7.8f6e4"]]},{"id":"fa41a7dd.3f907","type":"http request","z":"b9cb942f.daadd8","name":"Relay on","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/setIO?status=true&index_id=6&apikey=xxxxxxxxx","tls":"","proxy":"","authType":"","x":520,"y":300,"wires":[[]]},{"id":"cf0c88f7.8f6e4","type":"http request","z":"b9cb942f.daadd8","name":"Relay off","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/setIO?status=false&index_id=6&apikey=xxxxxxx","tls":"","proxy":"","authType":"","x":520,"y":340,"wires":[[]]},{"id":"4da1e770.ad372","type":"http request","z":"b9cb942f.daadd8","name":"Status","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/getOutputStatus?apikey=xxxxxxxxx","tls":"","proxy":"","authType":"","x":90,"y":422,"wires":[["2e452f3.3bcac5"]]},{"id":"2810067a.dff8ca","type":"inject","z":"b9cb942f.daadd8","name":"","topic":"","payload":"","payloadType":"date","repeat":"15","crontab":"","once":true,"onceDelay":0.1,"x":130,"y":462,"wires":[["4da1e770.ad372"]]},{"id":"2e452f3.3bcac5","type":"json","z":"b9cb942f.daadd8","name":"","property":"payload","action":"","pretty":false,"x":210,"y":422,"wires":[["48e9bc18.3de974"]]},{"id":"48e9bc18.3de974","type":"split","z":"b9cb942f.daadd8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":330,"y":422,"wires":[["7f40ecaa.dc51a4"]]},{"id":"bd6ae33b.c97a2","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Relay3","vt":"str"},{"t":"cont","v":"Relay4","vt":"str"},{"t":"cont","v":"Relay5","vt":"str"},{"t":"cont","v":"Relay6","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":570,"y":422,"wires":[["b5e5c6ca.1ae4d8"],["17ab4214.6cae6e"],["60a41a03.06668c"],["38089129.333fee"]]},{"id":"7f40ecaa.dc51a4","type":"function","z":"b9cb942f.daadd8","name":"on/off","func":"index = msg.payload.index_id;\nstatus = msg.payload.status;\nstrStatus = (status) ? \"on\" : \"off\" ;\nout = \"Relay\" + index + \" \" + strStatus; \n\nreturn {payload:out};","outputs":1,"noerr":0,"x":450,"y":422,"wires":[["bd6ae33b.c97a2"]]},{"id":"60a41a03.06668c","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":720,"y":439,"wires":[["152ef32c.a029ed"]]},{"id":"17ab4214.6cae6e","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":720,"y":396,"wires":[["f43f8312.6b8ec8"]]},{"id":"b5e5c6ca.1ae4d8","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":720,"y":349,"wires":[["229b4285.84b9c6"]]},{"id":"38089129.333fee","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":720,"y":484,"wires":[["ed2d504b.a7804"]]},{"id":"d89b6185.72aae","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"898abdc9.03ddd8","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":961,"y":349,"wires":[[]]},{"id":"747a68f5.2738a","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"898abdc9.03ddd8","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":961,"y":396,"wires":[[]]},{"id":"5f6a8c63.baa0bc","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"898abdc9.03ddd8","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_dryer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":961,"y":441,"wires":[[]]},{"id":"6fb00f4c.007338","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"898abdc9.03ddd8","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_relay","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":961,"y":486,"wires":[[]]},{"id":"229b4285.84b9c6","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":830,"y":349,"wires":[["d89b6185.72aae"]]},{"id":"f43f8312.6b8ec8","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":830,"y":396,"wires":[["747a68f5.2738a"]]},{"id":"152ef32c.a029ed","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":830,"y":440,"wires":[["5f6a8c63.baa0bc"]]},{"id":"ed2d504b.a7804","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":830,"y":484,"wires":[["6fb00f4c.007338"]]},{"id":"d9f08b8a.14825","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true},{"id":"898abdc9.03ddd8","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

1 Like

Have you managed to get the temperature values from the Enclosure plugin imported into Home Assistant?

I have added a DHT22 sensor to the Pi and its working fine through the Octoprint web dashboard, however I want to pull the temperature/humidity readings into HomeAssistant and I am not sure if its possible? I did read the API info but it seems to relate to outputs only

Hi there,
i’ve been digging through the Repsoitory and found the following API.
In my case, I got a response from this URL:

http://<YOUR_OCTOPI_IP>/plugin/enclosure/inputs/1

This way, you can create a REST sensor:

# Octprint Enclosure Plugin
- platform: rest
  name: printer_enclosure_temp
  device_class: temperature
  unit_of_measurement: "°C"
  resource: http://<YOUR_OCTOPI_IP>/plugin/enclosure/inputs/1
  value_template: '{{ value_json.temp_sensor_temp }}'
- platform: rest
  name: printer_enclosure_hum
  device_class: humidity
  unit_of_measurement: "%"
  resource: http://<YOUR_OCTOPI_IP>/plugin/enclosure/inputs/1
  value_template: '{{ value_json.temp_sensor_humidity }}'

Hope this helps.

Does it work, does it show you the enclosure Temp and Humidity??

Verry interessted in that.

I copied your code in the config.yaml but it doesn’t work… :frowning:

Would you be so kind and help me with that topic :smiley:

Thanks

Hello. share a switch for the enclosure plugin. can anyone come in handy

- platform: command_line
    scan_interval: 5
    switches:
      light_power:
        friendly_name: "Octoprint Light"
        command_on: "curl 'http://XXX.XXX.XXX.XXX/plugin/enclosure/setIO?status=true&index_id=5&apikey=XXXXXXXXXXXX'"
        command_off: "curl 'http://XXX.XXX.XXX.XXX/plugin/enclosure/setIO?status=false&index_id=5&apikey=XXXXXXXXXXXX'"
        command_state: "curl 'http://XXX.XXX.XXX.XXX/plugin/enclosure/getOutputStatus?apikey=XXXXXXXXXXXX'"
        value_template: "{{  (value_json[1].status==1) }}"

!secret dont work. comparison with 1 added because status is true and 1. sorry for my english.
for a long time I could not understand why it does not work. quotes!

Hi @Serchio,

Just yesterday i connected my printer to HA and i wanted to control the enclosureplugin with HA.

Your code works great for controlling, but I can’t get the state to work.

Are you sure this line is correct?

    value_template: "{{  (value_json[1].status==1) }}"

I’ve checked the state command in a browser and it posts the following which seems right:

[{“status”: 1, “index_id”: 1}, {“status”: true, “index_id”: 2}]

Everything else works fine.

Can anyone take a look at this?

Thanks!

Not entirely correct))) . You must specify the correct item number in the json array. That is, the required element is the second - instead of 1 we write 2. If 3 - write 3

Got it working, thanks man!

I can’t get this to work. I used to be able to turn it off, but not back on. Have had to reinstall HomeAssistant and now can’t get it to work at all.
Getting this error.
Error parsing value: list object has no element 1 (value: [{“status”: true, “index_id”: 1}], template: {{ (value_json[1].status==1) }})

Not sure why, But recreated the enclosure plugin for led lights to id2 with exactly the same details as id1. Changed config to id2 and worked first time.
So Hours later it now works.

Hello,

did you got it to work? When i go on the resource address, it shows me the json but in homeassistant i only get unknown.

Thanks

It took me some time to get it working, but this works for me.
I’ve got a single output defined in the enclosu

- platform: command_line
  scan_interval: 5
  switches:
     octoprint_gpio:
        friendly_name: "Prusa LEDs"
        command_on: 'curl -k "https://xxx.xxx.xxx.xxx/plugin/enclosure/setIO?status=true&index_id=1&apikey=xxxxxxxx"'
        command_off: 'curl -k "https://xxx.xxx.xxx.xxx/plugin/enclosure/setIO?status=false&index_id=1&apikey=xxxxxxxx"'
        command_state: 'curl -k "https://xxx.xxx.xxx.xxx/plugin/enclosure/getOutputStatus?&apikey=xxxxxxxxx"'
        value_template: "{{value_json.0.status }}"

Expecially the value template took me some time.
You can get a preview of the json info by entering the url (https://xxx.xxx.xxx.xxx/plugin/enclosure/getOutputStatus?&apikey=xxxxxxxxx) in your webbrowser.

In my case this returned:

0
index_id 1
status false
State " OFF "

In case of multiple gpio outputs the index will be 0, 1,…etc

1 Like

FYI that the old API methods are marked deprecating-soon in the Enclosure plugin code. Home Assistant debug homeassistant.components.rest: debug was utterly useless. I resorted to TCP dumps and lots of curl debugging to figure it out.

I was able to work out the correct restful switch as follows:

switch:
  - platform: rest
    name: Enclosure Lights
    resource: http:/<octoprint_ip>/plugin/enclosure/outputs/<enclosure_id>
    method: PATCH
    verify_ssl: false
    headers:
      X-Api-Key: "<Octoprint_API_key>"
      Content-Type: "application/json"
    body_on: '{"status":true}'
    body_off: '{"status":false}'
    is_on_template: "{{ value_json.current_value }}"

Note that <enclosure_id> is the index of the Output from the Enclosure plugin settings. Look for the field described as Id used for API control in OctoPrint Settings - Enclosure Plugin.

There are quite a few other undocumented API goodies buried in the code starting here. Just look for lines with @octoprint.plugin.BlueprintPlugin.route

1 Like

Thank you this worked for me also. No let me see if i can make the temperature sensor work as well.

ive tried so many times with the old api till i found this post!
Thanks man, great job. works like hell