# Auto shut-off during a power outage?

## don quixada

There was a power outage at home while I was at work today. My computer at home is on a UPS, but it eventually ran out of batteries and the computer shutoff uncleanly. Is there a way to get the shutdown to be tied to the battery power/ time-left so it shuts-down before the battery runs-out? Thanks.

Joe

----------

## eccerr0r

This depends on the UPS, do you have a smart UPS with a serial or USB port to communicate battery status back to the computer?

The next problem is that UPSes tend to have different protocols to communicate data back to the PC.  A project for APC-branded UPSes is documented at http://www.apcupsd.com/ ... other brands may vary.

If you have a 'dumb' ups...well, if you're good at electronics you could make your own power sense...

----------

## Akkara

Small DC 24V power supply plugged into a non-ups outlet, with a large capacitor connected via current-limiting resistor and diode, holds a low-current normally-closed relay open.

When power fails, capacitor discharges, closing the contacts of the relay.  The contacts of this relay are connected to the soft-power-down switch on the front panel through another time-delay capacitor to make it a momentary press.  Machine shuts down cleanly.

----------

## Akkara

Follow-up: Took a moment to draw up a circuit I've used.  Image and source included at the end of this post.

Use

This is meant to be powered off a DC supply rated between 1.5 and 2x the voltage of the relay you'll be using for switching.  The power supply is plugged into a normal, non-UPS outlet.  (Make sure the power-supply's voltage drops to near 0 when power is removed; use an appropriate load resistor if it remains powered too long.)

When power fails, the circuit waits out a suitable time-delay (determined mainly by C2 and R1) then the output relay switches 'on' momentarily.  The switch contacts may be used to initiate power-down by connecting them in parallel to the front-panel power button.  If power is restored before the circuit "fires", it resets without activating the relay.

How it works

Capacitor C1 serves two roles: It powers the circuit after main power fails and also determines how long the relay pulses ON after the power-fail timer has expired.  Basically, after the timer expires, the charge in C1 is dumped into the relay coil, turning it on.  Once discharged, the relay releases.

Transistors T1 and T2 drive the relay.  They are normally held off by transistor T3.  When power fails, base current for T3, which is normally delivered by diode D2, now has to flow through timing capacitor C2.  As current flows through C2, the voltage at node Vtiming drops.  Eventually this voltage is no longer enough to keep T3 turned on, and it turns off.  T3 turning off allows the current flowing through R5 to now flow into T2 and T1, turning them, and the relay, on.  The relay turning on causes capacitor C1 to discharge faster, which in turn causes Vtiming to also drop further, further reinforcing the turn-on ensuring it is crisp and noise-free.

Circuit

As an image (copy-pase this into a shell): *Quote:*   

> echo 'iVBORw0KGgoAAAANSUhEUgAAA4sAAAGRBAMAAAA+7zuEAAAAAXNSR0IArs4c6QAAABJQTFRFAAAAAACAgACA/wAAgICA//rhSgWrWgAAAAlwSFlzAAAPOgAADxIBymR1aA
> 
> AAAAd0SU1FB9gFAwszDvdqs6oAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAARy0lEQVR42u2dX5asKBKHnQfeL32OC+g6J1dwNzAPt9/RHva/lUnF
> 
> P6iAKAEC/mKqc7yVmWj5GRAEQUQjIeWLaHAPgBECjBBghAAjMEKAEQKMEGB8A0ax/hNHZR5BG9GpQoARAowQYARGCDBCgBECjMAIAUYIMEKAERghwAgBRkhmGJuGSclwfw
> ...

 

As a Qucs schematic.  Creates a file named "pfs.sch" (copy-pase this into a shell): *Quote:*   

> echo ' 
> 
> QlpoOTFBWSZTWSUfBnYACQ7fgBAQWBf/9z/X/4C/79/gUAW+OCFOCaCUV1ACUQTUyntTJPTFNGxQ
> 
> NNBoNAAAaDTQEU1AAAAAAAAAAJNSKaYmmmqMmoNMRjQGk9RkaepgRgc0xMBGmBGEYAAAAmEYCJIR
> ...

 

(PS: did this method of including non-text content in a post work out OK for you?)

Edit: adjusted the shellcode around the schematics to work with more browsers; thanks Mad Merlin for pointing this out.Last edited by Akkara on Sun May 04, 2008 6:58 am; edited 1 time in total

----------

## don quixada

I have and APC Back-UPS ES 650. 

Wow, that's circuit board solution is impressive! I'm not too confident with my soldering skills though...

dq

----------

## albright

 *Quote:*   

> PS: did this method of including non-text content in a post work out OK for you?

 

You get used to it. I don't even see the code. All I see is Blonde, Brunette, Redhead . . .

----------

## nOw2

Best to use sys-power/nut with APC UPS'. You'll need a special APC cable (it's not serial, but very close), available off eBay for very little.

The big advantage to Network UPS Tools is the network bit - it works fine on one machine, but if you have more on the one UPS it's easy to tie them in to shutdown on low battery too.

----------

## Mad Merlin

 *Akkara wrote:*   

> (PS: did this method of including non-text content in a post work out OK for you?)

 

I tried the first one, it worked, but there was a leading and trailing space I had to remove from each line first. The following works around that, provided you manually remove the leading space from the last line:

```
cat <<ENDEND | perl -pe 's/^ *([^ ]+) *$/$1/;' | base64 -d | xview stdin
```

You could probably pipe the heredoc directly into perl too, to make it slightly shorter:

```
perl -pe 's/^ *([^ ]+) *$/$1/;' <<ENDEND | base64 -d | xview stdin
```

----------

## Akkara

 *Quote:*   

> it worked, but there was a leading and trailing space I had to remove from each line first

 

Interesting!  When I try it here, it works just copy-pasting into a xterm and pressing 'return'.  There is no whitespace in front.

I'm using firefox 2.0.0.14, xorg-server-1.3.0.0-r5, xorg-x11-7.2, 

How are you doing it?

Edit: I just tried it in konqueror and I get spaces like you mention.  Thanks for pointing out a problem! I'll make it robust against that in the future.

----------

## don quixada

I don't have xview-- I don't think it is available for 64-bit systems like mine. Is there an alternate program?

Also, the qucs schematic is for 0.0.14 and the stable version is only 0.0.12.

dq

----------

## Mad Merlin

Yes, I'm using Konqueror, I didn't try Firefox.

don quixada: I'm on a 64-bit system, and xview is a symlink to xloadimage for me (which is in media-gfx/xloadimage). Now, why I have media-gfx/xloadimage is another matter, as nothing depends on it, and it's not in my world file...

----------

