What is the correct way to write and read some strings?

Basic Information:

Printer Model:
MCU / Printerboard:
Host / SBC
klippy.log

Fill out above information and in all cases attach your klippy.log file (use zip to compress it, if too big). Pasting your printer.cfg is not needed
Be sure to check our “Knowledge Base” Category first. Most relevant items, e.g. error messages, are covered there

Describe your issue:

I’m trying to detect which filament sensor (sensor_filamento_0 or sensor_filamento_1) triggered a runout and then use that information in my RESUME macro to purge and resume on the correct extruder. Here’s what I currently have:

# When sensor 0 trips:
SET_GCODE_VARIABLE MACRO=_PRINT_VARIABLE VARIABLE=runout_sensor VALUE='filament_switch_sensor sensor_filamento_0'

# Later in RESUME:
{% if "sensor_filamento_0" in runout_sensor %}
    # purge and resume on extruder 0
{% elif "sensor_filamento_1" in runout_sensor %}
    # purge and resume on extruder 1
{% endif %}

But ChatGPT suggested removing the embedded quotes to make it more robust and readable:

# Store without quotes:
SET_GCODE_VARIABLE MACRO=_PRINT_VARIABLE VARIABLE=runout_sensor VALUE=filament_switch_sensor sensor_filamento_0

# Then the same substring test still works:
{% if "sensor_filamento_0" in runout_sensor %}
    …
{% endif %}

btw this is the variable:

[gcode_macro _PRINT_VARIABLE]
variable_runout_sensor      : ""    # Name of the filament sensor that triggered the error (configured from filament failure macros)

Wich aproach is the better?

Thanks

The version without quotes will throw an error. You need the quotes to indicate the value is all one string. In me experience, ChatGPT is quite bad at Klipper’s implementation of Jinja2.

2 Likes

Thanks!!

Also other question.
I want to know the name of the active extruder.
I have seen the klipper status docs and they give an example: printer[printer.toolhead.extruder].target gives the target ot the active extruder

So… is this correct? I mean, does printer.toolhead.extruder returns a string?

 {% if printer.toolhead.extruder == "extruder" %}
...
{% elif printer.toolhead.extruder == "extruder1" %}
...

Hi @JuanR3D ,

You can quickly test with:

RESPOND MSG="{printer.toolhead.extruder}"

In your macro and see what it outputs.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.