One of the few things that bugs me about shutting down the the Raspberry Pi is how I need to toggle the power supply to start it back up. There are a few solutions around, like an inline power switch, or the MoPi shield.. or just pull the micro USB plug out of the socket & plug it back in again. None of these I was particularly impressed with, and I wanted something a bit more intuitive & automated (which makes it easier for the kids and wife to use).


In a previous project, I'd used an Energenie power socket to turn a bass speaker off/on with the TV, and I had a spare socket (there are 2 in the box) doing nothing. What I've been able to do is set up a PHP script on the Pi that has the Energenie controller & have that control a socket that another Pi is connected to. When the other Pi is shutting down, I can call that PHP script and tell it to turn off the socket after a few seconds (giving the Pi enough time to shut down).


The process looks like this;




Here’s how you can set it up the same system…


Scripts for the Pi (which is running OSMC)


First, add a new service script.. create a new file in this folder;




Description=Energenie Remote Call to Secondary Pi

ExecStop=/bin/sh /home/osmc/



Enable the service with;


sudo systemctl enable callenergenie.service


That service calls this new helper script which will call some PHP running on our Pi server;




echo Calling energenie socket...
wget --quiet --background --output-document="callenergenie.log" ""
echo Sleeping for 2 seconds
sleep 2
echo Done.
exit 0


Scripts for the Pi server with the Energenie shield/controller




/* MattC - Call this with various parameters..

delay = time in seconds to sleep before calling Energenie
switch = which socket to talk to
state = turn socket on/off

e.g. callenergenie.php?delay=5&switch=2&state=off


print ("Waiting ".$_GET['delay']." seconds");
print ("Switching socket ".$_GET['switch']." ".$_GET['state']);
exec("sudo python /var/www/ ".$_GET['state']." ".$_GET['switch']);



I needed to add the Apache user to the list of SUDO'ers, which I'd rather not have done, but the Energenie Python script supplied by the manufacturer didn't like being run as a regular user


nano /etc/sudoers




If there's a better way to do this, please let me know.