feat: Spoolman filament tracking integration #65
Reference in New Issue
Block a user
No description provided.
Delete Branch "p2l/KX-Bridge-Release:feature/spoolman"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds optional Spoolman integration for live filament consumption tracking, mirroring what Moonraker's built-in
[spoolman]support does for Klipper printers.How it works
The printer reports a
supplies_usagefield in everyprint/reportMQTT message — a cumulative extrusion counter in mm, reset each print. This was discovered during development by logging raw MQTT payloads and cross-checking against the slicer's own estimate (within ~1%). It is the direct equivalent of Klipper'sprint_stats.filament_usedand gives accurate consumption for both completed and cancelled prints with no estimation needed.Filament length is reported to Spoolman via
PUT /api/v1/spool/{id}/usewithuse_length(mm), letting Spoolman convert to weight using the spool's own filament profile density.Config
Add to
config.ini:Or via env vars:
SPOOLMAN_SERVER,SPOOLMAN_SYNC_RATE.UI
New API endpoints
/kx/spoolman/status/kx/spoolman/spools/kx/spoolman/active-spool{"slot_map": {"0": 42}}Notes
supplies_usageand therefore in the Spoolman report — this matches Moonraker's behaviour.Known limitation: multi-colour filament split
For multi-slot (multi-colour) prints,
supplies_usageis a single aggregate counter, so the current implementation splits total consumption equally across all mapped spools.Moonraker does not need to approximate here: Klipper tracks each extruder motor independently, so each spool receives its real measured value.
However, the data to do this properly is already in the MQTT stream.
multiColorBox/reportexposesloaded_slot— which slot is currently loaded in the toolhead — and this changes each time the AMS switches during a multi-colour print. By accumulatingsupplies_usagedeltas against theloaded_slotvalue at each poll tick (every ~3 seconds), per-slot consumption can be tracked with only small inaccuracy at tool-change boundaries. Thefeed_statusfield even signals when a loading/unloading transition is in progress, which could be used to pause accumulation during purges to avoid attributing them to the wrong slot.This incremental per-slot tracking is the intended improvement path for a follow-up, and would bring multi-colour accuracy close to what Klipper provides.
Sounds very good . I will check Monday.
Replace equal-split with poll-based per-slot accumulation using loaded_slot from multiColorBox/report: - _spoolman_attribute_tick(activity_map): called each poll cycle after both print/report and multiColorBox/report are processed. Attributes the supplies_usage delta to whichever slot is currently loaded. Skips attribution during loading/unloading transitions (tool changes + purges) so filament consumed during a slot swap is not charged to the wrong spool. - _spoolman_unreported(): returns {slot_idx: mm} not yet sent to Spoolman. Uses per-slot data when available, falls back to equal split for single-extruder/no-AMS setups where _ams_loaded_slot stays -1 throughout the print. - _spoolman_report(): shared fire-and-forget sender used by both notify_end and sync_midprint, eliminating duplicated loop logic. Per-slot state (_spoolman_slot_usage, _spoolman_slot_reported, _spoolman_last_usage) is reset at print start and when spool assignments change. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>Merged — thank you @p2l! This is a clean and well-thought-out implementation. The
supplies_usageMQTT field maps perfectly to what Moonraker does for Klipper, and the per-slot attribution vialoaded_slotdelta ticks is the right approach for multi-colour accuracy.One minor thing for a follow-up: the
import requestscall inside_req()could be moved to the module top level, though Python caches imports so it has no real runtime cost.Will ship in the next release.