Posts: 4,164
rokytnji
Joined: 20 Feb 2009
#16
My Battery line in conky for my IBM A22M

Code: Select all

Batt:${color e0e0e0} .$acpiacadapter, ${color e0e0e0} ${battery_percent BAT0}% ${color e0e0e0} ${execi 300 /home/harry/.battery.sh} | ${color}BATTERY:${color e0e0e0} ${battery_bar 7,50 BAT0} ${execi 300 /home/harry/.battery.sh}
My .battery.sh which is a hidden file I made in /home/harry and made sure it was made executable

Be sure to change /home/harry with /home/your user name/.battery.sh

Code: Select all

#!/usr/bin/env python

import os, math
from decimal import *

rate = 0.0
batteries = os.listdir("/sys/class/power_supply")
output ="No Data"

for battery in batteries:
    if"BAT" in battery:
        status = open("/sys/class/power_supply/%s/status" % (battery)).read().strip("\n")
        if"Discharging" in status:
            energy = Decimal(open("/sys/class/power_supply/%s/energy_now" % (battery)).read())
            power = Decimal(open("/sys/class/power_supply/%s/power_now" % (battery)).read())
            if power > 0:
                hours = math.floor(energy / power)
                minutes = math.floor(((energy % power) / power) * 60)
                output ="%.0f hours %.0f minutes" % (hours, minutes)
            else:
                output ="Gathering"
        else:
            output = status
print output
screenshot

Image
Posts: 137
duncan_mk
Joined: 19 Sep 2012
#17
Damn! Should have sorted this out months ago.

A short 'awk' script:

Code: Select all

#!/usr/bin/awk -f

# Battery script for conky

BEGIN {
        while (("acpi -b" | getline) > 0) {
                print $0                }
        close ("acpi -b")

        while (("acpi -a" | getline) > 0) {
                print $0                }
        close ("acpi -a")
        }
chmod u+x to make it executable and add the the following in conkyrc_icewm:

Code: Select all

${color}Battery
${Color}${exec battery.awk}
and 'Bob's your Uncle'. You can, of course, use the 'acpi' options to gather all sorts of information - but what charge I've got & whether I'm plugged in or not is all I need to know.

OK, I pinched the idea off Rocky but I'm not clever enough to write stuff in Python.

dmk

PS: and, before anyone picks me up on it, yes, it can be written as:

Code: Select all

BEGIN {
        ("acpi -b" | getline)
        print $0
        close ("acpi -b")

        ("acpi -a" | getline)
        print $0
        close ("acpi -a")
        }
__{{emoticon}}__