Macro Help: Get extruded filament length in macro

Basic Information:

Printer Model: Sovol SV08
MCU / Printerboard: Sovol SV08
Host / SBC: Sovol SV08
klippy.log: klippy.log (578.0 KB)

Describe your issue:

I’ve converted my Sovol SV08 to Mainline Klipper and one of the things on my to do list is to reduce filament wastage during a runout. The filament runout sensor (microswitch) is located on the top right of the frame, and when a filament runout occurs, there’s a good 600mm of filament left in the reverse bowden tube running to the toolhead. While it’s not a lot, I would still like the printer to use it up. I’ve considered moving the sensor closer to the toolhead, but the wiring won’t allow for that and the runout sensor housing is bulky and large enough that it would be impractical.

The printer.cfg file defines the filament switch sensor to trigger the PAUSE macro on runout detection, but I would like to have it run custom G-Code using the runout_gcode descriptor.

What I want to do is have a predefined variable that specifies the length of filament remaining after runout is triggered (declaring a global variable runout_filament_length and set it to say 500), and then compare it to the sum of extruder moves since filament runout detection and if the sum of extruder moves gets to be equal to or greater than the value stored in the variable, then run the PAUSE macro and unload any remaining filament. For example if the following G-Code statements are executed after runout is detected, I want to capture the sum total of E moves and compare it to the runout_filament_length variable:

G1 X261.001 Y245.445 E.66627
G1 X260.494 Y245.445 E.01482
G1 X277.125 Y262.077 E.68723
G1 X277.125 Y262.584 E.01482
G1 X259.987 Y245.445 E.70818
G1 X259.48 Y245.445 E.01482
G1 X277.125 Y263.091 E.72914
G1 X277.125 Y263.598 E.01482
G1 X258.973 Y245.445 E.75009
G1 X258.466 Y245.445 E.01482
G1 X277.125 Y264.105 E.77105
G1 X277.125 Y264.612 E.01482
G1 X257.958 Y245.445 E.792

In this case, the sum total of E moves would be 5.19288. Since it is less than 500, the g-code would continue to execute and the E moves would continue to be tallied until such time as the sum total equals or exceeds 500, at which point, a PAUSE and UNLOAD_FILAMENT would be executed.

My question is how do I capture the extruder moves in the macro and keep a tally to compare to the remaining filament length?