--- /dev/null
+#! /bin/bash
+
+# Send warning email and shut down system when UPS battery is low.
+# Andrew Lorimer, January 2020
+
+recipient="andrew"
+
+ctime=`date`
+state=`pwrstat -status`
+model=`echo $state | grep -oP 'Model Name\.+ \K\w+'`
+runtime=`echo $state | grep -oP 'Remaining Runtime\.+ \K\d+\s?\w+(?=\.)?'`
+level=`echo $state | grep -oP 'Battery Capacity\.+ \K\d+'`"%"
+
+echo -e "Battery level is $level (expected runtime $runtime) as of $ctime. Shutting down system in one minute.\n$state" | mail -a "Importance: High" -a "Content-Type: text/plain; charset=UTF-8" -s "⚠️🔋 Battery level $level on $model" $recipient
+sleep 60
+
+echo "Starting system shutdown due to low battery" | systemd-cat -t `basename "$0"`
+shutdown now
--- /dev/null
+#! /bin/bash
+
+# Send warning email on power loss. Triggered by pwrstatd.
+# Andrew Lorimer, January 202t
+
+recipient="andrew"
+
+ctime=`date`
+state=`pwrstat -status`
+model=`echo $state | grep -oP 'Model Name\.+ \K\w+'`
+runtime=`echo $state | grep -oP 'Remaining Runtime\.+ \K\d+\s?\w+(?=\.)?'`
+
+echo -e "Mains power was lost at $ctime. Expected runtime is $runtime.\n$state" | mail -a "Importance: High" -a "Content-Type: text/plain; charset=UTF-8" -s "⚠️🔌 Power failure on $model" $recipient