Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How is your laptop battery? here is how to check its health.
#1
a simple script to check the health of your battery

Code:
#!/bin/bash

this is free to use under GPL2

clear ; clear

touch ~/percintage.txt
BaT_aCtUaL="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/energy_full )"

BaT_dEsIgN="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/energy_full_design )"

PerC1="$(echo "100/${BaT_dEsIgN}*${BaT_aCtUaL}" | bc -l )"
echo ${PerC1} > ~/bat_percentage.txt

PerC="$(awk 'NF{print $1/1}' OFMT="%2.2f" ~/bat_percentage.txt ) "
echo -e "  BATTERY HEALTH \n\nDESIGN MAX: ${BaT_dEsIgN} Wh \nACTUAL MAX: ${BaT_aCtUaL} Wh \nBAT HEALTH: ${PerC}%"

echo ""
rm ~/bat_percentage.txt
save this as anything.sh

then run 
Code:
chmod +x anything.sh

super simple. if it is below 25% or 30% it is time to change the battery.

mine is at 66.3% and yesterday it was 71.2% so i will be replacing mine soon. unfortunately.


Also, your BAT0 folder might be in another location or called BAT1. if so, adjust accordingly. But for most this should work as is.

after an extensive search online the only real option I found was to install a very buggy app that didn't give the simple info i needed. so, i built one. actually it was surprisingly simple.

With so many laptops running Linux, I don't understand why is there so little information on this issue.

If you have a script or conky that does the same or similar, feel free to share. this is sloppy, but functional. as well as small, but reads from relatively static info.

anyway, hope my script helps.

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#2
Isn't this effectively what 'acpi -i' does? For more information, 'upower' is helpful but also more complex.

Also for temporary files you should probably use 'mktemp' or at least store them in '~/.cache/'. Actually, you probably do not even need it for this use case, 'awk' can operate on streams just fine.
Reply
#3
(07-11-2020, 03:33 AM)leon.p Wrote: Isn't this effectively what 'acpi -i' does? For more information, 'upower' is helpful but also more complex.

Also for temporary files you should probably use 'mktemp' or at least store them in '~/.cache/'. Actually, you probably do not even need it for this use case, 'awk' can operate on streams just fine.

Yes. But it isn't installed in most of my boxes. And the idea was to get the info without installing anything. I know acpi is very small in size and can be a powerful tool. However there are only 2 things needed to calculate health. max capacity of the battery, and actual capacity. With that you can get the batteries health. Everything else is just fluff.

This script is less than 20 lines(could be half that) and its readout is clear and easy to understand.

acpi is also easy to understand, don't get me wrong. Mine is just a little more portable. IMHO



I like getting responses from you. They are always informative, and just cocky enough to feel the burn, but not enough to be rude. And that truly takes skill. So I tip my hat to you on that. Brains need to be challenged.

I must confess, I have an updated version of this script. It uses awk and case instead of creating and deleting a file.
But I posted this one instead because I knew you would say something about it. and in doing so also give other golden nuggets of information for the community. 

I didn't think the response would be this fast. thanks for that.

You are a pillar in this community. It would not be the same without you. and I mean that. Thank you leon.p.


For the novice, this script works just fine as it is. What we are discussing is how the code gets the information, and which approach is better. The output is basically the same. So feel free to use it as it is.
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#4
also, at first I tried using upower and filtering out all the info that I didn't need. but this made for a script that was overly complex and still didn't give clean enough info.

I would be willing to try a different formula in this script. Possibly even another language like python. Assuming the output is about the same. Do you have any examples?
current output
Code:
  BATTERY HEALTH

DESIGN MAX: 47.52 Wh
ACTUAL MAX: 31.51 Wh
BAT HEALTH: 66.31 %
Yeah like i said, my battery is dying.

I added an alias to my bashrc/zshrc
actually I have an alias file and I just added a link to it in there. I call it by simply typing  "bh" short fot battery health.

My thought is that this could be done in 10 (clean) lines or less. I am at 15 (almost clean ) lines

any input is welcome

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#5
Interesting script. I've noticed that on most of my the files are 'charge_full' and 'charge_ful_design' while one has 'energy_full' and 'energy_full_design'. I'm not sure why the difference since each of my two laptops runnng Ubuntu 18.04 shows different files.

I'm intrigued, so I'll play around with it and probably set a variable for the battery and a check to see which file is used. It adds a litttle complexity to the script but it will cover all my bases.

Thanks for posting your script.
Rick Romig
"It's never wrong to introduce a child to Linux."
@ludditegeek
Rick's Tech Stuff
GitHub
Reply
#6
(07-11-2020, 06:46 PM)eliasw4u Wrote: I would be willing to try a different formula in this script. Possibly even another language like python. Assuming the output is about the same. Do you have any examples?

Ok now bear with me: Let's do it in C!

(I also have "charge_full" instead of "energy_full")

Code:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>

static bool int_from_file (const char *directory, const char *file, int *var)
{
        char path[1024];
        memset(path, '\0', sizeof(path));
        snprintf(path, sizeof(path) -1, "%s/%s", directory, file);
        FILE *fd;
        if ( NULL == ( fd = fopen(path, "r")) )
                return false;
        fscanf(fd, "%d", var);
        fclose(fd);
        return true;
}

int main (int argc, char *argv[])
{
        char battery_directory[1024];
        memset(battery_directory, '\0', sizeof(battery_directory));

        int capacity, full, full_design;
        for (int i = 0; i < 10; i++)
        {
                if ( i == 0 )
                        snprintf(battery_directory, sizeof(battery_directory) - 1,
                                        "/sys/class/power_supply/BAT/");
                else
                        snprintf(battery_directory, sizeof(battery_directory) - 1,
                                        "/sys/class/power_supply/BAT%d/", i - 1);

                if (! int_from_file(battery_directory, "capacity", &capacity))
                        continue;
                if (! int_from_file(battery_directory, "charge_full", &full))
                        continue;
                if (! int_from_file(battery_directory, "charge_full_design", &full_design))
                        continue;

                fprintf(stdout, "Capacity: %d%%\nHealth:  %d%%\n\n", capacity,
                                (int)((full / (float)full_design) * 100));
        }
}

This will automatically find the correct files in and even works with multiple batteries.

It's not perfect (A real program should actually query '/sys/class/power_suply/' for directories with "BAT" in the name instead of trying to guess a limited amount of possible names and it should check whether "charge" or "energy" is used and colourful output would also be nice), but this was supposed to be simple, eh?
Reply
#7
(07-12-2020, 12:53 AM)rickromig Wrote: Interesting script. I've noticed that on most of my  the files are 'charge_full' and 'charge_ful_design' while one has 'energy_full' and 'energy_full_design'. I'm not sure why the difference since each of my two laptops runnng Ubuntu 18.04 shows different files.

I'm intrigued, so I'll play around with it and probably set a variable for the battery and a check to see which file is used. It adds a litttle complexity to the script but it will cover all my bases.

Thanks for posting your script.
 
charge_full? That is interesting. I am also on 18.04 vanilla.
In a VM I spun up 20.04 as well as Arch Linux, Fedora, and Centos. They all have "energy_full". So I am not sure why you would have charge_full instead. But thanks for bringing that to my attention.
I will add add a variable for it.

If you have the same issue, power / energy then
Switch this
Code:
BaT_aCtUaL="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/energy_full )"
BaT_dEsIgN="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/energy_full_design )"

With this
Code:
if [ -f /sys/class/power_supply/BAT0/power_full_design ]
        then
           Var1="power_full"
           Var2="power_full_design"

        else
           Var1="energy_ful  l"
           Var2="energy_full_design"

fi

BaT_aCtUaL="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/$Var1 )"
BaT_dEsIgN="$( awk 'NF{print $1/1000000}' OFMT="%2.2f" /sys/class/power_supply/BAT0/$Var2 )"

That should fix the issue.

There are several ways to approach it, this one was the first that came to mind. and seems to work in testing.

Opinions are always welcome.

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#8
(07-12-2020, 03:28 AM)leon.p Wrote: ...It's not perfect (A real program should actually query '/sys/class/power_suply/' for directories with "BAT" in the name instead of trying to guess a limited amount of possible names  and it should check whether "charge" or "energy" is used and colorful output would also be nice), but this was supposed to be simple, eh?

Colorful, it is almost funny that you mention that, I am working on adding a little bling to the output of the script.
And adding a variable that checks to see if the health is below a certain amount and if so, change to yellow, then to red when it reaches the danger zone.

What is that they say about great minds?

On another note

My battery got a little better today. How is this possible?
Code:
  BATTERY HEALTH

DESIGN MAX: 47.52 Wh
ACTUAL MAX: 33.44 Wh
BAT HEALTH: 70.37 %
 
It was at 66.31 % yesterday
So strange.

For those that do not understand, the percentage shown is not how much the battery is charged, it represents the maximum amount of charge that it can hold. meaning that even though it is charged to 100 % it only actually has 70.37 % (in my case) and can hold no more.

It is natural for batteries to loose strength over time.
Which is why it is good to check it every once in a while. Thus, this script,

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply
#9
"My battery got a little better today. How is this possible?"
Ambiant conditions change the maximum amount of charge at any one time (temperature/humidity and other such things).
Reply
#10
(07-13-2020, 01:31 AM)spudnuts Wrote: "My battery got a little better today. How is this possible?"
Ambiant conditions change the maximum amount of charge at any one time (temperature/humidity and other such things).
I considered that. But I live in Acapulco Mexico. The climate here is almost always the same, 80 and humid. I expect a variation between a few tenths of a percent, but 4% is a lot. Although You are probably right. I will watch it over the next week or so.

thanks for the info

kudos
A computer without Microsoft is like a piece of chocolate cake without ketchup and mustard.


Telegram @eliasw4u
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)