Skip to content

Report load averages of 0 instead of going unavailable - #152

Open
shauneccles wants to merge 1 commit into
HarvsG:masterfrom
shauneccles:fix/load-avg-zero
Open

Report load averages of 0 instead of going unavailable#152
shauneccles wants to merge 1 commit into
HarvsG:masterfrom
shauneccles:fix/load-avg-zero

Conversation

@shauneccles

Copy link
Copy Markdown

Problem

The load-average sensors' value_fn ended in … and la[N]) or None. When a load value is genuinely 0 — e.g. an idle router's load average — and la[N] evaluates to 0 (falsy), so 0 or None returns None and the sensor goes unavailable instead of reporting 0. All three load sensors (1m/5m/15m) share this.

Fix

Return the indexed value directly when the list is well-formed, falling back to None only when the data is actually missing/malformed:

value_fn=lambda system_status: (
    la[2]
    if isinstance(la := system_status.get("load_average"), list) and len(la) > 2
    else None
),

Validation against real hardware (GL.iNet MT6000, firmware 4.9.0)

Tested in a Home Assistant dev instance connected to a live MT6000, captured at the same condition (an idle router reporting a 0 load average).

Before (this branch's base, master) — router's 15-minute load = 0:

sensor.gl_inet_mt6000_load_avg_15m = unavailable      <-- bug

After (with this fix) — router fully idle, router_get_status() returned load_average: [0, 0.02, 0]:

sensor.gl_inet_mt6000_load_avg_1m  = 0                <-- reports 0 (was unavailable under the bug)
sensor.gl_inet_mt6000_load_avg_15m = 0                <-- reports 0 (was unavailable under the bug)

Deterministic check on the exact failing input:

load_average=[0.06, 0.02, 0]    -> OLD=None    NEW=0      <-- the fix
load_average=[0.05, 0.03, 0.01] -> OLD=0.01    NEW=0.01   <-- no regression
load_average=None               -> OLD=None    NEW=None   <-- still safe
load_average=[]                 -> OLD=None    NEW=None   <-- still safe

Pre-existing bug (present unchanged on master), independent of #150 — raising it on its own as discussed.

The load-average sensors' value_fn ended in `... and la[N]) or None`, so a
genuine value of 0 (e.g. an idle router's 15-minute average) was treated as
falsy and the sensor went unavailable. Return the indexed value directly when
the list is well-formed, only falling back to None when the data is missing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
shauneccles added a commit to shauneccles/ha-glinet4-integration that referenced this pull request Jun 26, 2026
- Load averages of exactly 0 made the sensor go unavailable: the old
  `... and la[i]` short-circuits on the falsy 0. Use an explicit conditional so
  a genuine 0.0 is reported (HarvsG#152).
- Treat buffer/cache memory as available when computing memory usage, matching
  how Linux reports free memory, and expose memory_buff_cache / memory_available
  / memory_used attributes (HarvsG#111).

Co-Authored-By: Claude Opus 4.8 <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.

1 participant