Total capacity on laptops with multiple batteries | David Grajal

Total capacity on laptops with multiple batteries

My thinkpad 470s has two batteries and all linux commands to return battery capacity return capacity for each battery. I needed to update my awesomewm scripts to return battery capacity across all available batteries. This was a good exercise of string manipulation with shell. I came up with two versions:

1
paste -d'+' /sys/class/power_supply/BAT*/capacity | sed 's/$/)\/2/g' | sed 's/^/(/g' | bc
1
/usr/bin/acpi -b | cut -d' ' -f4 | sed 's/%.*/\/2/g' | paste -s -d+ - | bc

And then I looked up at stack overflow and found a better solution:

1
paste /sys/class/power_supply/BAT*/capacity | jq -s add/length

The new awesomewm battery widget:

widgets.batteries = awful.widget.watch("bash -c 'paste /sys/class/power_supply/BAT*/capacity | /home/dgrabla/bin/jq -s add/length '", 30, function(widget, stdout, stderr, exitreason, exitcode)
if exitcode == 0 then
widget:set_text("🔋 " .. string.gsub(stdout, "\n", "") .. " %")
return
end
widget:set_text("BROKEN WIDGET" .. stdout)
end)