You are not logged in.

#1 2012-01-05 16:49:36

beatle
Member
Registered: 2011-08-09
Posts: 16

[SOLVED] Low battery warning form acpi

Hello!

I hope you could help me out, as I am stuck.

I am trying to automate pm-utils stuff on my laptop, that I have until now been doing by hand with CLI. I installed acpid and I really like how it works as it is very easy and clean to add event handling for closing laptop lid etc.

But I was unable to use /etc/acpi/handler.sh to detect low battery warning. Only event that is received under "battery" is when the AC adapter is plugged in or battery is removed/inserted, but not when battery is low. 

I tried to find more info on this, but discovered only that it is supposedly possible to configure alarm level in /proc/acpi/battery/BAT1/alarm, but that file seams to be depreciated and it's replacement (?) /sys/class/power_supply/BAT1/alarm contains a number resulting in about 3% charge, that should have been sufficient for at least writing to log file if any event was received from the battery, but nothing was. (And anyway, I don't know how to configure it using this file, as I am not allowed to write to it and it gets overwriten by something.)

So I could really use some pointers on why the acpi event for low battery doesn't get generated (or why I am missing it) or what alternatives I have.

I really would not like to run a cron job for checking battery level, as the acpi event method seams much more beautiful to me and would allow me to sleep better.

Thanks a lot!

Last edited by beatle (2012-01-06 01:20:29)

Offline

#2 2012-01-05 16:58:57

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: [SOLVED] Low battery warning form acpi

I nice thing to have installed when having a laptop system is laptop-mode-tools. You can enable it when on both AC and BAT, or just BAT. latop-mode-tools uses /etc/acpi/events/lm_battery and /etc/acpi/actions/lm_battery.sh to start/stop on low battery.

Last edited by hesse (2012-01-05 17:01:53)

Offline

#3 2012-01-05 17:54:27

beatle
Member
Registered: 2011-08-09
Posts: 16

Re: [SOLVED] Low battery warning form acpi

I will check it out. Thanks!

Offline

#4 2012-01-05 21:10:02

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED] Low battery warning form acpi

You might find the scripts useful that I put on page 67 of this thread. It does rely on the command,

acpi -b

giving meaningful output.

Offline

#5 2012-01-06 01:19:53

beatle
Member
Registered: 2011-08-09
Posts: 16

Re: [SOLVED] Low battery warning form acpi

/dev/zero wrote:

You might find the scripts useful that I put on page 67 of this thread. It does rely on the command,

acpi -b

giving meaningful output.


This is great! Marking as solved, as this solution will let me sleep.

Thanks hesse and dev/zero for your help!

Offline

#6 2012-01-06 02:04:11

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED] Low battery warning form acpi

No worries :-) If you can think of any ways to improve the script, I'd be interested to know. It isn't perfect because output from "acpi -b" is unpredictable.

Offline

#7 2012-01-06 15:55:55

beatle
Member
Registered: 2011-08-09
Posts: 16

Re: [SOLVED] Low battery warning form acpi

/dev/zero wrote:

No worries :-) If you can think of any ways to improve the script, I'd be interested to know. It isn't perfect because output from "acpi -b" is unpredictable.

I did adapt your script a bit to suite my needs (Thanks for showing how to write deamon scripts! I haven't thought that it is so straightforward and beauty full.) , but I wouldn't say I improved it as I am basically illiterate in bash.

For now I deal with the output of "acbi -b" by searching for the "NN%" (with grep, as I know not of a better way) - remaining percent of battery charge - in it, as I suspect this sub string  would allays appear in "acbi -b" output even when the 'remaining time' part is unpredictable (haven't checked it though!).

Also, my laptop is usually on mute so I changed the text-to-speech warning to a i3-nagbar popping up. (And along the way learned how to control spawned processes in bash! I love just GNU/Linux!).

/usr/sbin/battery_monitor_loop

#! /bin/bash

SLEEP_TIME=5   # Default time between checks.
SAFE_PERCENT=30  # Still safe at this level.
DANGER_PERCENT=15  # Warn when battery at this level.
CRITICAL_PERCENT=5  # Hibernate when battery at this level.

NAGBAR_PID=0
export DISPLAY=:0.0

function launchNagBar
{
    i3-nagbar -m 'Battery low!' -b 'Hibernate!' 'pm-hibernate' >/dev/null 2>&1 &
    NAGBAR_PID=$!
}

function killNagBar
{
    if [[ $NAGBAR_PID -ne 0 ]]; then
        ps -p $NAGBAR_PID | grep "i3-nagbar"
        if [[ $? -eq 0 ]]; then
            kill $NAGBAR_PID
        fi
        NAGBAR_PID=0
    fi
}


while [ true ]; do

    killNagBar

    if [[ -n $(acpi -b | grep -i discharging) ]]; then
        rem_bat=$(acpi -b | grep -Eo "[0-9]+%" | grep -Eo "[0-9]+")

        if [[ $rem_bat -gt $SAFE_PERCENT ]]; then
            SLEEP_TIME=10
        else
            SLEEP_TIME=5
            if [[ $rem_bat -le $DANGER_PERCENT ]]; then
                SLEEP_TIME=2
                launchNagBar
            fi
            if [[ $rem_bat -le $CRITICAL_PERCENT ]]; then
                SLEEP_TIME=1
                pm-hibernate
            fi
        fi
    else
        SLEEP_TIME=10
    fi

    sleep ${SLEEP_TIME}m

done

Hm, and just now I got an idea, that, in order to avoid the unpredictable "acpi -b" I could use the acpi information form /sys/class/power_supply/BAT1/* directly as I already have a deamon running. Will try that out.

Last edited by beatle (2012-01-06 18:17:41)

Offline

#8 2012-01-06 22:06:43

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED] Low battery warning form acpi

Nice :-)

I like your idea of sourcing information from

/sys/class/power_supply/BAT?/

I got my stfw on and found Battery information in sysfs for userspace tools. I have:

$ find /sys/class/power_supply/BAT0/ -type f -regex ".*\_now" -exec basename {} \;
voltage_now
current_now
charge_now

So, using the formula charge_equals_current_by_time I will test whether it works to replace

rem_time=$(acpi -b | awk '{print $(NF-1)}')
hrs=$(date --date="$rem_time" +%H)
mins=$(date --date="$rem_time" +%M)
rem_mins=$(echo "$hrs*60 + $mins - $BUFFER_TIME" | bc)

with something like

BAT_INFO_DIR=/sys/class/power_supply/BAT0
CHARGE_NOW=$(cat $BAT_INFO_DIR/charge_now)
CURR_NOW=$(cat $BAT_INFO_DIR/current_now)
rem_mins=$(echo "60*$CHARGE_NOW/$CURRENT_NOW" | bc)

Offline

Board footer

Powered by FluxBB