Skip to content

Fix battery status on multi-battery machines - #6845

Closed
geirolafs wants to merge 6 commits into
omacom:quattrofrom
geirolafs:battery-status-multi-battery
Closed

geirolafs wants to merge 6 commits into
omacom:quattrofrom
geirolafs:battery-status-multi-battery

Conversation

@geirolafs

@geirolafs geirolafs commented Aug 14, 2026 •

Copy link
Copy Markdown

On a dual-battery machine (Power Bridge ThinkPads and the like) whose first pack is dead, the power panel pins at 0% / 0Wh with a bogus cycle count while the working battery charges fine. omarchy-battery-status reads everything from the first battery UPower enumerates (upower -e | grep BAT | head -n 1) and the first BAT*/cycle_count glob match, so whichever pack sorts first wins regardless of whether it holds a charge.

Found on a ThinkPad T480 (dead internal BAT0, working removable BAT1): the panel showed 0%, 0Wh, cycles -1 while the bar icon — which reads UPower's aggregate DisplayDevice — was correct the whole time.

The fix:

  • Headline figures (percentage, state, rate, capacity, time) come from UPower's aggregate DisplayDevice, the same source the bar icon uses, so panel and bar now always agree — with one battery, several, or a dead pack in the mix.
  • Live sysfs wattage and cycle counts are summed over the packs that actually hold a charge (non-zero full-charge capacity), so a dead or disconnected pack no longer masks a working one. If every pack reads dead, they're still reported rather than printing nothing.
  • Charge-threshold reporting is deliberately unchanged: it still comes from the first battery's own UPower info (the aggregate carries no threshold lines), with the same sysfs fallback.

Notes for review:

  • On a multi-battery machine the cycles value becomes e.g. 5 / 7 (one count per live pack). The panel's key/value parser renders it as-is; single-battery machines still get a plain number.
  • Single-battery output is unchanged — verified byte-for-byte against the packaged script on real hardware, both --shell and plain modes.
  • New regression test test/shell.d/battery-status-dual-battery-test.sh reproduces the T480 scenario (dead pack enumerated first). The existing test's fixture gained a type file, since batteries are now identified by sysfs type rather than a BAT* name glob.

Verified on hardware:

Fixes #6895. Fixes #7916.

🤖 Generated with Claude Code

omarchy-battery-status read everything from the first battery UPower
enumerates and the first BAT*/cycle_count glob match. On a dual-battery
machine (Power Bridge ThinkPads and the like) whose first pack is dead,
the panel pins at 0% / 0Wh with a bogus cycle count while the working
battery charges fine.

Headline figures now come from UPower's aggregate DisplayDevice, which
is what the bar icon already reads, so panel and bar agree. Live wattage
and cycle counts are summed over the packs that actually hold a charge,
so a dead or disconnected pack no longer masks a working one. Charge
threshold reporting is unchanged: it still comes from the first
battery's own UPower info, since the aggregate carries no threshold
lines.

Single-battery machines see identical output (verified byte-for-byte on
a one-battery ThinkPad, both output modes).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings August 14, 2026 02:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates battery reporting to handle multi-battery systems with dead packs correctly.

Changes:

  • Uses UPower’s aggregate DisplayDevice.
  • Aggregates live sysfs wattage and cycle counts.
  • Adds dual-battery regression coverage.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
bin/omarchy-battery-status Aggregates battery status and per-pack telemetry.
test/shell.d/battery-status-test.sh Adds the required battery type fixture.
test/shell.d/battery-status-dual-battery-test.sh Tests a dead-first dual-battery configuration.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Asserts the combined sysfs wattage and the joined per-pack cycle
format, which the dead-pack scenario alone never exercised.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 14, 2026 02:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

dhh and others added 2 commits August 13, 2026 23:05
… dead

A dead pack contributes zero wattage and its -1 cycle count already fails
the digit check, so the live/present split and its fallback bought nothing.
Excluding scope=Device supplies keeps wireless peripherals out of the
machine's numbers, which the old BAT* glob did by accident.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Summing only the packs that report would understate the machine's draw;
UPower's laggier aggregate beats a wrong instantaneous number.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 14, 2026 06:16
@dhh

dhh commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Pushed two commits on top:

  • Treat every system battery as one pool — the live/dead pack split and its fallback array bought nothing: a dead pack contributes 0 W to the sum and its -1 cycle count already fails the digit check. Replaced with one loop that also excludes scope=Device supplies, keeping wireless mice out of the machine's numbers (the old BAT* glob did that by accident).
  • Fall back to UPower's aggregate rate when a pack has no power telemetry — summing only the packs that report would understate the machine's draw.

Both scenarios are locked in the dual-battery test; full shell suite green.

— 🤖 Claude, posting on behalf of @dhh

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

@geirolafs

Copy link
Copy Markdown
Author

Ah, much simpler 😄 Thanks!

@ujo4eva

ujo4eva commented Aug 14, 2026

Copy link
Copy Markdown

This PR also fixes a related single-battery bug I just filed as #6895: on HP/ACPI laptops the power_now attribute exists and passes [[ -r ]], but read() fails with ENODEV, so $(<power_now) is empty and the widget shows 0W while UPower's energy-rate is correct (~16-30W). Your new metering logic ([[ $microwatts =~ ^-?[0-9]+$ ]] || continue) already discards the bad read and falls back to UPower's value, so this fixes it as a side effect.

Could you add a regression test for the ENODEV-on-read case (attribute present, read returns empty/invalid)? The existing fixture only covers the success path. Repro and root cause are in #6895.

Some ACPI firmware exposes sysfs attributes whose read() fails with
ENODEV, so a readable power_now or current_now can yield an empty
string. The metering loop already discards an empty power_now, but awk
coerced an empty current_now to 0, which passed the digit check and
reported 0W as a metered value. Validate the raw reads before the math
and lock both empty-read fallbacks in the dual-battery test (omacom#6895).
Copilot AI review requested due to automatic review settings August 15, 2026 00:11
@geirolafs

geirolafs commented Aug 15, 2026 •

Copy link
Copy Markdown
Author

Added the test. while writing it I found the current_now/voltage_now fallback had the same hole: awk coerces the empty read to 0, which passes the digit check and gets counted as a metered 0W. fixed that too and locked both empty-read cases in the dual-battery test.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

UPower's composite DisplayDevice sums every present battery's
energy-full, so a dead pack that still advertises the capacity it once
had drags the percentage toward half and stretches time-to-full toward
ten hours while the live pack sits near full (omacom#7916). Split packs by
electrical liveness — voltage, stored energy, or current flow, since a
dead pack keeps advertising energy_full — and rebuild percentage,
capacity, and remaining time from the live packs' own energy readings
whenever a dead pack was excluded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9xdoAKqs6u53PAahM88u
@geirolafs

Copy link
Copy Markdown
Author

Status: no conflicts against quattro, shell suite green. Two additions since the simplification pass:

One known gap, out of scope here: the panel icon fill reads UPower.displayDevice directly in QML, so on a #7916-shaped machine the glyph still sags even with this merged. #7917's Panel.qml changes cover that side and would compose on top of this.

@hegge25

hegge25 commented Aug 26, 2026

Copy link
Copy Markdown

Verified 3c40745 against the firmware it's meant to handle — nobody had run this branch on a machine that actually fails the read, so here's a data point from one.

HP Spectre x360 14-ea0, single pack, BAT0. power_now is present and world-readable and fails the read with ENODEV; there is no current_now at all, so the fallback branch can't rescue it either:

$ cat /sys/class/power_supply/BAT0/power_now
cat: /sys/class/power_supply/BAT0/power_now: No such device
$ ls /sys/class/power_supply/BAT0/current_now
ls: cannot access '/sys/class/power_supply/BAT0/current_now': No such file or directory

Same moment, packaged script vs this branch at cbe0705:

rate
packaged omarchy-battery-status (4.0.0-1) 0W
this branch 8.3W
upower -i energy-rate 8.27621 W

The single-pack path works out as intended: voltage_now reads non-zero so the pack counts as live, the power_now read fails the digit check and continues, metered_packs stays 0, and power_rate_raw keeps UPower's rate rather than a metered 0W. Percentage, capacity, time and cycles are unchanged from the packaged script.

Two other affected machines reported the identical failure on #7182 and #6895 — an HP Spectre x360 14-ef2xxx (BAT1) and an HP Pavilion 15-dk0xxx (BAT1) — so this looks like an ACPI firmware class rather than one board.

I'd opened #7182 for the single-battery case before this commit existed; since this covers it, I'm closing that in favour of this PR so there's only one to review.

@geirolafs

geirolafs commented Aug 26, 2026 •

Copy link
Copy Markdown
Author

@hegge25 Thank you for running the branch on the firmware it was actually written for, 3c40745 was built against #7182's report, not a machine I own, so this rate table and the code-path confirmation are exactly the verification it was missing. And I appreciate you closing #7182 in favour of this one; that makes for a much easier review.

@geirolafs

Copy link
Copy Markdown
Author

Summary for review, since the thread has grown long:

@FPGAwesome

Copy link
Copy Markdown

Tested this on a ThinkPad T480 with BAT0 + BAT1, running Omarchy 4.0.2-1.

Before this PR, BAT0 was at 77% and holding while BAT1 was at 13% and charging. UPower’s DisplayDevice correctly showed the combined state as 42.77%, 20.39/47.67 Wh, charging at 16.774 W. The battery popup was only showing BAT0: 77%, holding, 0 W, and 22 Wh.

After applying the PR, I tested again with BAT0 at 77% and BAT1 at 28%. DisplayDevice showed 50.79%, 24.21/47.67 Wh, charging at 17.084 W. The popup showed 50%, 47 Wh, charging at 17.1 W, 1h 23m to full, and both cycle counts (50 / 122).

So this fixes the multi-battery aggregation issue on my T480. The focused battery tests also passed before and after the live test. I tested while charging, but didn’t get a discharging sample. PASS.

@landsman

Copy link
Copy Markdown
Contributor

Tested cbe0705 on a ThinkPad T480 (20L6S0CE41) with both packs, internal BAT0 (22 Wh) + external BAT1 (57 Wh), running Omarchy 4.0.3-1 on kernel 7.2.3-arch1-3. Works as expected.

Before, the power panel showed only BAT0: 100%, fully-charged, 0W, 22Wh, cycles 42, while BAT1 was charging at 90%.
After, it matches UPower's DisplayDevice: 99%, charging, 3.1W, 79Wh, 5m to full, cycles 42 / 79. Tested while charging only.

image

@bjarneo

bjarneo commented Sep 20, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #10392, which covers the same battery-status multi-battery aggregation. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 20, 2026
@ujo4eva

ujo4eva commented Sep 20, 2026

Copy link
Copy Markdown

@bjarneo — fair call that #6845 had scope creep by the end, it started as one dual-battery fix (dead-first T480 pinning at 0%) and picked up two more failure modes along the way, so I understand why the duplication check flagged it against #10392.

That said, #10392 isn't a full duplicate, it's a subset — it only switches percentage/state to DisplayDevice when pack_count > 1. It doesn't cover:

Happy to help un-mess this: I can open a dedicated minimal PR just for the #6895 ENODEV guard (single-battery, tiny diff, easy to review) and leave the multi-battery aggregation to #10392 / a rebased follow-up — or go whatever way you prefer. Just let me know which split you'd like.

@bjarneo

bjarneo commented Sep 20, 2026

Copy link
Copy Markdown
Member

Hey. Yes. That would be nice. Trying to untangle all the PRs, so some might be not 100% correctly closed!

@ujo4eva

ujo4eva commented Sep 20, 2026

Copy link
Copy Markdown

@bjarneo — split it out as requested: #12649 covers just the #6895 ENODEV guard (single-battery, numeric-read check + UPower fallback, with regression tests for both empty-read cases). Minimal diff, tests green. Leaving multi-battery aggregation to #10392.

landsman added a commit to landsman/config that referenced this pull request Oct 4, 2026
landsman added a commit to landsman/config that referenced this pull request Oct 4, 2026
…e, bar plugins, power panel (#128)

* devops(arch): keep the display at scale 1 in monitors.lua, Omarchy now reads Lua and ignores monitors.conf

Omarchy moved its Hyprland config to Lua, and hyprland.lua only requires the
.lua files, so monitors.conf here is no longer loaded. Auto-scaling put the
T480's 14" 1080p panel at 1.5; scale 1 fits more on screen.

* docs(t480): name the Omarchy install T480-omarchy, so the booted OS shows in a prompt

* docs(t480): give each OS its own hostname, one machine should not look like one install

* devops: set each install's hostname with make hostname, so a new install does not depend on reading the docs

* devops(arch): count both T480 batteries in the power panel, cherry-picked from omacom/omarchy#6845 until it ships

* devops(arch): keep the System Pulse bar patch in the repo, separators and GB survive a reinstall

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* devops(arch): install the shell plugins with make plugins, so the list is a script and not README steps

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Power panel reports dead BAT0 (0% / 0W / Holding) on dual-battery ThinkPads Power panel shows 0W discharging when power_now read returns ENODEV

8 participants