Files
bambuddy/backend/app/utils/ams_humidity.py
maziggy 6e8d543d2a fix(ams): stop showing the humidity drop index as a percentage (issue #3140)
Bambu sends two humidity fields that are not the same quantity.
humidity_raw is relative humidity in percent; humidity is a 1-5 drop
index, and it runs the other way -- OpenBambuAPI's push_info sample
pairs "humidity:30%" with "humidity_idx:4", so a high index means dry
where a high percentage means wet.

Four call sites used the index whenever no percentage arrived. A unit
sending only the index therefore rendered as "2%" in the green band
while being the second-wettest of the five steps, charted an average of
index values as a percentage, and sat under every humidity threshold
forever, since no index can reach one -- the alarm and auto-drying could
not fire for such a unit at all.

- utils/ams_humidity: one leaf helper, a percentage or None. The index
  is never converted; None is what every caller already handles.
- routes/printers, printer_manager, print_scheduler, main, bambu_mqtt:
  all five readings go through it, so the card, the websocket, the
  chart, the alarm and auto-drying cannot answer differently.
- main: a unit that reports the index and no usable percentage says so
  once per unit in the log, with its firmware versions requested. No
  supported printer is known to do this, and "known" is doing work
  there -- the alternative is a card that goes blank with no trace.

Three faults found while checking what else those paths touched:

- main: humidity_raw=float(x) if x else None stored NULL for a numeric
  0% while writing 0.0 to humidity on the same row.
- main: that same expression was unguarded, unlike the parse above it,
  so a non-numeric humidity_raw raised inside record_ams_history and
  aborted the pass for every printer, not just the one that sent it.
- routes/ams_history: the averages were tested for truthiness, so a
  window averaging exactly 0 reported no average while the min and max
  beside it reported 0.0.

An affected unit now reports no humidity rather than a number that means
the opposite: the indicator is hidden, the chart leaves a gap, the alarm
and auto-drying skip the unit. Temperature is untouched. Auto-drying's
outcome is unchanged either way -- an index could never cross the
threshold -- so only the intent moves.

No supported printer is known to be affected; the report came from an
install running X1Plus, which Bambuddy does not support. Verified
against 7927 recorded samples from seven AMS units including an AMS-HT:
not one used the fallback. Two percentages that did fall through to the
index no longer do -- a reading with a decimal point, and "38.0", which
int() rejected.
2026-09-24 11:59:30 +02:00

41 lines
1.7 KiB
Python

"""Shared reading of an AMS unit's humidity.
Bambu sends two humidity fields and they are not the same quantity.
``humidity_raw`` is relative humidity in percent. ``humidity`` is a 1-5 drop
index, and it runs the other way: OpenBambuAPI's push_info sample carries both
in one line -- ``ams0 temp:18.4;humidity:30%;humidity_idx:4`` -- so a high index
means dry where a high percentage means wet.
Falling back from one to the other therefore does not degrade, it inverts.
Index 2 rendered as "2%" reads as the driest a unit can be while the unit is in
fact the second-wettest of the five steps, and no index can ever exceed a
percentage threshold, so the humidity alarm and auto-drying silently never fire
for such a unit (#3140). A unit that reports no percentage has no percentage:
this returns ``None``, which every caller already treats as "no reading" -- the
card hides the indicator, the alarm and auto-drying skip the unit, and the
history chart leaves a gap.
Kept as a leaf module like ``ams_drying``: nothing here imports from the app.
"""
from collections.abc import Mapping
from typing import Any
def ams_humidity_percent(ams_data: Any) -> float | None:
"""Relative humidity in percent for one AMS unit, or ``None``.
``None`` covers every case where the unit did not report a usable
percentage, including the units that send only the 1-5 index -- which is
deliberately never converted. See the module docstring.
"""
if not isinstance(ams_data, Mapping):
return None
raw = ams_data.get("humidity_raw")
if raw is None:
return None
try:
return float(raw)
except (TypeError, ValueError):
return None # Unparseable reading — not a licence to use the index