Trying to implement a loop using delayed gcode

I’m using this test macro below to try and understand how can I implement loops, the goal of this macro is to lower the toolhead until the probe is activated, but it keeps lowering even if the probe is triggered, I would be grateful if you can help me in finding the problem and point me to a possible solution.
klippy.log (1.2 MB)

[gcode_macro TEST]
gcode:
# Check if XYZ axes are not homed and perform homing if required
{% if printer.homed_axes != ‘XYZ’ %}
RESPOND MSG=“Printer Not Homed, Homing …”
G28 # Homing XYZ axes
{% endif %}

# Loop until the probe is triggered or a certain number of iterations is reached
UPDATE_DELAYED_GCODE ID=find_trigger DURATION=0.1

[delayed_gcode find_trigger]
gcode:
QUERY_PROBE

# Check if the probe is triggered
{% if printer.probe.last_query == 'TRIGGERED' %}
    RESPOND MSG="Probe triggered STOPPING!!!"
    UPDATE_DELAYED_GCODE ID=find_trigger DURATION=0 # Stop checking probe status
{% elif printer.probe.last_query != 'TRIGGERED' %}
    RESPOND MSG="lowering ..."
    G91                        # Set relative positioning mode
    G1 Z-0.1 F300              # Move the toolhead down by the defined depth
    UPDATE_DELAYED_GCODE ID=find_trigger DURATION=1 # Continue checking probe status after a delay of 0.1 seconds
{% endif %}

Unfortunately due to the way jinja is processed, you can’t QUERY_PROBE and then check its state in the same macro, the printer.probe.last_query reads the value at the start of the macro, before your QUERY_PROBE command is executed.

This is because all the jinja expressions are evaluated at the start of the macro, regardless of what gcode might be ahead of them in the actual macro code.

Sometimes you can get around this by using a second macro. Try something like this:

[delayed_gcode find_trigger]
gcode:
    QUERY_PROBE
    
    # Check if the probe is triggered
    check_probe

[gcode_macro check_probe]
gcode:
    {% if printer.probe.last_query == 'TRIGGERED' %}
        RESPOND MSG="Probe triggered STOPPING!!!"
        UPDATE_DELAYED_GCODE ID=find_trigger DURATION=0 # Stop checking probe status
    {% elif printer.probe.last_query != 'TRIGGERED' %}
        RESPOND MSG="lowering ..."
        G91                        # Set relative positioning mode
        G1 Z-0.1 F300              # Move the toolhead down by the defined depth
        UPDATE_DELAYED_GCODE ID=find_trigger DURATION=1 # Continue checking probe status after a delay of 0.1 seconds
    {% endif %}

Take note how the QUERY_PROBE command would be executed, then the check_probe macro is run, hopefully allowing the state change to be updated before you check it in your jinja if statement.

Thank You very much I will check it as soon as possible!

Also I forgot to mention, you can do actual loops in jinja as well:

{% for boops in range(1, (count + 1)) %}
    # Do something
{% endfor %}

Not sure it really helps in your case, but it’s good to know about at least.

Actually I started with for loop and wrote a macro but gave up after I found that I can’t stop the Jninja2 for loop by any means
this is what I wrote:

[gcode_macro Test]
gcode:
# Check if XYZ axes are not homed and perform homing if required
{% if printer.homed_axes != ‘XYZ’ %}
G28 ; Homing XYZ axes
{% endif %}

# Loop until the probe is triggered or a certain number of iterations is reached   
{% for i in range(999) %}
  {% if printer.query_probe == 'OPEN' %}
    RESPOND MSG="No Probe trigger lowering ..."
    G91                      ; Set relative positioning mode
    G1 Z-0.1 F300            ; Move the toolhead down by the defined depth
  {% endif %}
  {% if printer.query_probe == 'TRIGGERED' %}        # Check if the probe was triggered
    RESPOND MSG="Probe triggered STOPPING!!!"
    {% set i =  999 %}         
  {% endif %}        
{% endfor %}

Yeah that is probably being affected by the same issue, the jinja won’t see any change to the probe/endstop that happens during the macro.

Also printer.query_probe is not a valid object, it should be printer.probe.last_query

I tried this suggestion and still not activating unfortunately, will try my luck with for loop with the format you suggested again and see what gonna happen

I found the problem with the macro and this is the current version

[gcode_macro test]
gcode:
# Check if XYZ axes are not homed and perform homing if required
{% if printer.homed_axes != ‘XYZ’ %}
G28 ; Homing XYZ axes
{% endif %}

{% set step = 0 %}
{% for step in range(999) %}
    QUERY_PROBE 
    _Find_Trigger
{% endfor %}

[gcode_macro _Find_Trigger]
gcode:
{% if printer.probe.last_query %} # Check if the probe was triggered
RESPOND MSG=“Probe triggered STOPPING!!!”
SET_GCODE_VARIABLE MACRO=Auto_Z_Offset VARIABLE=step VALUE=999
{% elif not printer.probe.last_query %}
G91 ; Set relative positioning mode
G1 Z-0.1 F300 ; Move the toolhead down by the defined depth
{% endif %}