It is currently August 10th, 2020, 4:13 pm

Return laptop battery values

Help with creating, editing & fixing problems with skins
User avatar
Active Colors
Moderator
Posts: 605
Joined: February 16th, 2012, 3:32 am

Return laptop battery values

Post by Active Colors »

Hello. I am interested in returning some battery information values. Specifically I am interested in estimated time to full charge of a laptop battery, but any other data will be much helpful. In Windows 10 the thing I am looking for looks like this:
Capture.PNG
I am wondering how to obtain or calculate this info?

I have found out about Win32_PortableBattery class, and specifically TimeToFullCharge which I am interested in, but it does not return anything using command line WMIC PATH Win32_PortableBattery Get TimeToFullCharge. I believe my hardware simply does not provide this information. Therefore there is some other way to obtain or calculate this piece.

Somehow one of the nirsoft's utilities provide this information:
2.PNG
I am wondering if it obtains some values available and then makes its own estimations based on some calculations. I am trying to find out how it obtains those values and possible formulas.

I think by using mWh values of current capacity, full charged capacity, and charge/discharge rate I can calculate my desired estimation but I can't find out how to retrieve them.

(I know that this nirsoft utility is able to export values through command line but I would like to stay away from calling external application once every minute)
You do not have the required permissions to view the files attached to this post.
User avatar
Active Colors
Moderator
Posts: 605
Joined: February 16th, 2012, 3:32 am

Re: Return laptop battery values

Post by Active Colors »

Meanwhile I will try to break down SilverAzide's skin that does something familiar:
Capture.PNG
Update:
I found out he made a custom plugin to obtain the charge values. I will try to break them down and see if I can wrap my head around and make the estimation formula.
You do not have the required permissions to view the files attached to this post.
User avatar
SilverAzide
Posts: 927
Joined: March 23rd, 2015, 5:26 pm

Re: Return laptop battery values

Post by SilverAzide »

Active Colors wrote: July 25th, 2020, 11:11 am Meanwhile I will try to break down SilverAzide's skin that does something familiar:

Update:
I found out he made a custom plugin to obtain the charge values. I will try to break them down and see if I can wrap my head around and make the estimation formula.
Hi AC,
You can get all the info you need from WMIC or Powershell, but like you stated, I didn't want to make all those external calls once a minute. The custom plugin is the way I handled it. The plugin docs are in the Resources folder if you want to use it. If you want the WMI queries, let me know.
Gadgets DeviantArt More...
User avatar
Active Colors
Moderator
Posts: 605
Joined: February 16th, 2012, 3:32 am

Re: Return laptop battery values

Post by Active Colors »

SilverAzide wrote: July 25th, 2020, 4:34 pm Hi AC,
You can get all the info you need from WMIC or Powershell, but like you stated, I didn't want to make all those external calls once a minute. The custom plugin is the way I handled it. The plugin docs are in the Resources folder if you want to use it. If you want the WMI queries, let me know.
Hi SilverAzide,
First of all, thank you for the plugin! It really does simplify some things regarding the battery like obtaining charge/discharge watts/ampers rates, current and max charges.

I have been poking formulas for a whole day and haven't come up with anything close to the possible estimations. I want to work on the calculations for a bit more, maybe I will come up with something but I see that I have tried so far various combinations and guides, and left with very little air to work with. If it won't take you much time I will really appreciate your hints or queries. Sorry for asking you to do my job...
User avatar
CyberTheWorm
Posts: 714
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

Re: Return laptop battery values

Post by CyberTheWorm »

While not as elegant as SilverAzide's on. I did make a monitor for my laptop, the part I used for estimated run time was this code.

Code: Select all

[MeasureBattery]
Measure=Plugin
Plugin=PowerPlugin
Powerstate=ACLine
Substitute="0":"Battery","1":"AC Power"

[MeasureStatus]
Measure=Plugin
Plugin=PowerPlugin
Powerstate=Status
Substitute="0":"No Battery","1":"Charging"

[MeasureLiftime]
Measure=Plugin
Plugin=PowerPlugin
Powerstate=Lifetime

[MeasurePercent]
Measure=Plugin
Plugin=PowerPlugin
Powerstate=Percent

[MeterShapeRectangle]
Meter=Shape
X=0
Y=0
Shape=Rectangle 3,3,200,100 | Fill Color 32,32,32,255 | StrokeWidth 6 | StrokeColor 0,0,0,220

[MeterUptime]
Meter=String
X=103
Y=53
MeasureName=MeasureBattery
MeasureName2=MeasureStatus
MeasureName3=MeasureLiftime
MeasureName4=MeasurePercent
Text=%1#CRLF#%2#CRLF#%3 %4%
FontColor=255,255,255,255
StringAlign=CenterCenter
AntiAlias=1
The only source of knowledge is experience. Albert Einstein
Deviant Art Page
User avatar
SilverAzide
Posts: 927
Joined: March 23rd, 2015, 5:26 pm

Re: Return laptop battery values

Post by SilverAzide »

Active Colors wrote: July 26th, 2020, 12:06 pm Hi SilverAzide,
First of all, thank you for the plugin! It really does simplify some things regarding the battery like obtaining charge/discharge watts/ampers rates, current and max charges.

I have been poking formulas for a whole day and haven't come up with anything close to the possible estimations. I want to work on the calculations for a bit more, maybe I will come up with something but I see that I have tried so far various combinations and guides, and left with very little air to work with. If it won't take you much time I will really appreciate your hints or queries. Sorry for asking you to do my job...
Here are some PowerShell queries that might help you. The PowerPlugin plugin gives you plenty of useful info, some of which is duplicated in what you can get from the queries, but here are the two main ones in a form that will dump out everything. You can Google the Microsoft docs on the classes to find the meanings of certain status values.

Code: Select all

Get-WmiObject -Namespace root\cimv2 -Class Win32_Battery
Get-WmiObject -Namespace root\wmi -Class BatteryStatus
The most useful thing the first command will give you is the name of the battery. There are some additional properties that duplicate PowerPlugin.

The second command is the real one you want. This will dump out all the battery info you need to build a robust skin without using any plugin other than a RunCommand measure. You will notice two properties, ChargeRate and DischargeRate. One or both of these will be zero; if both are zero, the machine is plugged in and not charging. If either is >0, then you are either charging or discharging the battery. I opted to make my plugin return a single value, positive charge rate or a negative discharge rate. You'll never have a case where both are >0.

Another tip: It *IS* possible for a laptop to have more than one battery. A few years back I had Dell Precision laptop with two batteries (when one was discharged, the machine would switch over to the other one). If you care to design a skin that can handle this situation, you'll need to design your queries to handle that case, otherwise you'll get two sets of results from your query. You can either specify the InstanceName of the battery you want, or you can simply ask for whichever battery is measuring more than zero volts. I found that, on my Dell laptop at least, the voltage would be zero for the inactive battery. Using the InstanceName requires escaping some backslashes, so it's a little more problematic to use it.

Here are the above queries that will produce output that is limited to what you might want and can be easily parsed with some regex:

Code: Select all

Get-WmiObject -Namespace root\cimv2 -Class Win32_Battery | Format-List Name
Get-WmiObject -Namespace root\wmi -Class BatteryStatus -Filter 'Voltage > 0' | Format-List ChargeRate, DischargeRate, InstanceName, RemainingCapacity, Voltage
ChargeRate/Discharge rate is in millwatts, so divide by 1000 to get it into watts. If you want it in milliamps instead, divide by the Voltage value. RemainingCapacity is in milliwatt/hours, so divide by 1000 to get watt/hours or by Voltage to get milliamp/hours.

Hope this helps!
Last edited by SilverAzide on July 26th, 2020, 7:07 pm, edited 1 time in total.
Gadgets DeviantArt More...
User avatar
Yincognito
Posts: 2223
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Return laptop battery values

Post by Yincognito »

Active Colors wrote: July 26th, 2020, 12:06 pm Hi SilverAzide,
First of all, thank you for the plugin! It really does simplify some things regarding the battery like obtaining charge/discharge watts/ampers rates, current and max charges.

I have been poking formulas for a whole day and haven't come up with anything close to the possible estimations. I want to work on the calculations for a bit more, maybe I will come up with something but I see that I have tried so far various combinations and guides, and left with very little air to work with. If it won't take you much time I will really appreciate your hints or queries. Sorry for asking you to do my job...
I use something like this:

Code: Select all

Measure=Plugin
Plugin=RunCommand
Program=powershell
Parameter=-command gwmi -namespace 'root/wmi' -class BatteryStatus -filter \"InstanceName like '%PNP0C0A%'\" | select Active,@{n='ChargeRate';e={[string][math]::round($_.ChargeRate/1,2)}},@{n='DischargeRate';e={[string][math]::round($_.DischargeRate/1,2)}},InstanceName | sort InstanceName | format-list
State=Hide
OutputType=ANSI
I believe that SilverAzide and I talked a bit on this subject (batterries and identifying them, charge and discharge rates) in the past, and each of us chose the path he was most satisfied with, but these paths are similar, as you can see, as they're both based on WMI / Powershell. I learned a lot from that discussion, thanks to SilverAzide.

However, on the matter of estimation, I don't think it's a big deal, really. Sure, you have to identify the battery, get its current and max charge, and then it's just a matter of dividing (max charge - current charge) by the charge or discharge rate, according to the scenario requirements. Charge and discharge rates are relatively easy to get, battery identification is simple if just one but can be complicated in the case of multiple batteries, I believe the only potential problems may be getting the current and max charge of the battery, especially the latter one. The reason is that the data might or might not be available in WMI or PerfMon, depending on the device.
User avatar
Active Colors
Moderator
Posts: 605
Joined: February 16th, 2012, 3:32 am

Re: Return laptop battery values

Post by Active Colors »

Hey guys,
I have seen your replies but I wanted to try my best before bothering you again. Today I have finished all the estimations I wanted to calculate. There are some obstacles with some hardware as they do not report the metrics I needed, as they are designed to output only basic (fundamental) information. Here are the three commands I used to obtain infomation:

Code: Select all

Get-WmiObject -Namespace root\cimv2 -Class Win32_Battery
Get-WmiObject -Namespace root\cimv2 -Class Win32_PortableBattery
Get-WmiObject -Namespace root\wmi -Class BatteryStatus
And these are my outputs:
Image 4.jpg
In the end I made my formulas like Yincognito mentioned, and so far so good.
Thank you SilverAzide for your extensive descriptions! And again, for the plugin as well! They are very helpful for my skin I am currently building. I plan to use it in long-term to have a conscious control over the battery/energy use and sustainable consumption :)
And thanks for describing the multiple battery case use! I will add this support a bit later when I am done with the core construction. Here is how it looks like:
IMG_0261.JPG
When charging, instead of battery lifetime and total battery working time, it shows time until charged and total charging time. Those are just estimations of course but they are very helpful for me :)
I will release 1.0 later when I am done with all the logics.
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Posts: 2223
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Return laptop battery values

Post by Yincognito »

Looks good to me - nice job. :thumbup: