Index in SET_LED_TEMPLATE gcode is shifted

Basic Information:

Printer Model: Custom
MCU / Printerboard: SKR Pico

Describe your issue:

Hi everyone,

I have been trying to set up the LED template to indicate the print process and notice a strange behavior. When using SET_LED with index (ex. index=1), the first LED lights up. But when using SET_LED_TEMPLATE with the same index=1, the last LED lights up. And so on with other indexes.

I have searched everywhere and found no information about it. So, I look into the code and find in the LED.py file, when using SET_LED, the index is passed to the “_set_color” method (unchanged), and in the “_set_color” method, the index is -1 to set the value of the “led_state” list. So that’s the basics.

The issue is, when using SET_LED_TEMPLATE command, the “set_template” method is called in line 95, and index is -1 when passing to “tcallbacks”, and then when it comes to the “_set_color” method later, the index is -1 again. So basically the index is -2, and that explains the behavior observed when using the SET_LED_TEMPLATE, as described above.

Is that something necessary, or am I missing something here?. I’m just starting to learn coding, so I don’t have deep knowledge about it, so correct me if i’m wrong. Thanks.

I have also noticed this after an Klipper update and my LED templates for my toolhead and display was messed up. I have macros that set both static colors and templates according to machine states and printing status. I “fixed” it by changing my index values in my macros but that’s not a real fix.

The faulty index was intruduced on Sep 30, 2024 where the LED template was “Generalized” and two changes commits made (3358295 and 8a7a395). I am not sufficient in reading code to be able to find where the index gets wrong, good thing you have.

Maybe not many users are using LED lemplates to have notice the bug. I was actually about to submit a bug report last week but never got off. I was unsure about the klippy log since the LED templates doesn’t log anything.

Thank you for providing the commit hash.

After commit (3358295), the SET_TEMPLATE is being moved to the same class as the SET_LED, and in the old code, the index indeed does not change (it is not -1). I don’t know if it is necessary to work with other functions (still have to look further into that).

Right now, I’m just shifting the index in the macro as you did and waiting to see if that will be fixed in the future.

If you make this change:

--- a/klippy/extras/led.py
+++ b/klippy/extras/led.py
@@ -21,7 +21,7 @@ class LEDHelper:
         self.led_state = [(red, green, blue, white)] * led_count
         # Support setting an led template
         self.template_eval = output_pin.lookup_template_eval(config)
-        self.tcallbacks = [(lambda text, s=self, index=i:
+        self.tcallbacks = [(lambda text, s=self, index=i+1:
                             s._template_update(index, text))
                            for i in range(led_count)]
         # Register commands

does it improve the results?

-Kevin

EDIT: I committed that change (commit 037377b9). Hopefully that fixes the issue.

1 Like

Yes, happy to report that the led template index now works like intended.
Thank you.