If you're like me and you have a Raspberry Pi for occasional use as a Kodi media device with OSMC at its core, you may also have an interest in being able to shut the power off to the Pi automatically. There are various ways of implementing a power switch on the Pi, but this isn't one of them. Although you could just use a power switch (or just turn the Pi off by pulling the plug) you should also manually shutdown Kodi because turning the power off while Kodi is loaded and running can supposedly cause corruption to occur on the SD Card.
This is why automating it is preferential as it just makes it all easier to operate and you can do it using CEC; a standard set out years back for controlling devices connected over HDMI using just one remote. Now I'm going to suddenly get right to the point without really going into detail and then end this article, as even I admit it's a niche request to be able to power down the Pi and effectively turn it off in an automatic fashion. This will probably frustrate some people looking for the finer details but I'm mostly confident anyone could have figured all this out for themselves anyway, I'm merely suggesting it as a method rather than providing a full-on tutorial. Anyway there are some pre-requisites:
- Raspberry Pi — Duh.
- TV that supports CEC — This is rather important.
- Energy saving power plug — This is even more important.
Essentially you set everything up to use your energy saving plug. I use E.ON (an energy company in the UK) branded plugs that have a remote sensor. This thing powers on when you use the TV remote and detects when power is ramped up on a particular device plugged into it and stays on throughout. When power drops (i.e. when the main device has gone into standby mode) after a few seconds it shuts the power off to those devices connected to the socket you want to lose power automatically.
You'll want something like the TV to be in the main socket on the adapter, as that'll be the device going into Standby mode. In Kodi's settings for the CEC options you want to change it to shutdown when the TV does and possibly also not to switch the inputs when Kodi starts up to avoid it annoying you, unless you only plan to use the TV with Kodi. And that's it, when you turn the TV off Kodi also shuts down making it safe for the Pi to lose power and after a few seconds because the TV has gone into standby mode the energy saving plug detects this and shuts the power off to the lot.