Skip to content

Instantly share code, notes, and snippets.

@cmavr8 cmavr8/bathealth
Last active Aug 29, 2015

Embed
What would you like to do?
Bathealth - Shows live info and statistics about a Lenovo Thinkpad battery
#!/bin/bash
# Bathealth - Shows live info and statistics about a Lenovo Thinkpad battery
# By Chris Mavrakis - cmavrakis.com
# v0.3 - 6-May-15
# Usage:
# - Install in /usr/bin or ~/bin, or not...
# - Run as a normal user: bathealth or ./bathealth
############## FUNCTIONS ##############
# Gets a file ($1), a description ($2) and a unit ($3) and retrieves the value from the file and prints it. No return.
function myprint {
tmp=`cat $bat0/$1`
# Check if we were asked to print the State of charge
if [[ "$2" != "SOC:" ]]
then
# If not, do a division (usinb bc), set some colors and print everything together
echo $2 ${green}`echo "scale=2; $tmp / 1000" | bc`"${reset} ($3)"
else
# If it's SOC just print the result in green
echo "$2 ${green}$tmp${reset} ($3)"
fi
}
# Prints the current and design capacity of a battery
function printcap {
tmp=`cat $bat0/design_capacity`
design_cap=`echo "scale=2; $tmp / 1000" | bc`
tmp=`cat $bat0/last_full_capacity`
last_cap=`echo "scale=2; $tmp / 1000" | bc`
# Calculate the percentage of the battery's health condition using bc
condition=`echo "scale=5; ($last_cap / $design_cap) * 100" | bc`
echo -e "Design capacity: ${green}$design_cap${reset} (Ah)\nLast full charge: ${green}$last_cap${reset} (Ah) (${green}$condition${reset} % of design)."
}
# Prints the voltages of individual battery cells. No input params or return.
# The non-use of functions is ridiculus, I know, but AFAIK bash cannot handle floats a return values, so we have to stick
# to stupid copy-pasting of commands.
function printcells {
tmp=`cat $bat0/group1_voltage`
cell1=`echo "scale=2; $tmp / 1000" | bc`
tmp=`cat $bat0/group1_voltage`
cell2=`echo "scale=2; $tmp / 1000" | bc`
tmp=`cat $bat0/group1_voltage`
cell3=`echo "scale=2; $tmp / 1000" | bc`
echo "Individual cell voltages: ${green}$cell1 $cell2 $cell3${reset}"
}
########## END OF FUNCTIONS ###########
# Define some colors
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
# Battery to monitor
bat0="/sys/devices/platform/smapi/BAT0"
# Main part
echo "Thinkpad Battery Health & Status"
# Check for kernel module tp_smapi. This is an old part of the script, probably unneeded
smapi_loaded=`lsmod |grep tp_smapi`
if [[ "$smapi_loaded" == '' ]] # blank var
then
gksudo modprobe tp_smapi # load tp_smapi
smapi_loaded=`lsmod |grep tp_smapi` # recheck if smapi was loaded
if [[ "$smapi_loaded" == '' ]] #blank var
then
echo "Tp_smapi cannot be loaded. Is it installed? Exiting..."
exit
fi
fi
# Get some information that will not change much over time
model=`cat $bat0/model`
manufacturer=`cat $bat0/manufacturer`
manufacture_date=`cat $bat0/manufacture_date`
first_use=`cat $bat0/first_use_date`
cycle_count=`cat $bat0/cycle_count`
# Main battery status loop
while :
do
clear
# Print title. ${red} etc are for setting color, $(tput bold) is for bold - dah
echo -e "${red}$(tput bold)== Live battery stats ==${reset}\n"
myprint remaining_capacity "Current charge:" "Wh"
myprint voltage "Voltage:" "V"
printcells
myprint remaining_percent "SOC:" "%"
# Is the battery charging/discharging or idle?
echo -e "State: ${green}`cat $bat0/state`${reset}"
myprint temperature "Temperature:" "degrees C"
myprint power_now "Consumption:" "W"
# Print the capacity and health
printcap
echo -e "\n(Wh stands for Watt-hours, Ah for Ampere-hours)\n\n$(tput smul)Further battery info:$(tput rmul)"
echo -e "You are using a battery with cells made by $manufacturer. Its model is $model and it was manufactured on $manufacture_date. It was first used on $first_use and has been cycled $cycle_count times since."
sleep "1"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.