Speaking on the power devices builtin statistics reporting capabilities level, not all devices support providing statistics ... Use the upower
command of UPower to list power devices with the option -e
like so(this is a demonstration on a test system):
$ upower -e
/org/freedesktop/UPower/devices/line_power_ADP0
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/DisplayDevice
Then check the information of the device you want with the option -i
... Names are pretty descriptive, so the direct power line:
$ upower -i /org/freedesktop/UPower/devices/line_power_ADP0
native-path: ADP0
power supply: yes
updated: Fri 11 Aug 2023 03:45:32 PM +03 (68 seconds ago)
has history: no
has statistics: no
line-power
warning-level: none
online: yes
icon-name: 'ac-adapter-symbolic'
and as you can see, it doesn't have statistics ... So, information about current power consumption (among other information) can't be pulled from that device.
While the battery:
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0
native-path: BAT0
vendor: LGC
model: L16L2PB2
serial: 5545
power supply: yes
updated: Fri 11 Aug 2023 03:47:33 PM +03 (2 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: charging
warning-level: none
energy: 5.06 Wh
energy-empty: 0 Wh
energy-full: 17.99 Wh
energy-full-design: 30 Wh
energy-rate: 9.238 W
voltage: 7.744 V
charge-cycles: N/A
time to full: 1.4 hours
percentage: 28%
capacity: 59.9667%
technology: lithium-polymer
icon-name: 'battery-low-charging-symbolic'
History (charge):
1691758053 28.000 charging
History (rate):
1691758053 9.238 charging
does have statistics and among those is energy-rate:
which you can isolate and format for printing with something like this:
$ upower --show-info /org/freedesktop/UPower/devices/battery_BAT0 |
awk '/energy-rate:/{print $2}'
10.828 # <--- output
However, the statistics update interval might vary and updated:
shows the time the statistics were last updated, but you can force statistics refresh of a certain device with for example:
busctl call org.freedesktop.UPower \
/org/freedesktop/UPower/devices/battery_BAT0 \
org.freedesktop.UPower.Device Refresh
and you can pull for e.g. energy-rate
every for example three seconds with something like this:
while sleep 3
do
# Refresh device statistics
busctl call org.freedesktop.UPower \
/org/freedesktop/UPower/devices/battery_BAT0 \
org.freedesktop.UPower.Device Refresh
# Query the device statistics
upower -i /org/freedesktop/UPower/devices/battery_BAT0 |
awk '/energy-rate:/{print $2}'
done