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/ 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.
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
Update 2025
Sometime after publishing I noticed that the service which should run at shutdown doesn’t work, probably it should be written to link to other steps of the shutdown procedure. I never found the time to investigate how, so I turned off manually the plug.
The delayed off in the plug works just fine though.
Now a better solution could be linking the button press to Moonraker, which has a section “power”.
First revision: 2021-02-16.