Jinja has really weird scoping rules which make loops tricky. It can be done though, google “jinja while loop” for some solutions
[gcode_macro SetBaseLine]
BaseLine: 0
gcode:
{% set BaseLine = printer.gcode_move.position.z%}
[gcode_macro TestPoint]
InTolerance: False
gcode:
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].BaseLine-(printer[“gcode_macro ScrewAdjust”].Tolerance)/2 and printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].BaseLine+(printer[“gcode_macro ScrewAdjust”].Tolerance)/2%}
{action_respond_info(“Perfect”)}
{% set InTolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Lower”)}
{% endif %}
{% if printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Higher”)}
{% endif %}
[gcode_macro ScrewAdjust]
Tolerance: 0.01
ScrewPitch: 0.5
counter: 1
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for _ in range(counter, 999999999) %}
PROBE
TestPoint
{%if printer[“gcode_macro TestPoint”].InTolerance != True %}
{%set counter = 9999999999%}
{% endif %}
{% endfor %}
Something with the variables seems to cause problems:
Option ‘baseline’ is not valid in section ‘gcode_macro setbaseline’
Something I noticed is that variables need to be in all lower case, so try “baseline” rather than “BaseLine” and see how that works
[gcode_macro SetBaseLine]
baseline: 0
gcode:
{% set baseline = printer.gcode_move.position.z%}
[gcode_macro TestPoint]
intolerance: False
gcode:
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].baseline-(printer[“gcode_macro ScrewAdjust”].tolerance)/2 and printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].baseline+(printer[“gcode_macro ScrewAdjust”].tolerance)/2%}
{action_respond_info(“Perfect”)}
{% set intolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].baseline %}
{action_respond_info(“Lower”)}
{% endif %}
{% if printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].baseline %}
{action_respond_info(“Higher”)}
{% endif %}
[gcode_macro ScrewAdjust]
tolerance: 0.01
screwPitch: 0.5
counter: 1
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for _ in range(counter, 999999999) %}
PROBE
TestPoint
{%if printer[“gcode_macro TestPoint”].intolerance != True %}
{%set counter = 9999999999%}
{% endif %}
{% endfor %}
Did that but still getting this issue: Klipper reports: ERROR
Option ‘baseline’ is not valid in section ‘gcode_macro setbaseline’
Try this:
[gcode_macro SetBaseLine]
variable_baseline: 0
gcode:
{% set baseline = printer.gcode_move.position.z %}
So I got no error when restarting and using the macro worked (the one u send me). Should I now put a variable_ infront of every variable? Thx btw
I have seen macros where the variables have been declared as you had, but it never worked for me, I really don’t know why, maybe that’s how it was done in an earlier verion. But i’ve always prefixed variable declarations with “variable_”, and it works for me. So, in short, yes ![]()
So restarting after changing the variabels to variable_ helped. But if I start the macro it just keeps looking like this (don’t even homes, directly after entering the macro):
![]()
Won’t do other things like homing when writing G28 in the console. Even Emergency Stop doesn’t work. I need to completly REBOOT.
The code:
[gcode_macro TestPoint]
variable_intolerance: False
gcode:
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].baseline-(printer[“gcode_macro ScrewAdjust”].tolerance)/2 and printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].baseline+(printer[“gcode_macro ScrewAdjust”].tolerance)/2%}
{action_respond_info(“Perfect”)}
{% set intolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>printer[“gcode_macro SetBaseLine”].baseline %}
{action_respond_info(“Lower”)}
{% endif %}
{% if printer.gcode_move.position.z<printer[“gcode_macro SetBaseLine”].baseline %}
{action_respond_info(“Higher”)}
{% endif %}
[gcode_macro ScrewAdjust]
variable_tolerance: 0.01
variable_screwPitch: 0.5
variable_counter: 1
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for _ in range(counter, 999999999) %}
PROBE
TestPoint
{%if printer[“gcode_macro TestPoint”].intolerance != True %}
{%set counter = 9999999999%}
{% endif %}
{% endfor %}
I’ve got a long print running at the moment, but if I get time tomorrow I will load up your macros and see if I can figure out what is going on
OK, I think I have found a problem. This has to do with the Jinja scoping rules that I mentioned earlier. This line:
{%set counter = 9999999999%}
Won’t alter the variable “counter” outside of the if statement, and so the for loop will always go from 1 to 999999999999. I don’t know if this is causing your problem, but it’s a problem nonetheless
(post deleted by author)
But eachtime the loop runs it should use the command PROBE and my macro right?
That’s why I said “I don’t know if this is causing your problem, but it’s a problem nonetheless” ![]()
OK, I got past the hanging error that you were experiencing. The number 99999999999999999999999 was too high, I just set it to 99 and it worked. No doubt that a higher value than 99 will work too, but this will help you get testing. The next problem I had is that no matter how much I adjusted the bed screw, it kept saying “Lower”, I didn’t try to debug that but it seems straight forward.
There were a couple of other changes I made: Firstly, I renamed all gcode macro names to be uppercase separated with underscores, just to keep with convention (ie SetBaseLine => SET_BASE_LINE)
lastly, I changed the line
{% set baseline = printer.gcode_move.position.z %}
to
{% set baseline = printer.probe.last_z_result %}
This means that if you have your probe command set to probe multiple readings and take the average as I do, it will still work.
EDIT: There was one more thing I changed, and that is how I broke out of the for loop. I’m not sure if it will work or not as I wasn’t able to test it, but it seemed a neater way to do it:
[gcode_macro MY_SCREW_ADJUST]
variable_tolerance: 0.01
variable_screwPitch: 0.5
variable_test_range: 99
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SET_BASE_LINE
G1 X{279.91} Y{67.96} F6000
{% for i in range(1, test_range) %}
PROBE
TEST_POINT
{% if printer["gcode_macro TEST_POINT"].intolerance != True %}
{% set i = test_range + 1 %}
{% endif %}
{% endfor %}
Please keep me posted with how you go, I’m kinda invested in this now ![]()
Could you please also post the code you used that always returned “Lower”? Thanks for your help!
Here’s everything I used. I had to add a few lines to get it to work with my probe (euclid).
[gcode_macro SET_BASE_LINE]
variable_baseline: 0
gcode:
{% set baseline = printer.probe.last_z_result %}
MESSAGE MSG="Baseline set to {baseline}"
[gcode_macro TEST_POINT]
variable_intolerance: False
gcode:
{% if printer.gcode_move.position.z>printer["gcode_macro SET_BASE_LINE"].baseline-(printer["gcode_macro MY_SCREW_ADJUST"].tolerance)/2 and printer.gcode_move.position.z<printer["gcode_macro SET_BASE_LINE"].baseline+(printer["gcode_macro MY_SCREW_ADJUST"].tolerance)/2 %}
{action_respond_info("Perfect")}
{% set intolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>printer["gcode_macro SET_BASE_LINE"].baseline %}
{action_respond_info("Lower")}
{% endif %}
{% if printer.gcode_move.position.z<printer["gcode_macro SET_BASE_LINE"].baseline %}
{action_respond_info("Higher")}
{% endif %}
MESSAGE MSG="End of TEST_POINT"
[gcode_macro MY_SCREW_ADJUST]
variable_tolerance: 0.01
variable_screwPitch: 0.5
variable_test_range: 99
gcode:
G28
EUCLID_PROBE_BEGIN_BATCH
DEPLOY_PROBE
G1 X{43.71} Y{67.96} F6000
PROBE
SET_BASE_LINE
G1 X{279.91} Y{67.96} F6000
{% for i in range(1, test_range) %}
PROBE
TEST_POINT
{% if printer["gcode_macro TEST_POINT"].intolerance != True %}
{% set i = test_range + 1 %}
{% endif %}
{% endfor %}
EUCLID_PROBE_END_BATCH


