Why doesn't `upower -i` work?

The operands -i and --show-info return nothing in the terminal. Anyone know why?

purism@pureos:~$ upower -i
purism@pureos:~$

purism@pureos:~$ upower -i
purism@pureos:~$

But upower -d does work.

purism@pureos:~$ upower -d
Device: /org/freedesktop/UPower/devices/line_power_tps6598x_source_psy_0_003f
  native-path:          tps6598x-source-psy-0-003f
  power supply:         yes
  updated:              Wed 06 Apr 2022 10:21:27 AM PDT (368 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

Device: /org/freedesktop/UPower/devices/battery_max170xx_battery
  native-path:          max170xx_battery
  power supply:         yes
  updated:              Wed 06 Apr 2022 10:26:39 AM PDT (56 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              10.5513 Wh
    energy-empty:        0 Wh
    energy-full:         13.4118 Wh
    energy-full-design:  13.8572 Wh
    energy-rate:         1.03929 W
    voltage:             3.89484 V
    time to empty:       10.2 hours
    percentage:          78%
    temperature:         31.2 degrees C
    capacity:            96.7857%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1649265999	1.039	discharging

Device: /org/freedesktop/UPower/devices/line_power_bq25890_charger
  native-path:          bq25890-charger
  power supply:         yes
  updated:              Wed 06 Apr 2022 10:21:27 AM PDT (368 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

Device: /org/freedesktop/UPower/devices/DisplayDevice
  power supply:         yes
  updated:              Wed 06 Apr 2022 10:26:39 AM PDT (56 seconds ago)
  has history:          no
  has statistics:       no
  battery
    present:             yes
    state:               discharging
    warning-level:       none
    energy:              10.5513 Wh
    energy-full:         13.4118 Wh
    energy-rate:         1.03929 W
    time to empty:       10.2 hours
    percentage:          78%
    icon-name:          'battery-full-symbolic'

Daemon:
  daemon-version:  0.99.11
  on-battery:      yes
  lid-is-closed:   no
  lid-is-present:  no
  critical-action: PowerOff
purism@pureos:~$ 
2 Likes

I tried upower -i on my L5, a workstation and one of my laptop (with and without powersupply connected), same result

The functionnality does probably nothing on its own, maybe it show more info with an other option, or maybe the option is not implemented :smiley:

Looking at the poorly documented man, the answer seems only in the source code :face_with_monocle:

2 Likes

I see that I can run this:

upower -i /org/freedesktop/UPower/devices/battery_BAT0

which returns this:

native-path: (null)
power supply: no
updated: Wed 31 Dec 1969 04:00:00 PM PST (1649280856 seconds ago)
has history: no
has statistics: no
unknown
warning-level: unknown
battery-level: unknown
icon-name: ‘(null)’

But why is the battery-level “unknown?”

EDIT: BTW, upower -i /org/freedesktop/UPower/devices/battery_BAT0 is part of a script I was attempting. No big deal.

probably because the path /org/freedesktop/UPower/devices/battery_BAT0 is unknown

try upower -i /hoyhoy or upower -i /choubidooowaaa you’ll see :wink:

1 Like

Don’t know whether it helps or is relevant but here’s what I am using:

#!/bin/bash

THRESH=${1:-20}

temp=/sys/class/power_supply/max170xx_battery/charge_
chnow=`cat ${temp}now`
chfull=`cat ${temp}full`
# both figures are in uAh

pc=$(( $chnow*100 / $chfull ))
echo $pc%

if [ $pc -ge $THRESH ]; then
    exit
fi

# alarm via sound, sms, email, ...
echo LOW

Note the divide with truncate. If you care for rounding then adjust accordingly. For my purposes, it didn’t matter. I just wanted some kind of alert around about when I should put the Librem 5 on charge.

1 Like
sudo systemctl enable upower
upower --show-info /org/freedesktop/UPower/devices/battery_max170xx_battery

Hmmm… This script doesn’t do anything on my L5. I must be doing something wrong.

I copied the script, changed THRESH=${1:-20} to THRESH=${1:-35}, saved it in my home folder as battery_notify.sh, gave it execute permission, and created this cron job:
*/5 * * * * bash /home/purism/battery_notify.sh

Then service cron restart (Is this required?).
Then bash battery_notify.sh to test; it gave the current battery percentage.

Then I waited, but there was no notification when the charge dropped to 35%.

Did I neglect something?

=====
Here’s the script I originally wanted to implement:

#!/bin/bash

# You need to export this environment variable in order to play sound from cron
export XDG_RUNTIME_DIR="/run/user/1000"

currentBatteryPercentage=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "percentage" | awk '{print $2}')

currentBatteryPercentage=${currentBatteryPercentage/\%/}

if [ $currentBatteryPercentage -lt 22 ]

then

mplayer /home/purism/batalarm.ogg -l 2

fi

But bash batwarn.sh returns:
batwarn.sh: line 10: [: -lt: unary operator expected

And as I said before, upower -i /org/freedesktop/UPower/devices/battery_BAT0 doesn’t return the %.

Think you need <= in front of 22.

1 Like

That returns:
batwarn.sh: line 10: =22: No such file or directory

Reverting to the original -lt 22 to investigate:

purism@pureos:~$ bash -x batwarn.sh
+ export XDG_RUNTIME_DIR=/run/user/1000
+ XDG_RUNTIME_DIR=/run/user/1000
++ upower -i /org/freedesktop/UPower/devices/battery_BAT0
++ awk '{print $2}'
++ grep percentage
+ currentBatteryPercentage=
+ currentBatteryPercentage=
+ [[ '' -lt 22 ]]
+ mplayer /home/purism/batalarm.ogg -l 2
Unknown option on the command line: -l
Error parsing option on the command line: -l
MPlayer 1.4 (Debian), built with gcc-10 (C) 2000-2019 MPlayer Team

EDIT: I had an additional error in the script re mplayer, so I corrected the output above. And I ran the script with double [[ ]] around -lt 22, which is an additional way to troubleshoot. (I think?)

Sorry, I’m not great with bash. The internet says you need quotes around $currentBatteryPercentage, but then that suggests that your variable is being empty.

1 Like

I’m very inexperienced with bash, but I’m trying to understand and learn a few things. The fun of copying random bloggers’ scripts and then having to troubleshoot… :neutral_face:

/battery_BAT0 on Librem 5 equals to /battery_max170xx_battery. Please just give up on using /battery_BAT0, as not related part, not here in this thread, not at all.

All based on this output that comes from your post:

1 Like

You want to use [[ instead of [. So the if line would change to:

if [[ $currentBatteryPercentage -lt 22 ]]

There is a bit more information here if you are okay with StackOverflow: https://stackoverflow.com/a/3427931

1 Like

Do you mean replace this:
currentBatteryPercentage=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "percentage" | awk '{print $2}')

with this:

currentBatteryPercentage=$(upower -i /org/freedesktop/UPower/devices//battery_max170xx_battery | grep "percentage" | awk '{print $2}')

1 Like

Yes, please. I hope this will work for you: /devices/battery_max170xx_battery.

I get:

purism@pureos:~$ bash -x batwarn.sh
+ export XDG_RUNTIME_DIR=/run/user/1000
+ XDG_RUNTIME_DIR=/run/user/1000
++ upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery
++ grep percentage
++ awk '{print $2}'
+ currentBatteryPercentage=23%
+ currentBatteryPercentage=23
+ [[ 23 -lt 20 ]]

P.S. The sound file doesn’t play unless I remove -l 2 completely. [EDIT: change “-l” to “-loop 2” successfully plays the sound twice.]

At least now, the % is found. Thanks.

2 Likes

Thanks for that! It’s helpful.

So, I’m charging the phone now, but I’ll see later if it works as expected. Thanks, everybody.

purism@pureos:~$ bash -x batwarn.sh
+ export XDG_RUNTIME_DIR=/run/user/1000
+ XDG_RUNTIME_DIR=/run/user/1000
++ upower -i /org/freedesktop/UPower/devices/battery_max170xx_battery
++ awk '{print $2}'
++ grep percentage
+ currentBatteryPercentage=34%
+ currentBatteryPercentage=34
+ [[ 34 -lt 20 ]]

EDIT: Success! I made a copy of the script to notify when charging goes above 80%, and it played the alarm file at 81%, as well as 5 minutes later, as instructed. (But it doesn’t turn itself off after unplugging. More research needed, I guess. And I’ll look for how to combine these two scripts into one.)

2 Likes

If you used the /sys/class stuff that I am using then current_now tells you whether it is charging (positive) or discharging (negative) or doing nothing (zero).

That isn’t quite the same as whether it is plugged in. For that maybe

cat /sys/class/power_supply/tps6598x-source-psy-0-003f/uevent

and look at ONLINE or PRESENT (I don’t know what the difference is).

1 Like

No, not required.

1 Like

ONLINE means that power is flowing into the phone. PRESENT means that something is merely plugged in (it could be a USB device or an adapter).

2 Likes