Skip to content

Instantly share code, notes, and snippets.

@Kyome22
Created August 24, 2025 04:33
Show Gist options
  • Save Kyome22/e212428af0273a072df35a1cbd64ce07 to your computer and use it in GitHub Desktop.
Save Kyome22/e212428af0273a072df35a1cbd64ce07 to your computer and use it in GitHub Desktop.
Battery Health
import Foundation
import IOKit.ps
@main
struct TestCode {
static func main() async throws {
let psInfo = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let psList = IOPSCopyPowerSourcesList(psInfo).takeRetainedValue() as? [CFDictionary]
guard let psList else {
return
}
let powerSource = psList[0] as NSDictionary
print(powerSource)
}
}
@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

M1 Pro Mac

{
    "Battery Provides Time Remaining" = 1;
    BatteryHealth = Good;
    BatteryHealthCondition = "";
    Current = 0;
    "Current Capacity" = 100;
    DesignCycleCount = 1000;
    "Hardware Serial Number" = F8Y152505GYQ1LTA9;
    "Is Charged" = 1;
    "Is Charging" = 0;
    "Is Present" = 1;
    "LPM Active" = 0;
    "Max Capacity" = 100;
    Name = "InternalBattery-0";
    "Optimized Battery Charging Engaged" = 0;
    "Power Source ID" = 35782755;
    "Power Source State" = "AC Power";
    "Time to Empty" = 0;
    "Time to Full Charge" = 0;
    "Transport Type" = Internal;
    Type = InternalBattery;
}
Program ended with exit code: 0

@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

M2 Mac

{
    "Battery Provides Time Remaining" = 1;
    BatteryHealth = Good;
    BatteryHealthCondition = "";
    Current = 0;
    "Current Capacity" = 100;
    DesignCycleCount = 1000;
    "Hardware Serial Number" = F8Y2275059S10X2AF;
    "Is Charged" = 1;
    "Is Charging" = 0;
    "Is Present" = 1;
    "LPM Active" = 0;
    "Max Capacity" = 100;
    Name = "InternalBattery-0";
    "Power Source ID" = 7405667;
    "Power Source State" = "AC Power";
    "Time to Empty" = 0;
    "Time to Full Charge" = 0;
    "Transport Type" = Internal;
    Type = InternalBattery;
}
Program ended with exit code: 0

@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

M4 Pro Mac

{
    "Battery Provides Time Remaining" = 1;
    BatteryHealth = Good;
    BatteryHealthCondition = "";
    Current = 0;
    "Current Capacity" = 80;
    DesignCycleCount = 1000;
    "Hardware Serial Number" = F8YHB6006DC0000FWK;
    "Is Charged" = 0;
    "Is Charging" = 0;
    "Is Present" = 1;
    "LPM Active" = 0;
    "Max Capacity" = 100;
    Name = "InternalBattery-0";
    "Optimized Battery Charging Engaged" = 0;
    "Power Source ID" = 33751139;
    "Power Source State" = "AC Power";
    "Time to Empty" = 0;
    "Time to Full Charge" = 0;
    "Transport Type" = Internal;
    Type = InternalBattery;
}
Program ended with exit code: 0

@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

ioreg -rn AppleSmartBattery | awk '/AppleRawMaxCapacity/{max=$3} /DesignCapacity/{design=$3} END{printf "%.1f%%\n", max/design*100}'

@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

system_profiler SPPowerDataType

@Kyome22
Copy link
Author

Kyome22 commented Aug 24, 2025

Intel Mac

+-o AppleSmartBattery  <class AppleSmartBattery, id 0x1000002ca, registered, matched, active, busy 0 (1 ms), retain 6>
    {
      "TimeRemaining" = 132
      "AvgTimeToEmpty" = 65535
      "AdapterDetails" = {"AdapterVoltage"=20000,"Watts"=60,"PMUConfiguration"=1452,"IsWireless"=No,"Current"=3000}
      "AppleRawCurrentCapacity" = 577
      "InstantTimeToEmpty" = 65535
      "AppleRawMaxCapacity" = 3814
      "ExternalChargeCapable" = Yes
      "FullPathUpdated" = 1756012814
      "Voltage" = 12158
      "PermanentFailureStatus" = 0
      "BatteryInvalidWakeSeconds" = 30
      "AdapterInfo" = 0
      "MaxCapacity" = 3814
      "DesignCycleCount70" = 0
      "UpdateTime" = 1756012994
      "Manufacturer" = "SMP"
      "AppleRawAdapterDetails" = ({"AdapterVoltage"=20000,"Watts"=60,"PMUConfiguration"=1452,"IsWireless"=No,"Current"=3000})
      "Location" = 0
      "CurrentCapacity" = 577
      "LegacyBatteryInfo" = {"Amperage"=3105,"Flags"=7,"Capacity"=3814,"Current"=577,"Voltage"=12158,"Cycle Count"=70}
      "BatteryInstalled" = Yes
      "FirmwareSerialNumber" = 1
      "CycleCount" = 70
      "ChargerData" = {"ChargingCurrent"=3240,"NotChargingReason"=0,"ChargingVoltage"=13048,"VacVoltageLimit"=4350}
      "AvgTimeToFull" = 132
      "BatteryData" = {"LifetimeData"={"TotalOperatingTime"=31690,"UpdateTime"=1756012994,"AverageTemperature"=296,"Raw"=<000225a601252079013d403e000004e606ccce748080e467b24000000000000002580050110f09d3330b21290dd0ed9a1120ee46f506f39301280007bca4000b>,"TimeAtHighSoc"=<27000000d81400008f16000000000000>},"Serial"="D869395H0VVHDTDAG","ChemID"=14626,"Flags"=128,"DataFlashWriteCount"=2557,"PassedCharge"=18446744073709551163,"Voltage"=12113,"ResScale"=125,"RaTableRaw"=(<00000084008400a000ba0097007a0081008d009700ac00b60101019702a103f7>,<005500a600a600c300dd00b80099009f00ab00b900c800c70109016c02340341>,<0055007d007d009200ab008c006e007700810087009c009800ca014001e402e0>),"StateOfCharge"=16,"Qmax"=(4264,4282,4266),"CycleCount"=70,"DesignCapacity"=4315,"SystemPower"=4539,"PMUConfigured"=1620,"DOD0"=(15232,15168,15232),"PresentDOD"=(78,78,78),"CellVoltage"=(4026,4092,4040)}
      "OperationStatus" = 58371
      "PackReserve" = 125
      "BestAdapterIndex" = 1
      "DesignCapacity" = 4315
      "BootPathUpdated" = 1752872833
      "PostDischargeWaitSeconds" = 120
      "Serial" = "D869395H0VVHDTDAG"
      "Temperature" = 3125
      "UserVisiblePathUpdated" = 1756012994
      "IOReportLegendPublic" = Yes
      "InstantAmperage" = 2911
      "ManufacturerData" = <00000000090100013922000003333131033030330341544c015000>
      "FullyCharged" = No
      "MaxErr" = 1
      "DeviceName" = "bq20z451"
      "IOGeneralInterest" = "IOCommand is not serializable"
      "IOReportLegend" = ({"IOReportChannels"=((7167869599145487988,6460407809,"BatteryCycleCount")),"IOReportGroupName"="Battery","IOReportChannelInfo"={"IOReportChannelUnit"=0}})
      "Amperage" = 3105
      "IsCharging" = Yes
      "DesignCycleCount9C" = 1000
      "PostChargeWaitSeconds" = 120
      "ExternalConnected" = Yes
    }
Power:

    Battery Information:

      Model Information:
          Serial Number: D869395H0VVHDTDAG
          Manufacturer: SMP
          Device Name: bq20z451
          Pack Lot Code: 0
          PCB Lot Code: 0
          Firmware Version: 901
          Hardware Revision: 1
          Cell Revision: 3922
      Charge Information:
          The battery’s charge is below the warning level: No
          Fully Charged: No
          Charging: Yes
          Full Charge Capacity (mAh): 3814
          State of Charge (%): 16
      Health Information:
          Cycle Count: 70
          Condition: Normal

    System Power Settings:

      AC Power:
          System Sleep Timer (Minutes): 60
          Disk Sleep Timer (Minutes): 10
          Display Sleep Timer (Minutes): 60
          Wake on AC Change: No
          Wake on Clamshell Open: Yes
          Wake on LAN: Yes
          AutoPowerOff Delay: 259200
          AutoPowerOff Enabled: 1
          Current Power Source: Yes
          Display Sleep Uses Dim: Yes
          Hibernate Mode: 3
          LowPowerMode: 0
          PrioritizeNetworkReachabilityOverSleep: 0
      Battery Power:
          System Sleep Timer (Minutes): 4
          Disk Sleep Timer (Minutes): 10
          Display Sleep Timer (Minutes): 4
          Wake on AC Change: No
          Wake on Clamshell Open: Yes
          Wake on LAN: No
          AutoPowerOff Delay: 259200
          AutoPowerOff Enabled: 1
          Display Sleep Uses Dim: Yes
          Hibernate Mode: 3
          LowPowerMode: 0
          PrioritizeNetworkReachabilityOverSleep: 0
          Reduce Brightness: Yes

    Hardware Configuration:

      UPS Installed: No

    AC Charger Information:

      Connected: Yes
      Wattage (W): 60
      Charging: Yes

    Power Events:

        Next Scheduled Events:

              appPID: 381
              Type: Wake
              Scheduled By: com.apple.alarm.user-visible-com.apple.CalendarNotification.EKTravelEngine.periodicRefreshTimer
              Time: 2025/08/24 22:23
              UserVisible: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment