martinthorne
Posts: 12
Joined: Tue Mar 03, 2015 10:13 am

Rpi detect power failure

Sun May 16, 2021 7:35 am

Hi all,

I've just been descussing a project on another forum and an idea has come up.

Has anybody had experience of talking to a UPS to establish when a power cut occurs?

I'm mostly interested in being able to run a script that calls a webhook when power is restored.

JumpZero
Posts: 1237
Joined: Thu Mar 28, 2013 7:35 pm
Location: Arcachon, France

Re: Rpi detect power failure

Sun May 16, 2021 7:43 am

martinthorne wrote:
Sun May 16, 2021 7:35 am
Has anybody had experience of talking to a UPS to establish when a power cut occurs?
Of course you can do that, there is a wonderful piece of software: NUT (Network Ups Tool)
https://networkupstools.org/
NUT runs on Linux and on the Raspberry Pi, you can install with apt

martinthorne
Posts: 12
Joined: Tue Mar 03, 2015 10:13 am

Re: Rpi detect power failure

Sun May 16, 2021 8:03 am

Thanks @JumpZero for your quick reply

Can I just confirm, is it just on a setting somewhere to call a webhook on power failure/power restore, or do you somehow tell to to run the script XXX on the pi (which in ten would call the webhook)?

Do you know how quickly the webhook is likely to be triggered? I.e. upto one minute from event/immediately etc.


deepo
Posts: 808
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Rpi detect power failure

Sun May 16, 2021 9:43 am

Routers also need time to start up after a power failure.
So my guess is that you need to write some code that reacts on the messages from the UPS in order to call the web hook you mention, until a connection to the internet can be made.

martinthorne
Posts: 12
Joined: Tue Mar 03, 2015 10:13 am

Re: Rpi detect power failure

Sun May 16, 2021 10:59 am

deepo wrote:
Sun May 16, 2021 9:43 am
Routers also need time to start up after a power failure.
So my guess is that you need to write some code that reacts on the messages from the UPS in order to call the web hook you mention, until a connection to the internet can be made.
The plan is to have the ups power the modem, router, RPi and another device. So the internet will stay on.

The webhook that needs to be called needs to also include authentication headers - can NUT do that?

JumpZero
Posts: 1237
Joined: Thu Mar 28, 2013 7:35 pm
Location: Arcachon, France

Re: Rpi detect power failure

Sun May 16, 2021 3:23 pm

martinthorne wrote:
Sun May 16, 2021 10:59 am
can NUT do that?
NUT can run your own script on an event. You can read this here: https://networkupstools.org/docs/user-m ... 01s07.html
It works very well
@RonR excellent scripts! I keep in my notes.

deepo
Posts: 808
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Rpi detect power failure

Sun May 16, 2021 8:03 pm

curl is probably capable of calling your web hook.
https://curl.se/

So find out how to call your web hook and put that into a script.

martinthorne
Posts: 12
Joined: Tue Mar 03, 2015 10:13 am

Re: Rpi detect power failure

Sun May 16, 2021 8:16 pm

deepo wrote:
Sun May 16, 2021 8:03 pm
curl is probably capable of calling your web hook.
https://curl.se/

So find out how to call your web hook and put that into a script.
Hi all, thanks for your replies

Is there any limitation on the scripting language?

Anybody know of a step-by-step on setting this up?

Thanks

RonR
Posts: 2187
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Rpi detect power failure

Sun May 16, 2021 8:54 pm

The install-nut script in nut.zip sets up a shell script (/etc/nut/notify-cmd.sh) to send an email on all notifications. It should be easy to add a test for a NOTIFYTYPE of ONLINE to detect power restored and call your webhook.

/etc/nut/notify-cmd.sh:

Code: Select all

#!/bin/sh

echo "$*" | mail -s "Message from PBX (UPS Monitor)" root

### Added webhook code ###
if [ "${NOTIFYTYPE}" = "ONLINE" ]; then
  # Call webhook here #
fi

deepo
Posts: 808
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Rpi detect power failure

Mon May 17, 2021 1:05 pm

martinthorne wrote:
Sun May 16, 2021 8:16 pm
deepo wrote:
Sun May 16, 2021 8:03 pm
curl is probably capable of calling your web hook.
https://curl.se/

So find out how to call your web hook and put that into a script.
Hi all, thanks for your replies

Is there any limitation on the scripting language?

Anybody know of a step-by-step on setting this up?

Thanks
Many use bash, as that's installed by default.
If you install python, then you can write the scripts in python.
But I don't know what the UPS software allows for scripts.

Return to “General discussion”