Using NUT UPS (linux) to trigger Rule Machine: power outage/restoration actions

You can monitor the NUT UPS app to drive rule machine actions. I decided to go the direct route and trigger rules directly from NUT UPS, as it would be more responsive and less load on the hubitat. The following is mainly a warning to those naive enough to follow in my footsteps. I forgot how arcane NUT UPS configuration is. I finally gave up on fancy stuff (e.g., UPSSCHED is a nightmare!) and got something quite simple in the end.

The Rule Machine side is simple:

  1. A "Power Restored" rule with two triggers: Local end point, and Location event: systemStart.
  2. A "Power Lost" rule with one trigger: Local end point.

My "Power Lost" rule has one action: Cancel Timed Actions: Power Restored. This is designed to stop the power restored activity if the power goes out again.

My "Power Restored" rule starts with: Cancel Timed Action: **This Rule**. It then goes on to do things to "catch up" on time-based or mode-based rules (e.g., power went out during the day, it's now night and the outside lights should be on). Each of the actions is delayed 30 seconds, random, cancelable. This is so they don't overload the hub during startup and give enough time to be hopeful that the power won't go out again.

The NUT UPS side is where the fun occurred. I couldn't get UPSSCHED to work (needs two new configuration files and a change in the upsmon.conf file, and I never got it quite right). After rereading TFM for the third time, I noticed that NOTIFYCMD provides to the script it points to the UPS issue in an environment variable NOTIFYTYPE.

So, with that all out of the way, here's the setup. In upsmon.conf, include the following line:

NOTIFYCMD /etc/nut/notify.sh

where ''notify.sh'' is the script you'll run when something happens. Also, in upsmon.conf, make sure the NOTIFYFLAG lines include EXEC, like this:

NOTIFYFLAG ONLINE     SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT     SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT    SYSLOG+WALL+EXEC
NOTIFYFLAG FSD        SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK     SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD    SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN   SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM     SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT   SYSLOG+WALL+EXEC

Then, write notify.sh (if you're using NUT, you probably already have a brief version of this file). My version, following, also emails and texts me about the status:

#!/bin/sh
echo "$@" | mail -s "UPS Event on `hostname`" MyEmail@gmail.com,5305555555@txt.att.net
case "$NOTIFYTYPE" in
     "ONLINE")
        wget -O - http://the_rest_of_this_is_the_local_endpoint_for_Power_Restored > /dev/null
        ;;
     "ONBATT")
        wget -O - http://the_rest_of_this_is_the_local_endpoint_for_Power_Lost > /dev/null
        ;;
esac

Test this by pulling the power on the ups to see that it really works. If not, check the permissions on the ''notify.sh'' file. Mine is 755, as the file is not run by root, but by nut. If you want to shut down the hub on low battery, you could use the SHUTDOWN notice from nut in the above shell script to send to a rule that powers off Hubitat. I didn't do that as I don't have a remote way to power on Hubitat if the power failure didn't last long enough to shut down the UPS, and am just going on hope that Hubitat won't be corrupted by a sudden power loss, which I figure is a less likely event than the power failure not lasting long enough to kill the UPS (it's very lightly loaded when I'm gone, so it lasts a long time).

If this is gibberish to you, welcome to the club!

1 Like

I took a very similar approach, although I tend to use my own custom apps over Rule Machine. I don’t know if it’ll be useful for anyone, but I’ve uploaded all of the NUT UPS scripts and the Hubitat app to GitHub here:

2 Likes