You are not logged in.
Pages: 1
I am trying to figure out how to get info on battery drain. I found that /sys/class/power_supply/BAT0/capacity gives battery capacity in percentages, but where can i look for energy losses? I want to get something like n [%/h] info. Also for whatever reason /sys/class/power_supply/BAT0/status gives "unknown" when laptop is plugged and capacity does not go up.
(I am using old x220 lenovo).
Offline
Power management and monitoring is handled by UPower (extra/upower). You may communicate with it using D-Bus. EnergyRate is the property you want.
Alternatively, if you want to do that the hard and not very reliable way, you may read whatever is available in “/sys/class/power_supply/$DEVICE/uevent” and either wait for it to be updated automatically (if that works) or trigger udev — by calling udevadm trigger or writing literal string “change” to that file — and either re-read the file or setup a udev rule to react to the CHANGE event.
--- edit
Here is an example, showing accessing the information you asked for. The output is JSON that can be fed to other tools.
#!/usr/bin/env python
# SPDX-License-Identifier: CC0-1.0
# Copyright © 2021 mpan; <https://mpan.pl/>; CC0 1.0 (THIS SCRIPT!)
# Context: <https://bbs.archlinux.org/viewtopic.php?id=269453>
import dbus
from xml.dom.minidom import parseString
import json
bus = dbus.SystemBus()
upower = bus.get_object('org.freedesktop.UPower', '/org/freedesktop/UPower')
devPaths = dbus.Interface(upower, 'org.freedesktop.UPower').EnumerateDevices()
result = {}
for devPath in devPaths:
dev = bus.get_object('org.freedesktop.UPower', devPath)
devProps = dbus.Interface(dev, 'org.freedesktop.DBus.Properties') \
.GetAll('org.freedesktop.UPower.Device')
if 2 == devProps['Type']:
result[devProps['NativePath']] = {
'percentage': devProps['Percentage'],
'rate': devProps['EnergyRate'],
'hoursLeft': devProps['TimeToEmpty'] / 3600
}
print(json.dumps(result))Requires extra/dbus-python and extra/upower.
Last edited by mpan (2021-09-07 14:14:37)
Offline
Thank you for answering. Well neigher /sys/class/power_supply/BAT0/uevent nor /sys/class/power_supply/AC/uevent give me the info on power drain (current energy losses). Upower seems to have answer, but another issue is that 'cat /sys/class/power_supply/BAT0/uevent' says POWER_SUPPLY_CAPACITY=98, even though laptop remained plugged all night. Is it because the battery is old? I doubt that upower solves it (it just parse those files isn't it?).
Offline
The output of upower -d:
Device: /org/freedesktop/UPower/devices/line_power_AC
native-path: AC
power supply: yes
updated: Thu 09 Sep 2021 01:36:24 PM MSK (297 seconds ago)
has history: no
has statistics: no
line-power
warning-level: none
online: yes
icon-name: 'ac-adapter-symbolic'
Device: /org/freedesktop/UPower/devices/battery_BAT0
native-path: BAT0
vendor: LGC
model: 45N1025
serial: 19839
power supply: yes
updated: Thu 09 Sep 2021 01:40:25 PM MSK (56 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: fully-charged
warning-level: none
energy: 36.2637 Wh
energy-empty: 0 Wh
energy-full: 36.9186 Wh
energy-full-design: 62.16 Wh
energy-rate: 0 W
voltage: 12.532 V
percentage: 98%
capacity: 59.3929%
technology: lithium-ion
icon-name: 'battery-full-charged-symbolic'
Device: /org/freedesktop/UPower/devices/DisplayDevice
power supply: yes
updated: Thu 09 Sep 2021 01:36:24 PM MSK (297 seconds ago)
has history: no
has statistics: no
battery
present: yes
state: fully-charged
warning-level: none
energy: 36.2637 Wh
energy-full: 36.9186 Wh
energy-rate: 0 W
percentage: 98%
icon-name: 'battery-full-charged-symbolic'
Daemon:
daemon-version: 0.99.13
on-battery: no
lid-is-closed: no
lid-is-present: yes
critical-action: HybridSleepSo it says 98% and it worries me. Also why not just use this output why is it more 'reliable way' to use extra/dbus-python ? Energy rate is 0 because the laptop is plugged and it does not drain power from battery. But it would be nice to know the energy losses for the whole machine (even when it is plugged)
Offline
The number that should worry you is
capacity: 59.3929%Well neither /sys/class/power_supply/BAT0/uevent nor /sys/class/power_supply/AC/uevent give me the info on power drain (current energy losses).
You want to poll /sys/class/power_supply/BAT0/uevent and inspect the difference of POWER_SUPPLY_CHARGE_NOW per time or use POWER_SUPPLY_CURRENT_NOW as a metric.
Offline
The number that should worry you is
capacity: 59.3929%
Can you explain? I thought /sys/class/power_supply/BAT0/capacity gives the capacity in percentage. And it says 98 (used to be 100 but when i unplugged for a minute of so it went to 98). According to https://upower.freedesktop.org/docs/Dev … e:Capacity it means that battery is 40% dead (which does not worry me because I have a replacement
)
Last edited by madil86639 (2021-09-09 15:45:32)
Offline
energy-full: 36.9186 Wh
energy-full-design: 62.16 WhWhen the battery left the fab is was built to hold ~62Wh
Now it can only hold ~37Wh (60%) so it has lost more than a third of its original capacity since then to age.
Offline
Thank you for explaining this. But why is it
not very reliable way
to read files from /sys/class/power_supply/ ? And is there a way to get actual energy losses even when laptop is plugged in? Because "EnergyRate" in upower gives the amount of energy being drained from the battery, but what I want to know (if that is possible using laptop alone) is the amount of energy being drained by the laptop right now.
Offline
I assume he's talking about blindly assuming the device to be available in that position.
powertop will tell you about the energy demand of individual hardware components (but that's not a good way if you want to know what's being drained from the battery)
Offline
I'm not sure why this hasn't been mentioned - but you can just watch the /sys/class/<whatever>/energy_now "file" in a loop.
EDIT: oops, nevermind - I missed that you want a measure of the energy used while plugged in. Frankly I'd just invest a couple dollars on a power monitor you'd put on the outlet. But this is very different from the question in the title - the battery will not be draining at all when plugged in.
Last edited by Trilby (2021-09-09 16:38:57)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
energy: 36.2637 Wh
energy-empty: 0 Wh
energy-full: 36.9186 Wh
energy-full-design: 62.16 Wh
energy-rate: 0 W
voltage: 12.532 V
percentage: 98%Another issue with the upower -d output is that it says energy is slitly smaller than energy-full with laptop being plugged. Is it possible that battery capacity reduced over this period and it does not recalculate energy-full yet? How can I obtain the actual energy-full.
Offline
How precise measurement are you expecting? The difference between values is already under 2%, which is a very good result.
Offline
Yes but over the time actual energy capacity (what is now 36.2637, because laptop is plugged) will decrese even further. How can I recalculate "energy-full" that gives upower(it shows 36.9186)? Or will it happen automatically when the difference become bigger?
Offline
Offline
Pages: 1