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.
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.
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%.
#!/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 %.
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.
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âŚ
/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:
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.)
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