# Command to get details on charge and time remaining for two batteries

I have a laptop with two batteries. I would like to get combined details on the two batteries. In particular, I'd like the time remaining before both batteries run out of charge and the percentage charge left in both batteries. Is there a command to do this?

When I run:

``````acpi -b
``````

I get the following output:

``````Battery 0: Full, 100%
Battery 1: Discharging, 80%, 05:10:03 remaining
``````

So I'd like a command that instead gives me something like:

``````All batteries: Discharging 90%, 10:10:06 remaining
``````

Here's my script. It depends on `acpi` and `acpitool`

It:

1. Outputs the average percent of all batteries in the device

2. How long it'll take for all batteries to fully charge (if the device is plugged in), or how long until it becomes completely empty (if not plugged in),

3. Says if the device is being charged.

The final output is in the format `All batteries: Discharging 90%, 10:10:06 remaining` (with different numbers and Discharging can be Charging).

``````#!/bin/bash

get_time_until_charged() {

# parses acpitool's battery info for the remaining charge of all batteries and sums them up
sum_remaining_charge=\$(acpitool -B | grep -E 'Remaining capacity' | awk '{print \$4}' | grep -Eo "[0-9]+" | paste -sd+ | bc);

# finds the rate at which the batteries being drained at
present_rate=\$(acpitool -B | grep -E 'Present rate' | awk '{print \$4}' | grep -Eo "[0-9]+" | paste -sd+ | bc);

# divides current charge by the rate at which it's falling, then converts it into seconds for `date`
seconds=\$(bc <<< "scale = 10; (\$sum_remaining_charge / \$present_rate) * 3600");

# prettifies the seconds into h:mm:ss format
pretty_time=\$(date -u -d @\${seconds} +%T);

echo \$pretty_time;
}

get_battery_combined_percent() {

# get charge of all batteries, combine them
total_charge=\$(expr \$(acpi -b | awk '{print \$4}' | grep -Eo "[0-9]+" | paste -sd+ | bc));

# get amount of batteries in the device
battery_number=\$(acpi -b | wc -l);

percent=\$(expr \$total_charge / \$battery_number);

echo \$percent;
}

get_battery_charging_status() {

if \$(acpi -b | grep --quiet Discharging)
then
echo "Discharging";
else # acpi can give Unknown or Charging if charging, https://unix.stackexchange.com/questions/203741/lenovo-t440s-battery-status-unknown-but-charging
echo "Charging";
fi
}

echo "All batteries: \$(get_battery_charging_status) \$(get_battery_combined_percent)%, \$(get_time_until_charged ) remaining";
``````
• Welcome on the Unix SE! Note, only posting scripts/core/commands is not a very HQ answer. Explain what is does and how. – peterh Aug 4 '18 at 19:36
• @Pierre.Vriens it outputs the average percent of all batteries combined, how long it'll take for all batteries to fully charge (if device is plugged in) or completely empty (if not plugged in), and says if the device is being charged, in the format "All batteries: Discharging 90%, 10:10:06 remaining" (with different numbers of course). I've never written a bash script before, sorry if it's confusing – David M Aug 4 '18 at 19:36
• much better now! – Pierre.Vriens Aug 4 '18 at 19:53