Skip to content

Window 11 battery percent always 100% #3108

Closed

Description

@draganbjedov
var systemInfo = new SystemInfoFFM();
var hal = systemInfo.getHardware();

var battery = hal.getPowerSources().stream()
        .filter(ps -> ps.getName().equals("BAT0") || ps.getName().equals("System Battery"))
        .findFirst()
        .orElseThrow();

timer = new Timer(0, _ -> {
       battery.updateAttributes();
       double remainingCapacityPercent = battery.getRemainingCapacityPercent();
       System.out.println("Battery level: " + remainingCapacityPercent + "%, Power online: " + battery.isPowerOnLine());
});
timer.setDelay(1000);
timer.start();

Power online is handled correctly but, for remaining capacity percent it always prints 1%.

Battery level: 1%, Power online: true

Activity

  1. dbwiddis commented on Apr 2, 2026

    @dbwiddis
    Member

    The variable naming is poor, but that 1 is a 1.0 and you need to multiply by 100 to get percentage.
    From the javadoc:

    @return A value between 0.0 (fully drained) and 1.0 (fully charged)

    From the toString():

    sb.append("RemainingCapacityPercent: ").append(getRemainingCapacityPercent() * 100).append("%, ");
  2. added
    question
    on Apr 2, 2026
  3. draganbjedov commented on Apr 7, 2026

    @draganbjedov
    Author

    Actually the problem is that it always shows 1.0

    Image
  4. dbwiddis commented on Apr 7, 2026

    @dbwiddis
    Member

    Ah, now I understand.

    Looks like we used to have an initial capacity set from CallNtPowerInformation. The other place we use that in the code (Processor Frequency) has a note that approach broke in Win11 23H2, so it probably broke here too.

    And the problem is that we calculated the more accurate values but never re-did the calculation of capacity with it. 🤦

  5. added
    confirmed bug
    and removed
    question
    on Apr 7, 2026
  6. self-assigned this
    on Apr 7, 2026
  7. coderabbitai commented on Apr 7, 2026

    @coderabbitai
    Contributor

    Coding Plan

    Summary

    • The fix adds a single line of code to recalculate psRemainingCapacityPercent after the IOCTL queries update both psCurrentCapacity and psMaxCapacity
    • This ensures the reported percentage matches the authoritative per-battery IOCTL data rather than using stale or defaulted Phase 1 values
    • The fix mirrors the existing calculation pattern for consistency and requires no new abstractions or refactoring

    Implementation Steps

    Fix Battery Percentage Calculation

    This phase addresses the root cause of the bug by ensuring psRemainingCapacityPercent is recalculated after the IOCTL queries update the capacity values.

    Add Percentage Recalculation After IOCTL Status Query

    After the IOCTL BATTERY_STATUS query successfully retrieves the current capacity, recalculate the remaining capacity percentage using the IOCTL-sourced values.

    • Update oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java in the getPowerSource() method
    • Inside the IOCTL_BATTERY_QUERY_STATUS success block (after psCurrentCapacity = bs.Capacity is assigned), add the recalculation: psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity / psMaxCapacity)
    • Place this after all status values are extracted (after the psAmperage calculation) but still within the same if block that confirms the IOCTL call succeeded
    • Use the same formula as Phase 1 (line 124) to maintain consistency: Math.min(1d, (double) psCurrentCapacity / psMaxCapacity)
    • No additional guard for division by zero is needed since psMaxCapacity is initialized to 1 and the existing Phase 1 code follows the same pattern
    🤖 Prompt for AI agents
    Fix the battery percentage bug in `WindowsPowerSource.java` by recalculating
    `psRemainingCapacityPercent` after the IOCTL phase overwrites the capacity
    values.
    
    - File to modify:
    `oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java`
    - In the `getPowerSource()` method, locate the `IOCTL_BATTERY_QUERY_STATUS`
    success block where `psCurrentCapacity = bs.Capacity` is assigned
    - After all status values are extracted from the IOCTL result (after the
    `psAmperage` calculation), add the following recalculation still within the same
    `if` block that confirms the IOCTL call succeeded:
    - `psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity /
    psMaxCapacity)`
    - Use this exact formula to mirror the existing Phase 1 calculation at line 124
    for consistency
    - No division-by-zero guard is needed since `psMaxCapacity` is initialized to 1
    and the existing Phase 1 code follows the same pattern
    
    Research

    The battery implementation for Windows lives in oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java. The getPowerSource() method retrieves battery data in two phases: first via CallNtPowerInformation (PowrProf API) to get SystemBatteryState, then via DeviceIoControl (IOCTL) to query per-battery BATTERY_INFORMATION and BATTERY_STATUS. The percentage is calculated only in Phase 1, but the capacity values are overwritten by Phase 2 without recalculating the percentage—causing the inconsistency reported in the ticket. When using SystemInfoFFM on Windows, the code falls back to this JNA-based implementation since no FFM Windows hardware layer exists yet.


    🚀 Next Steps

    🤖 All AI agent prompts combined
    Fix the battery percentage bug in `WindowsPowerSource.java` by recalculating
    `psRemainingCapacityPercent` after the IOCTL phase overwrites the capacity
    values.
    
    - File to modify:
    `oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java`
    - In the `getPowerSource()` method, locate the `IOCTL_BATTERY_QUERY_STATUS`
    success block where `psCurrentCapacity = bs.Capacity` is assigned
    - After all status values are extracted from the IOCTL result (after the
    `psAmperage` calculation), add the following recalculation still within the same
    `if` block that confirms the IOCTL call succeeded:
    - `psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity /
    psMaxCapacity)`
    - Use this exact formula to mirror the existing Phase 1 calculation at line 124
    for consistency
    - No division-by-zero guard is needed since `psMaxCapacity` is initialized to 1
    and the existing Phase 1 code follows the same pattern
    
    💡 Iterate on the plan with: @coderabbitai <feedback>
    Example Feedback
    - `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
    - `@coderabbitai` For design choice 1 go ahead with option 3 and replan.
    

    💬 Have feedback or questions? Drop into our discord!

  8. changed the title [-]Window 11 battery percent always 1%[/-] [+]Window 11 battery percent always 100%[/+] on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

confirmed bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions