Linux – Lenovo T440s battery status unknown, but charging

arch linuxbatterylaptoppower-management

I'm trying to configure my Lenovo on arch linux. The last thing I need to do is get the battery 100% working.

Right now it appears that the main battery's state is unknown:

> acpi -V
Battery 0: Unknown, 97%
Battery 0: design capacity 5849 mAh, last full capacity 5956 mAh = 100%
Battery 1: Charging, 96%, 00:05:50 until charged
Battery 1: design capacity 1861 mAh, last full capacity 1536 mAh = 82%
Adapter 0: on-line
Thermal 0: ok, 43.0 degrees C
Thermal 0: trip point 0 switches to mode critical at temperature 200.0 degrees C
Cooling 0: x86_pkg_temp no state information available
Cooling 1: intel_powerclamp no state information available
Cooling 2: Processor 0 of 10
Cooling 3: Processor 0 of 10
Cooling 4: Processor 0 of 10
Cooling 5: Processor 0 of 10

But if I query the battery directly I get a completely different response:

> cat /sys/class/power_supply/BAT0/status
Charging

> cat /sys/class/power_supply/BAT0/uevent
POWER_SUPPLY_NAME=BAT0
POWER_SUPPLY_STATUS=Charging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_CYCLE_COUNT=0
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
POWER_SUPPLY_VOLTAGE_NOW=12389000
POWER_SUPPLY_POWER_NOW=0
POWER_SUPPLY_ENERGY_FULL_DESIGN=23200000
POWER_SUPPLY_ENERGY_FULL=19150000
POWER_SUPPLY_ENERGY_NOW=19050000
POWER_SUPPLY_CAPACITY=99
POWER_SUPPLY_CAPACITY_LEVEL=Normal
POWER_SUPPLY_MODEL_NAME=45N1773
POWER_SUPPLY_MANUFACTURER=SANYO
POWER_SUPPLY_SERIAL_NUMBER=16120

I'm at a complete loss as to what to do here. I have a script reporting battery life that runs off of the output of some of these commands and I would like it to be as complete as possible. Clearly the battery is charging, but why would acpi -V say it's unknown? Does anyone know?

Best Answer

This is a common issue with Thinkpad laptops with dual batteries.

When you plug in your laptop it will start by charging BAT0 while BAT1 reports a unknown state. BAT1 will report a charging state when BAT0 is full and BAT1 actually starts charging.

You need to take this into account in your script, and combine values for BAT0 and BAT1 to have something usable :

battery_level=$(("$battery_level_0 + $battery_level_1"))
battery_max=$(("$battery_max_0 + $battery_max_1"))

battery_percent=$(("$battery_level * 100"))
battery_percent=$(("$battery_percent / $battery_max"))

Here is a full example :

#!/bin/sh

path_ac="/sys/class/power_supply/AC"
path_battery_0="/sys/class/power_supply/BAT0"
path_battery_1="/sys/class/power_supply/BAT1"

ac=0
battery_level_0=0
battery_level_1=0
battery_max_0=0
battery_max_1=0

if [ -f "$path_ac/online" ]; then
    ac=$(cat "$path_ac/online")
fi

if [ -f "$path_battery_0/energy_now" ]; then
    battery_level_0=$(cat "$path_battery_0/energy_now")
fi

if [ -f "$path_battery_0/energy_full" ]; then
    battery_max_0=$(cat "$path_battery_0/energy_full")
fi

if [ -f "$path_battery_1/energy_now" ]; then
    battery_level_1=$(cat "$path_battery_1/energy_now")
fi

if [ -f "$path_battery_1/energy_full" ]; then
    battery_max_1=$(cat "$path_battery_1/energy_full")
fi

battery_level=$(("$battery_level_0 + $battery_level_1"))
battery_max=$(("$battery_max_0 + $battery_max_1"))

battery_percent=$(("$battery_level * 100"))
battery_percent=$(("$battery_percent / $battery_max"))

if [ "$ac" -eq 1 ]; then
    plug=""

    echo "$plug $battery_percent %"
else
    if [ "$battery_percent" -gt 95 ]; then
        echo ""
    elif [ "$battery_percent" -gt 85 ]; then
        icon=""
    elif [ "$battery_percent" -gt 60 ]; then
        icon=""
    elif [ "$battery_percent" -gt 35 ]; then
        icon=""
    elif [ "$battery_percent" -gt 10 ]; then
        icon=""
    else
        icon=""
    fi

    echo "$icon $battery_percent %"
fi
Related Question