Run script when a print job is scheduled through the API

I've a command-line script, "printer_up" which turns on the printer and makes octopi connect to it using octopi API. That's the part which works flawless when triggered from the command-line.

Now the part which i don't know how to make it work.

PrusaSlicer sends the Gcode to Octoprint using API i guess so i want to run my command-line script "printer_up" when slicer sends the code to octopi so that it prints without issues.

I looked the event list but didn't find any which can immediately run when a print job is scheduled.

You could use the file upload event. Gets fired whenever a file gets uploaded.

Hi! I created an event:

events:
  enabled: True
  subscriptions:
  - event: Upload
    command: sh /home/pi/.octoprint/scripts/printer_up.sh
    type: system

it did start my printer but state got changed like this:

Relevant log:

tail -f ~/.octoprint/logs/octoprint.log -n 0
2019-07-08 20:27:10,101 - octoprint.filemanager.analysis - INFO - Starting analysis of local:blade_holder_PLA0.16mm.gcode
2019-07-08 20:27:10,104 - octoprint.filemanager.analysis - INFO - Invoking analysis command: /home/pi/oprint/bin/python2 -m octoprint analysis gcode --speed-x=6000 --speed-y=6000 --max-t=10 --throttle=0.0 --throttle-lines=100 /home/pi/.octoprint/uploads/blade_holder_PLA0.16mm.gcode
2019-07-08 20:27:10,104 - octoprint.events.fire - DEBUG - Firing event: MetadataAnalysisStarted (Payload: {'origin': 'local', 'path': u'blade_holder_PLA0.16mm.gcode', 'type': 'gcode', 'name': u'blade_holder_PLA0.16mm.gcode', 'file': u'blade_holder_PLA0.16mm.gcode'})
2019-07-08 20:27:10,107 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:10,112 - octoprint.events.fire - DEBUG - Firing event: FileAdded (Payload: {'path': u'blade_holder_PLA0.16mm.gcode', 'storage': 'local', 'name': u'blade_holder_PLA0.16mm.gcode', 'type': ['machinecode', 'gcode']})
2019-07-08 20:27:10,113 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:10,115 - octoprint.events.fire - DEBUG - Firing event: UpdatedFiles (Payload: {'type': 'printables'})
2019-07-08 20:27:10,115 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:10,118 - octoprint.events.fire - DEBUG - Firing event: UpdatedFiles (Payload: {'type': 'gcode'})
2019-07-08 20:27:10,120 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:10,142 - octoprint.printer.standard - INFO - Cannot load file: printer not connected or currently busy
2019-07-08 20:27:10,144 - octoprint.events.fire - DEBUG - Firing event: Upload (Payload: {'path': u'blade_holder_PLA0.16mm.gcode', 'name': u'blade_holder_PLA0.16mm.gcode', 'file': u'blade_holder_PLA0.16mm.gcode', 'target': u'local'})
2019-07-08 20:27:10,158 - octoprint.events - DEBUG - Sending action to <bound method CommandTrigger.eventCallback of <octoprint.events.CommandTrigger object at 0x719ae4f0>>
2019-07-08 20:27:10,167 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:10,171 - octoprint.events - INFO - Executing a system command
2019-07-08 20:27:12,871 - octoprint.filemanager.analysis - INFO - Analysis of entry local:blade_holder_PLA0.16mm.gcode finished, needed 2.77s
2019-07-08 20:27:12,992 - octoprint.events.fire - DEBUG - Firing event: MetadataAnalysisFinished (Payload: {'origin': 'local', 'path': u'blade_holder_PLA0.16mm.gcode', 'name': u'blade_holder_PLA0.16mm.gcode', 'file': u'blade_holder_PLA0.16mm.gcode', 'result': {'estimatedPrintTime': 1878.4187417321227, 'printingArea': {'maxZ': 13.64, 'minX': 1.0, 'maxX': 122.228, 'maxY': 200.0, 'minY': 20.0, 'minZ': 0.2}, 'dimensions': {'width': 121.228, 'depth': 180.0, 'height': 13.440000000000001}, 'filament': {'tool0': {'volume': 2.8069285669009116, 'length': 1166.985290000013}}}})
2019-07-08 20:27:12,993 - octoprint.events - DEBUG - Sending action to <bound method Printer._on_event_MetadataAnalysisFinished of <octoprint.printer.standard.Printer object at 0x7197f5b0>>
2019-07-08 20:27:12,994 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,010 - octoprint.events.fire - DEBUG - Firing event: Connecting (Payload: None)
2019-07-08 20:27:17,010 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,088 - octoprint.util.comm - INFO - Changing monitoring state from "Offline" to "Opening serial port"
2019-07-08 20:27:17,093 - octoprint.events.fire - DEBUG - Firing event: PrinterStateChanged (Payload: {'state_string': 'Opening serial port', 'state_id': 'OPEN_SERIAL'})
2019-07-08 20:27:17,097 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,095 - octoprint.util.comm - INFO - Changing monitoring state from "Opening serial port" to "Connecting"
2019-07-08 20:27:17,102 - octoprint.events.fire - DEBUG - Firing event: PrinterStateChanged (Payload: {'state_string': 'Connecting', 'state_id': 'CONNECTING'})
2019-07-08 20:27:17,106 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,113 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2019-07-08 20:27:17,120 - octoprint.util.comm - INFO - Changing monitoring state from "Connecting" to "Operational"
2019-07-08 20:27:17,124 - octoprint.events.fire - DEBUG - Firing event: PrinterStateChanged (Payload: {'state_string': 'Operational', 'state_id': 'OPERATIONAL'})
2019-07-08 20:27:17,127 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,133 - octoprint.events.fire - DEBUG - Firing event: Connected (Payload: {'baudrate': 250000, 'port': u'/tmp/printer'})
2019-07-08 20:27:17,135 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2019-07-08 20:27:17,135 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>
2019-07-08 20:27:17,142 - octoprint.plugins.firmwareupdater - INFO - Got CONNECTED event
2019-07-08 20:27:17,143 - octoprint.plugins.firmwareupdater - INFO - Run postflash flag is not set
2019-07-08 20:27:17,152 - octoprint.util.comm - INFO - Printer reports firmware name "Klipper"
2019-07-08 20:27:17,152 - octoprint.events.fire - DEBUG - Firing event: FirmwareData (Payload: {'data': {'FIRMWARE_VERSION': 'v0.7.0-654-g57eb0e8', 'FIRMWARE_NAME': 'Klipper'}, 'name': 'Klipper'})
2019-07-08 20:27:17,154 - octoprint.util.comm - INFO - Detected Klipper firmware, enabling relevant features for issue free communication
2019-07-08 20:27:17,154 - octoprint.events - DEBUG - Sending action to <bound method PrinterStateConnection._onEvent of <octoprint.server.util.sockjs.PrinterStateConnection object at 0x6cdfaa50>>

Now, the problem is that printer turns on and is automatically connected but doesn't start printing.
How can i ensure that the print starts automatically?

Update 1 : Changed event to FileAdded in hopes that it will start printer before, octoprints gives up on the uploaded file but it didn't help, same effect.

You could consider adding a command-line argument to your script which accepts the file that was uploaded. Your script could wait a moment after what it already does and then invoke the OctoPrint API to select and print it.

Thanks, that worked :slight_smile:

events:
  enabled: true
  subscriptions:
  - command: sh /home/pi/.octoprint/scripts/select_print.sh {target} {path}
    event: Upload
    type: system

Within script select_print.sh:

printer_up
sleep 3
curl -d '{"command": "select", "print": true}' -H "X-Api-Key: XX" -H "Content-Type: application/json" -X POST http://localhost:5000/api/files/$1/$2
1 Like