Conditional Power Shutdown

Basic Information:

Printer Model: Upgraded Ender 3 Pro
MCU / Printerboard: SKR Mini E3 V3
Host / SBC BIQU Pi V1.2
klippy.log

Describe your issue:

I recently switched to Klipper from Marlin and everything works great. There is one thing I would like to be able to do but with my lack of experience in writing macros I need some help.

I’m using the BTT Power Relay V1.2 to shutdown the printer when it is finished. I found a set of macros that accomplish this. When I was running Marlin and a BTT TFT35 E3 V3 and the shutdown process started it would prompt you with the option to shutdown IMMEDIATE or CANCEL. If you didn’t respond by the time the hotend cooled down to the specified it would go ahead and turn off the printer. Is there any way simulate this within a Klipper macro? I just want the option to abort the shutdown process so I can print something else. I use the DO_SHUTDOWN macro in my ending gcode. Here are the macros I have so far:

[output_pin power_detect]

Pull the pin initially high after starting Klipper

pin: PC13
value: 1
shutdown_value: 0

[gcode_macro POWER_ON]
description: Turn on the printer via the relay
gcode:
SET_PIN PIN=power_detect VALUE=1

[gcode_macro POWER_OFF]
description: Turn off the printer via the relay
gcode:
{ action_respond_info(‘Shutting down now’) }
SET_PIN PIN=power_detect VALUE=0

[gcode_macro DO_SHUTDOWN]
description: Turn off all heaters and cut power once below 50C
gcode:
TURN_OFF_HEATERS
TEMPERATURE_WAIT SENSOR=extruder MAXIMUM=50
POWER_OFF

Hello @slwise !

Please have a look on this:

1 Like

Hi,

I have solved the whole thing a little differently but maybe you are still interested.

First I created a power device with which I can set whether the printer should shutdown at the end of printing or not. Of course, this is also possible with a normal pin, but I wanted to have it in Mainsail in the overview of the power devices. This needs the small macro PRINTER_SHUTDOWN_AFTER_PRINT

##################################################################
# Enable / Disable auto shutdown after a print finished and the nozzle cooled down
##################################################################
[gcode_macro PRINTER_SHUTDOWN_AFTER_PRINT]
variable_printer_shutdown_after_print: True
variable_value: 1
gcode:
    {action_respond_info("Printer auto shutdown toggled")}
    SET_GCODE_VARIABLE MACRO=PRINTER_SHUTDOWN_AFTER_PRINT VARIABLE=printer_shutdown_after_print VALUE={ printer["gcode_macro PRINTER_SHUTDOWN_AFTER_PRINT"].printer_shutdown_after_print == False }
    # To show the actually state in the power device bar, an variable 'value' must be returned
    SET_GCODE_VARIABLE MACRO=PRINTER_SHUTDOWN_AFTER_PRINT VARIABLE=value VALUE={ printer["gcode_macro PRINTER_SHUTDOWN_AFTER_PRINT"].printer_shutdown_after_print == False }

In my macro, which is executed at the end of printing, first I check with an if statement this power device setting, if yes, the delayed macro PRINTER_SHUTDOWN_PREPARE is executed every 120 seconds.

{% if (printer["gcode_macro PRINTER_SHUTDOWN_AFTER_PRINT"].printer_shutdown_after_print)%}
    UPDATE_DELAYED_GCODE ID=PRINTER_SHUTDOWN_PREPARE DURATION=120 ; after 120 seconds, check the nozzle temperature and shutdown the printer if its low enough
  {% endif %}

First I set in PRINTER_PREPARE_SHUTDOWN other settings that should be done before shutdown the printer, such as the cooling fan of the Raspi.

[delayed_gcode PRINTER_SHUTDOWN_PREPARE]
gcode:
  {action_respond_info("Printer try to auto shutdown")}
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=RasperryPi_CoolingFan TARGET=0
  UPDATE_DELAYED_GCODE ID=PRINTER_SHUTDOWN DURATION=5

The actual shutdown takes place by calling the delayed macro PRINTER_SHUTDOWN.

[delayed_gcode PRINTER_SHUTDOWN]
gcode:
  {% if (printer.idle_timeout.state == 'Printing') %}
    {action_respond_info("Auto shutdown stopped. Printing is active. No retry.")}
  {% elif (printer['extruder']['temperature'] | int > 50) %}
    {action_respond_info("Shutdown stopped. Nozzle temperature about 50 C. Retry in 60 seconds.")}
    M104 S0    ; Set target nozzle temperature to 0°C without waiting
    UPDATE_DELAYED_GCODE ID=PRINTER_SHUTDOWN DURATION=60
  {% else %}
     {action_call_remote_method("set_device_power", device="Printer", state="off")} #shutdown the printer
  {% endif %}

This checks whether the printer is currently printing again, in this case the process should obviously be stopped.
It also checks the extruder temperature, as long as this is too high, the macro is repeated every 60 seconds.

1 Like

Thank you so much for your response. I will definitely look into this.