Jump to content

Trouble getting battery info for laptop


Go to solution Solved by rsn,

Recommended Posts

I want to capture the watt hours capacity and percentage used (using the difference of battery charge level at 2 points in time). _WinAPI_GetSystemPowerStatus should work for the percentage used. 

I have used Win32_Battery but that doesn't have the data I want. 

This below looks like it might work for watt hours but I don't know how to get autoit to talk to a windows Namespace. Any suggestions?

https://learn.microsoft.com/en-us/uwp/api/windows.devices.power.batteryreport.remainingcapacityinmilliwatthours?view=winrt-22621

 

Link to post
Share on other sites

In reading the notes on Win32_battery it says the design capacity property is the capacity expressed in milliwatt-hours.  Trying to understand why that is not what you are after from reading your post?

Quote

DesignCapacity

Data type: uint32

Access type: Read-only

Qualifiers: MappingStrings ("MIF.DMTF|Portable Battery|002.8"), Units ("milliwatt-hours")

Design capacity of the battery in milliwatt-hours. If the property is not supported, enter 0 (zero).

This property is inherited from CIM_Battery.

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to post
Share on other sites

@Jfish I mispoke. from the description of DesignCapacity it sounds like it the total capacity of the battery and not the amount of milliwatt-hours of charge left on the battery. I tried Win32_battery but the value for DesignCapacity is blank (ran code on my laptop which has a battery) and the value comes back as null.

I tried other fields to make sure I was getting something. I get values for Availability, BatteryStatus, and Caption.

Here is the code I am using:

#include <MsgBoxConstants.au3>
Dim $info

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$objBatt = $objWMIservice.ExecQuery ("Select * from Win32_Battery")

For $object In $objBatt
    $info &= 'Availability: ' & $object.Availability & @CRLF
    $info &= 'BatteryRechargeTime: ' & $object.BatteryRechargeTime & @CRLF
    $info &= 'BatteryStatus: ' & $object.BatteryStatus & @CRLF
    $info &= 'DesignCapacity: ' & $object.DesignCapacity & @CRLF
    $info &= 'FullChargeCapacity: ' & $object.FullChargeCapacity & @CRLF
    $info &= 'TimeOnBattery: ' & $object.TimeOnBattery & @CRLF
    $info &= 'TimeToFullCharge: ' & $object.TimeToFullCharge & @CRLF
    $info &= 'Caption: ' & $object.Caption & @CRLF
Next

MsgBox($MB_SYSTEMMODAL, "battery", $info)

 

Edited by PeterlFF
Link to post
Share on other sites

I haven't tested the code but out of pure curiosity - is your laptop plugged in when you run the code (if so, consider unplugging)?  Also, confirming you are not using an emulator? "When debugging on a device emulator, the BatteryReport object returns null to the capacity and rate properties."

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to post
Share on other sites

I popped open WMI Explorer to look at the properties of Win32_Battery. On the Dell system I'm using, from WMIC command line:

wmic /NAMESPACE:\\root\cimv2 path Win32_Battery get EstimatedChargeRemaining,EstimatedRunTime,caption,deviceID,batterystatus

Which outputs:

BatteryStatus  Caption           DeviceID            EstimatedChargeRemaining  EstimatedRunTime
2              Internal Battery  183BYDDELL M3KCN12  97                        71582788

I had to remove power from the laptop for a few minutes to deplete it enough for WMI to report actual numbers. Not sure how the format may change for multiple laptop batteries. 

Also, Win32_PortableBattery has a property DesignCapacity that is measured in milliwatt-hours. On the Dell system I'm using, from WMIC command line:

wmic /NAMESPACE:\\root\cimv2 path Win32_PortableBattery  get DesignCapacity

Which outputs:

DesignCapacity
41580

That all said, your AutoIt code is probably good (I didn't try it) but note below:

  • BatteryRechargeTime is considered obsolete and left empty
  • DesignCapacity, FullChargeCapacity probably aren't supported by your bios/manufacturer/battery.
  • TimeOnBattery, TimeToFullCharge refers to UPS so may not apply to laptop internal batteries.

rsn

Edited by rsn
Win32_PortableBattery stuff
Link to post
Share on other sites

@rsnThanks for checking it out. I don't think Design capacity will work for me because it measures the capacity of the battery. I need to note the difference in watt hours from the start of some tasks and then at the end of those tasks. The BatteryReport API would work but I don't know how to connect to it. I tried this below but it doesn't return anything. Not even the "RemainingCapacityInMilliwattHours" header text so I don't think it even found that property or really connected to the BatteryReport API.

Any ideas what I am doing wrong?

 

#include <MsgBoxConstants.au3>
Dim $info

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$objBatt = $objWMIservice.ExecQuery ("Select * from BatteryReport")

For $object In $objBatt
    $info &= 'RemainingCapacityInMilliwattHours: ' & $object.RemainingCapacityInMilliwattHours & @CRLF

Next

MsgBox($MB_SYSTEMMODAL, "battery", $info & $objDisks)

 

Link to post
Share on other sites

@PeterlFF So remaining capacity of the battery is what you're after. I poked around some more in WMI Explorer (highly recommended to find WMI classes and properties quickly) and found something that might help. On the Dell system I'm using, from WMIC command line:

wmic /NAMESPACE:\\root\WMI path BatteryStatus get RemainingCapacity

which outputs:

RemainingCapacity
35226

According to the notes:

RemainingCapacity - UInt32
       Qualifiers: CIMTYPE, countertype, defaultscale, Description, DisplayName, perfdetail, read, WmiDataId, WmiVolatility
       The amount of remaining battery capacity. The measurement unit is defined by the underlying hardware platform, but is often represented in milliwatt-hours.

So if I had to guess, the AutoIt code would look like this (untested):

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI")
$objBatt = $objWMIservice.ExecQuery ("Select * from BatteryStatus")

For $object In $objBatt
    $info &= 'Remaining Capacity: ' & $object.RemainingCapacity & @CRLF
Next

Or 

$objBatt = $objWMIservice.ExecQuery ("Select RemainingCapacity from BatteryStatus")

if you didn't have any other properties you wanted to look at (19 others if you were curious).

 

rsn

Edited by rsn
Link to WMI Explorer
Link to post
Share on other sites

@rsn That looks great but not working for me. The AutoIT code doesn't return anything. I download the WMI Explorer, connected to the laptop, found the BatteryStatus class and executed the SELECT * FROM BatteryStatus query inside WMIExplorer and it said it completed successfully and retrieved 0 instances. I tried the other Battery related classes I found there and all were the same as BatteryStatus.

I tried it with the laptop plugged in and unplugged. 

 

Link to post
Share on other sites
  • Solution

Try this:

#include <MsgBoxConstants.au3>

$WMIObj = ObjGet("winmgmts:\\.\root\wmi")
    If @error Then
        MsgBox($MB_ICONERROR + $MB_TASKMODAL, @ScriptName, "Error getting wmi : " & Hex(@error, 8) , 10)
        Exit
    Else
        $WMIObjItems = $WMIObj.ExecQuery("select RemainingCapacity FROM BatteryStatus")
        If IsObj($WMIObjItems) Then
            For $oItem In $WMIObjItems
                $iRemainingCapacity = $oItem.RemainingCapacity
            Next
        EndIf
    EndIf

MsgBox (4096 , @ScriptName , "Remaining battery capacity in milliwatt hours=" & $iRemainingCapacity )

On the Dell laptop I use, the script returned 35226 @ 100% battery and 34531 @ 98%.

 

rsn

Edited by rsn
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...