# Conditional Power Shutdown

**URL:** <https://klipper.discourse.group/t/conditional-power-shutdown/19891>\
**Category:** General Discussion\
**Created:** [November 5, 2024, 3:08pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891 "2024-11-05T15:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slwise](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@slwise](https://klipper.discourse.group/u/slwise)\
**Post date:** [November 5, 2024, 3:08pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891/1 "2024-11-05T15:08:52Z")

</div>

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

…

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [November 5, 2024, 3:33pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891/2 "2024-11-05T15:33:17Z")

</div>

Hello @slwise !

Please have a look on this:

> [@How-To Ask for Help](https://klipper.discourse.group/t/how-to-ask-for-help/6327/1):
>
> How-To Ask for Help General All contributors here help out in their spare time and because they want to give something back to the Klipper project. Be respectful and patient. If someone knows the answer to your question, they will answer it. Choose the right Category Questions / Issues should go into the General Discussion category most of the time. Filling the template is mandatory. The categories Config, Developers and Macros are used to post results of one’s work for the benefit of others T…

---

<div class="post-metadata">

**Author:** ![DerGrinch](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@DerGrinch](https://klipper.discourse.group/u/DerGrinch)\
**Post date:** [November 5, 2024, 3:51pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891/3 "2024-11-05T15:51:39Z")

</div>

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

 ![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/9/92df0eaaab86b8c100fa9454bedbe997d77967a1.png)

```auto
##################################################################
# 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.

```auto
{% 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.

```auto
[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.

```auto
[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.

---

<div class="post-metadata">

**Author:** ![slwise](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@slwise](https://klipper.discourse.group/u/slwise)\
**Post date:** [November 5, 2024, 7:52pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891/4 "2024-11-05T19:52:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/64419bf2aac6639e6ef5cef200dcd456b0a01ac3.png) [@system](https://klipper.discourse.group/u/system)\
**Post date:** [January 4, 2025, 7:53pm UTC](https://klipper.discourse.group/t/conditional-power-shutdown/19891/5 "2025-01-04T19:53:29Z")

</div>

Closed on January 4, 2025, 7:53pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
