3D printer control with Octoprint and Node Red

My setup is inspired by others but I went the way of using Node Red more than I saw others do for my setup.
I have a CR10 - Mini that I control with Octoprint running on a PI3 B+ The Pi is powered from the printers Power supply via a LM2596 buck converter. I have a Shelly 1 switching the main power to the printer,

To do a soft shutdown of Octoprint I have a script that calls a rest command

#shutdown octopi RPI
rest_command:
  shutdown_octoprint:
    url: http://octopi.local/api/system/commands/core/shutdown #use numeric IP alternatively
    method: POST
    headers: 
      X-Api-Key: !secret octoprint_api

For manually powering on and off the printer I have 2 input Booleans and some logic in node red to avoid accidental shutdown during a print.

The automated shutdown flow looks like this and if im not home or if is later than 22:00 it shuts down the printer after 5min, also if not home I get a simple email about it. I will try to add a picture from the pi cam to the email at some point. If im at home i have a 20 min timer in case I want to start a new print right away.

Just because its possible My google homes announces that the print is done and it blinks my Livingroom ceiling lamp

In Home Assistant there is a simple view to show some information

Im certain that this can be done more elegant but it does work very well.

if someone is interested here is the Node Red flow

[{"id":"7428b3a1.32108c","type":"tab","label":"3D Print Done","disabled":false,"info":"When octo print changes status on binary sensor for printing \noffice lamp blinks \nlivingroom lamp blinks \nif no new print started in 15 min turn power off (to be set up)"},{"id":"829b4668.3b50c8","type":"server-state-changed","z":"7428b3a1.32108c","name":"3D print done","server":"ad021f45.a359d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":" binary_sensor.octoprint_printing","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":119,"y":131,"wires":[[],["8b451685.f2dd08","f2547c81.7359","989cec39.9de1c"]]},{"id":"18635730.208fb9","type":"api-call-service","z":"7428b3a1.32108c","name":"living room lamp","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"toggle","entityId":"switch.livingroom_lamp","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":640,"y":80,"wires":[[]]},{"id":"59c2db31.4e80a4","type":"api-call-service","z":"7428b3a1.32108c","name":"living room lamp","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"toggle","entityId":"switch.livingroom_lamp","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":840,"y":80,"wires":[[]]},{"id":"5fb5cef6.ab3bf","type":"api-call-service","z":"7428b3a1.32108c","name":"living room lamp","server":"ad021f45.a359d","version":"1","debugenabled":false,"service_domain":"homeassistant","service":"toggle","entityId":"switch.livingroom_lamp","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1040,"y":80,"wires":[[]]},{"id":"be6a4a52.e8b4a8","type":"api-call-service","z":"7428b3a1.32108c","name":"living room lamp","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"toggle","entityId":"switch.livingroom_lamp","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1200,"y":140,"wires":[[]]},{"id":"4e26e3e3.9a299c","type":"delay","z":"7428b3a1.32108c","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":840,"y":140,"wires":[["5fb5cef6.ab3bf","c2fd9d74.b635e"]]},{"id":"39fa9cc4.2fcbd4","type":"delay","z":"7428b3a1.32108c","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":640,"y":140,"wires":[["59c2db31.4e80a4","4e26e3e3.9a299c"]]},{"id":"c2fd9d74.b635e","type":"delay","z":"7428b3a1.32108c","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1026,"y":139,"wires":[["be6a4a52.e8b4a8"]]},{"id":"8b451685.f2dd08","type":"api-current-state","z":"7428b3a1.32108c","name":"living room lamp on","server":"ad021f45.a359d","version":"1","outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"switch.livingroom_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":80,"wires":[[],["18635730.208fb9","39fa9cc4.2fcbd4"]]},{"id":"f2547c81.7359","type":"api-current-state","z":"7428b3a1.32108c","name":"living room lamp off","server":"ad021f45.a359d","version":"1","outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"switch.livingroom_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":140,"wires":[[],["39fa9cc4.2fcbd4","18635730.208fb9"]]},{"id":"60aec84.e178038","type":"server-state-changed","z":"7428b3a1.32108c","name":"3D print done","server":"ad021f45.a359d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.octoprint_printing","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":110,"y":520,"wires":[["99e0d653.eb8908"],["a4cac99e.d99948"]]},{"id":"17d445d8.c72b2a","type":"change","z":"7428b3a1.32108c","name":"on to text","rules":[{"t":"set","p":"payload","pt":"msg","to":"3D Print is done","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":220,"wires":[["61977ca1.bc8c34","eeffff9d.0ff95"]]},{"id":"61977ca1.bc8c34","type":"cast-to-client","z":"7428b3a1.32108c","name":"Living room G mini","url":"","contentType":"","message":"","language":"en","ip":"192.168.40.109","port":"","volume":"50","x":690,"y":240,"wires":[[]]},{"id":"989cec39.9de1c","type":"time-range-switch","z":"7428b3a1.32108c","name":"night","lat":"36.08999","lon":"-79.82967","startTime":"22:00","endTime":"06:30","startOffset":0,"endOffset":0,"x":330,"y":220,"wires":[[],["17d445d8.c72b2a"]]},{"id":"a4cac99e.d99948","type":"api-current-state","z":"7428b3a1.32108c","name":"Jesper Home","server":"ad021f45.a359d","version":1,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"person.jesper","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":310,"y":520,"wires":[["a7bfd393.977cf","3dc0b487.e3e90c"],["f7a1f309.a2498"]]},{"id":"a7bfd393.977cf","type":"time-range-switch","z":"7428b3a1.32108c","name":"night","lat":"36.08999","lon":"-79.82967","startTime":"22:00","endTime":"06:30","startOffset":0,"endOffset":0,"x":570,"y":520,"wires":[["3dc0b487.e3e90c"],["9908c118.3d94a"]]},{"id":"ae774441.a66328","type":"inject","z":"7428b3a1.32108c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"on","payload":"","payloadType":"str","x":170,"y":1060,"wires":[[]]},{"id":"3dc0b487.e3e90c","type":"stoptimer","z":"7428b3a1.32108c","duration":"5","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":700,"y":460,"wires":[["32894459.7c4a8c"],[]]},{"id":"9908c118.3d94a","type":"stoptimer","z":"7428b3a1.32108c","duration":"20","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":700,"y":580,"wires":[["32894459.7c4a8c"],[]]},{"id":"99e0d653.eb8908","type":"change","z":"7428b3a1.32108c","name":"send stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":580,"wires":[["9908c118.3d94a","3dc0b487.e3e90c"]]},{"id":"d5aa65a6.c45c98","type":"e-mail","z":"7428b3a1.32108c","server":"smtp.gmail.com","port":"465","secure":true,"tls":true,"name":"[email protected]","dname":"Jeppe","x":510,"y":420,"wires":[]},{"id":"f7a1f309.a2498","type":"change","z":"7428b3a1.32108c","name":"on to text","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hey, The 3D Print is done you should check it out when you get home ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":420,"wires":[["d5aa65a6.c45c98"]]},{"id":"eeffff9d.0ff95","type":"cast-to-client","z":"7428b3a1.32108c","name":"Office G mini","url":"","contentType":"","message":"","language":"en","ip":"192.168.40.10","port":"","volume":"50","x":670,"y":180,"wires":[[]]},{"id":"32894459.7c4a8c","type":"api-call-service","z":"7428b3a1.32108c","name":"Octoprint Shutdown","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"script","service":"octoprint_shutdown","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":930,"y":520,"wires":[["4bfe8b1a.5a2a04"]]},{"id":"3520f805.9e2de8","type":"api-call-service","z":"7428b3a1.32108c","name":"Printer Power","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.3d_printer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1220,"y":520,"wires":[["bfbe791f.d73318"]]},{"id":"4bfe8b1a.5a2a04","type":"stoptimer","z":"7428b3a1.32108c","duration":"1","units":"Minute","payloadtype":"num","payloadval":"0","name":"shut down delay timer","x":980,"y":620,"wires":[["3520f805.9e2de8"],[]]},{"id":"ec83a2d1.a7442","type":"server-state-changed","z":"7428b3a1.32108c","name":"Printer Turn On","server":"ad021f45.a359d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.3d_printer_on","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":120,"y":760,"wires":[["d2575c0.c5e5ba8","4485a003.7b2e"],[]]},{"id":"d2575c0.c5e5ba8","type":"api-current-state","z":"7428b3a1.32108c","name":"printing state","server":"ad021f45.a359d","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.octoprint_printing","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":330,"y":760,"wires":[[],["15ad3776.5bbdf9"]]},{"id":"15ad3776.5bbdf9","type":"api-call-service","z":"7428b3a1.32108c","name":"Printer Power On","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.3d_printer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":550,"y":720,"wires":[["de29d8bc.b266a8"]]},{"id":"c6a1120b.517bf","type":"server-state-changed","z":"7428b3a1.32108c","name":"Printer Turn Off","server":"ad021f45.a359d","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.3d_printer_off","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":120,"y":880,"wires":[["499442a6.dd81bc"],[]]},{"id":"499442a6.dd81bc","type":"api-current-state","z":"7428b3a1.32108c","name":"printing state","server":"ad021f45.a359d","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.octoprint_printing","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":330,"y":880,"wires":[["de29d8bc.b266a8"],["c6c118fe.3c5008"]]},{"id":"c6c118fe.3c5008","type":"api-call-service","z":"7428b3a1.32108c","name":"Octoprint Shutdown","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"script","service":"octoprint_shutdown","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":550,"y":880,"wires":[["e019e91f.d81758","190a410.be86bbf"]]},{"id":"e019e91f.d81758","type":"stoptimer","z":"7428b3a1.32108c","duration":"30","units":"Second","payloadtype":"num","payloadval":"0","name":"shut down delay timer","x":780,"y":860,"wires":[["ac481406.7748c8"],[]]},{"id":"ac481406.7748c8","type":"api-call-service","z":"7428b3a1.32108c","name":"Printer Power Off","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.3d_printer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1010,"y":860,"wires":[[]]},{"id":"de29d8bc.b266a8","type":"api-call-service","z":"7428b3a1.32108c","name":"Off bolean off","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.3d_printer_off","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":760,"y":780,"wires":[[]]},{"id":"190a410.be86bbf","type":"api-call-service","z":"7428b3a1.32108c","name":"On bolean off","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.3d_printer_on","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":880,"y":940,"wires":[[]]},{"id":"bfbe791f.d73318","type":"api-call-service","z":"7428b3a1.32108c","name":"On bolean off","server":"ad021f45.a359d","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.3d_printer_on","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1300,"y":620,"wires":[[]]},{"id":"4485a003.7b2e","type":"change","z":"7428b3a1.32108c","name":"send stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":820,"wires":[["e019e91f.d81758"]]},{"id":"ad021f45.a359d","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
2 Likes

I like your solutions!
I realise you’re using “connect on power on” for your Octoprint to avoid needing to send a “connect” request.

That is correct

Great project, it gives me some good idea’s on what I need to do with my rig and Octopi.

Hey guys,

Im looking to send pause/resume and home commands via node red.

I can turn Octoprint off with adding the API key as a header and then a HTTP request as a POST command.

But I’m unsure as how to send something like

{
“command”: “home”,
“axes”: [“x”, “y”]
}

Thanks in advance :slight_smile: