Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

reading battery status

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
morgon
Posts: 189
Joined: 2010-08-28 03:04

reading battery status

#1 Post by morgon »

Hi,

I would like to understand how the various ways to read the battery status fit together.

I can do this
> acpi -bi
Battery 0: Discharging, 73%, 01:13:10 remaining
Battery 0: design capacity 5320 mAh, last full capacity 1393 mAh = 26%
this is one way to read the battery status.
Or I can look at the files below /sys/class/power_supply/BAT1:
> find /sys/class/power_supply/BAT1/ -type f -exec echo {} \; -exec cat {} \;
/sys/class/power_supply/BAT1/uevent
POWER_SUPPLY_NAME=BAT1
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_CYCLE_COUNT=0
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
POWER_SUPPLY_VOLTAGE_NOW=11642000
POWER_SUPPLY_POWER_NOW=9453000
POWER_SUPPLY_ENERGY_FULL_DESIGN=62160000
POWER_SUPPLY_ENERGY_FULL=16280000
POWER_SUPPLY_ENERGY_NOW=11340000
POWER_SUPPLY_CAPACITY=69
POWER_SUPPLY_CAPACITY_LEVEL=Normal
POWER_SUPPLY_MODEL_NAME=42T4957
POWER_SUPPLY_MANUFACTURER=SANYO
POWER_SUPPLY_SERIAL_NUMBER= 1170
/sys/class/power_supply/BAT1/serial_number
1170
/sys/class/power_supply/BAT1/technology
Li-ion
/sys/class/power_supply/BAT1/power_now
9453000
/sys/class/power_supply/BAT1/present
1
/sys/class/power_supply/BAT1/power/runtime_active_time
0
/sys/class/power_supply/BAT1/power/runtime_active_kids
0
/sys/class/power_supply/BAT1/power/runtime_usage
0
/sys/class/power_supply/BAT1/power/runtime_status
unsupported
/sys/class/power_supply/BAT1/power/autosuspend_delay_ms
/sys/class/power_supply/BAT1/power/async
disabled
/sys/class/power_supply/BAT1/power/runtime_suspended_time
0
/sys/class/power_supply/BAT1/power/runtime_enabled
disabled
/sys/class/power_supply/BAT1/power/control
auto
/sys/class/power_supply/BAT1/manufacturer
SANYO
/sys/class/power_supply/BAT1/energy_now
11340000
/sys/class/power_supply/BAT1/type
Battery
/sys/class/power_supply/BAT1/capacity
69
/sys/class/power_supply/BAT1/cycle_count
0
/sys/class/power_supply/BAT1/voltage_now
11642000
/sys/class/power_supply/BAT1/status
Discharging
/sys/class/power_supply/BAT1/alarm
3108000
/sys/class/power_supply/BAT1/model_name
42T4957
/sys/class/power_supply/BAT1/voltage_min_design
11100000
/sys/class/power_supply/BAT1/capacity_level
Normal
/sys/class/power_supply/BAT1/energy_full_design
62160000
/sys/class/power_supply/BAT1/energy_full
16280000
If I divide the content of "energy_now" and "energy_full" I get the percentage that the acpi call displays and if I divide the content of "energy_full" by the content of "energy_full_design" I get the "last full percentage" value.

That makes sense and I assume they are the same thing.

But how does acpi derive "design capacity" of 5383 mAh from the value 62160000 in "energy_full_design".

And if I configure UPower to hibernate the laptop at 7% - do these "7%" refer to the "energy_now/energy_full" ratio or has UPower yet another notion of these percentages?

Many thanks!

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: reading battery status

#2 Post by Head_on_a_Stick »

morgon wrote:But how does acpi derive "design capacity" of 5383 mAh from the value 62160000 in "energy_full_design".
Check acpi.c in the source code, the value in /sys for energy_full_design is in mWh and acpi divides this by 1000 then uses the voltage value (which is in mV and is also divided by 1000) to derive the mAh figure.

I've checked your posted figures using https://milliamps-watts.appspot.com/ and they all shake out as expected.
morgon wrote:And if I configure UPower to hibernate the laptop at 7% - do these "7%" refer to the "energy_now/energy_full" ratio or has UPower yet another notion of these percentages?
Why does the derivation matter? 7% capacity will always be 7%, no matter how it is derived.

Please don't make me look in upower's source code as well... :mrgreen:

Oh, and please use code tags rather than quote tags for terminal output, it greatly aids readability.
deadbang

morgon
Posts: 189
Joined: 2010-08-28 03:04

Re: reading battery status

#3 Post by morgon »

The thing is that my laptop does not emit any acpi or udev events for the battery discharge, so if I want to generate a notification that UPower is about to hibernate it soon
I can see no other way as to have a cron-job that checks the battery levels periodically.

I have had bad experiences with xfce4-power-manager that I've configured to warn at 10% but in fact warned at an acpi-level of 20% (I use i3 btw).

All I want is a way to predict reliably when my laptop is about to put to sleep from UPower...

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: reading battery status

#4 Post by bw123 »

yeah laptop-mode-tools has a timer to poll in cases like that where the batt doesn't send events. Maybe check that out... you could hack your own if you want to use the script as a sample.

Code: Select all

/etc/systemd/system/multi-us~rget.wants/laptop-mode.timer        235/235               100%
[Unit]
Description=Runs Laptop Mode Tools - Polling Service, every 150 seconds


[Timer]
# Let's run every 150 secs, to poll the battery
OnUnitActiveSec=150s
OnActiveSec=150s
Unit=lmt-poll.service

[Install]
WantedBy=multi-user.target

timer is better than cron nowadays from what I have read. cron tends to be unreliable on a machine that isn't always up, and can't guarantee when it will run. I am not sure you could run it every minute or so anyway?

p.s. did you use acpi_listen and make sure the batt isn;t reporting?
p.p.s why would you need upower on I3 anyway? I never use that crud with a wm.

also, there's a cool little wmacpi dock app that's what I've been using (with aosd_cat for notification ) on fluxbox for awhile.
resigned by AI ChatGPT

morgon
Posts: 189
Joined: 2010-08-28 03:04

Re: reading battery status

#5 Post by morgon »

bw123 wrote: p.s. did you use acpi_listen and make sure the batt isn;t reporting?
I did.
bw123 wrote:
p.p.s why would you need upower on I3 anyway? I never use that crud with a wm.
I need something to hibernate my laptop on low battery. I've tried xfce4-power-manager but that did not do it for me.
It works well, the one piece missing for me is a notification - I'll do that myself.

What would be an alternative the UPower? Doing everything with a home-made script? Certainly possible, but why not use the tested tools?

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: reading battery status

#6 Post by bw123 »

morgon wrote:
bw123 wrote:
p.p.s why would you need upower on I3 anyway? I never use that crud with a wm.
...

What would be an alternative the UPower? Doing everything with a home-made script? Certainly possible, but why not use the tested tools?
Well my first thought would be why to use freedesktop piecemeal like that? There are already several desktop environments that combine these tools in a way that (mostly) works. There was life before the big integrated powermanager idea invaded from the other os, some of the old tools are really easy and reliable.

I'd still recommend either laptop-mode-tools with it's timer, or wmacpi with your own. The bad thing about laptop-mode-tools for hibernate is that it runs outside of X so notification is a hassle. wmacpi is a little X prog and can run aosd_cat for you real easy.

http://manpages.org/wmacpi

p.s. according to this upower should be polling the batt anyway?

Code: Select all

/etc/UPower/UPower.conf                                                1528/2898               52%

# Don't poll the kernel for battery level changes.
#
# Some hardware will send us battery level changes through
# events, rather than us having to poll for it. This option
# allows disabling polling for hardware that sends out events.
#
# default=false
NoPollBatteries=false
resigned by AI ChatGPT

morgon
Posts: 189
Joined: 2010-08-28 03:04

Re: reading battery status

#7 Post by morgon »

bw123 wrote: p.s. according to this upower should be polling the batt anyway?
I assume it polls it, it could even be that I could get notifications from UPower via DBus, but then it is almost trivial to have a script that runs every few minutes, calculates the power status and notifies me via dunst if the level is close to where UPower will hibernate.

But then (philosophically I am with you not using things like UPower) I might just as well do the hibernation myself and get rid of UPower altogether...

But my main motivation for this thread was to understand how the diffent was to read the battery levels fit together and I think that has been clarified.

Post Reply