The general process of this guide was taken from here, and advice from here.

Prerequisites

Install python-pip:

osmc@osmc:~$ sudo apt-get install python-pip

Use apt-get to install setuptools:

osmc@osmc:~$ sudo apt-get install python-setuptools

use apt-get to instal gcc:

osmc@osmc:~$ sudo apt-get install gcc-arm-linux-gnueabihf
s

Use pip to install wheel:

osmc@osmc:~$ sudo pip install wheel

Use pip to install RPi.GPIO and gpiozero:

osmc@osmc:~$ sudo pip install RPi.GPIO
osmc@osmc:~$ sudo pip install gpiozero

Check the python package directory:

osmc@osmc:~$ cd /usr/bin
osmc@osmc:/usr/bin$ ls

Amongst other files, the list should contain:

python

Now we create the Unit file for systemd:

osmc@osmc:/usr/bin$ cd /lib/systemd/system
osmc@osmc:/lib/systemd/system$ sudo nano power-off-pi.service

Paste the following into Nano:

[Unit]
Description=Power Off Pi Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/osmc/power-off-pi.py

[Install]
WantedBy=multi-user.target

Ctrl and o for save

Ctrl and x for exit

Set the permissions of the Unit file to 644:

osmc@osmc:~$ sudo chmod 644 /lib/systemd/system/power-off-pi.service

It's now time to create the python shutdown script:

osmc@osmc:/usr/bin$ cd /home/osmc/
osmc@osmc:~$ sudo nano power-off-pi.py

Paste the following into Nano:
This code if from AndrewH7's work here.

#!/bin/python
#This script was authored by AndrewH7 and belongs to him (www.instructables.com/member/AndrewH7)
#You have permission to modify and use this script only for your own personal usage
#You do not have permission to redistribute this script as your own work
#Use this script at your own risk

import RPi.GPIO as GPIO
import os

gpio_pin_number=22
#Replace YOUR_CHOSEN_GPIO_NUMBER_HERE with the GPIO pin number you wish to use
#Make sure you know which rapsberry pi revision you are using first
#The line should look something like this e.g. "gpio_pin_number=7"

GPIO.setmode(GPIO.BCM)
#Use BCM pin numbering (i.e. the GPIO number, not pin number)
#WARNING: this will change between Pi versions
#Check yours first and adjust accordingly

GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#It's very important the pin is an input to avoid short-circuits
#The pull-up resistor means the pin is high by default

try:
    GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
    #Use falling edge detection to see if pin is pulled
    #low to avoid repeated polling
    os.system("sudo shutdown -h now")
    #Send command to system to shutdown
except:
    pass

GPIO.cleanup()
#Revert all GPIO pins to their normal states (i.e. input = safe)

I have elected to use GPIO pin BCM 22 (physical pin 15), see here, this is the only change I made to the python script.

Ctrl and o for save

Ctrl and x for exit

osmc@osmc:~$ sudo systemctl daemon-reload
osmc@osmc:~$ sudo systemctl enable power-off-pi.service

This should give:

Created symlink /etc/systemd/system/multi-user.target.wants/power-off-pi.service -> /lib/systemd/system/power-off-pi.service.

Reboot the Pi:

osmc@osmc:~$ sudo reboot

Clearly this will close the SSH session.

After the reboot re-connect via SSH (using your pi's IP address):

[scott@USBmanjaro ~]$ ssh osmc@192.168.1.38

Check the service

osmc@osmc:~$ sudo systemctl status power-off-pi.service