Automatic power off of a Raspberry Pi

How to power off the system after shutdown

My 3D printer is controlled by a SKR PRO control board on which which the firmware [[ https://www.klipper3d.org/ | Klipper]] is running. Klipper is a two-part firmware, which requires also another computer (typically a Raspberry Pi) to run the Klipper server.

In my case the Raspberry Pi is powered by an ATX power supply and the printer motors by an extra 24 V power supply (since ATX only provides 12 V). Additionally I have a relay controlled by the Raspberry Pi which controls the power to the SKR Pro.

With this setup I would need to physically turn off power after the printer is shut down, so I installed a Gosund SP111 (or equivalent) plug which gives me the opportunity to measure power consumption and to have a master switch.

The structure is shown in the following flowchart.

Components of the printer

I wanted to automate the process further so I modified the Gosund plug and I wrote a service for the Raspberry Pi with the goal of having a delayed physical power down after the shutdown of the Raspberry Pi is completed.

The Gosund plug was already reflashed with ESPhome, so I added only an extra section:

button:
  - platform
: template
    id
: delayed_off
    name
: "${plug_name} - Delayed off"
    on_press
:
      - delay
: 30s
      - switch.turn_off
: relay

When the command is issued, the Gosund waits for 30 seconds (enough for a clean shutdown) and then cuts the power to the whole printer.

On the Raspberry Pi I added a service which is run at shutdown for the purpose:

[Unit]
Description=Shut down power (delayed) at shutdown via ESPhome
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop='/usr/bin/curl -X POST http://192.168.3.155/button/hevo_-_delayed_off/press'

[Install]
WantedBy=multi-user.target

The service file was saved to /etc/systemd/system/esphome_poweroff.service and it was loaded with

chmod 644 /etc/systemd/system/esphome_poweroff.service
systemctl daemon-reload
systemctl enable /etc/systemd/system/esphome_poweroff.service

Author: Olaf Marzocchi

First revision: 2021-02-16.
Last revision: 2022-01-01.