Probe value use in macro

I want to use the value, you get when using the PROBE command in a macro. how do I have to do that?

something like this? Feature request: Run a gcode command and store output in a macro variable or some internal location ¡ Issue #2573 ¡ Klipper3d/klipper ¡ GitHub

personally it can just be grep’d out of the klippy.log
this one greps the last 100lines of klippy.log

tail -n 100 klippy.log | grep “Result”

outputs:
Result is z=-0.017969

for example, but what you do with it?? you can pipe it into another file and read it maybe…

1 Like

I asked a similar question recently, you can find the z_offset by using

{printer.gcode_move.homing_origin.z}

Note that you can’t query that variable in the same macro as you do a G28 or other probe commands, so you have to put the query inside another macro, and call that macro to do the query. Something like this:

[gcode_macro QUERY_ZOFFSET]
gcode:
    MESSAGE MSG="Final Z Offset value: {printer.gcode_move.homing_origin.z}"

Note 2: That variable only includes the changes made to z_offset at runtime, and does not include the value specified in the config file. You can retrieve that value by using something like

{printer.config_file.probe.z_offset} (double check the syntax, I'm going by memory)

Hope this helps

2 Likes

So I tried your solution but it shows 0.0 on the LCD (probably my fault). In Addition I
need the value in the original macro any Idea?

[gcode_macro QUERY_ZOFFSET]
gcode:
m117 {printer.gcode_move.homing_origin.z}

[gcode_macro ScrewAdjust]
gcode:
G28
G1 X{43.71} Y{67.96} F6000
Probe
QUERY_ZOFFSET

I want to write a custom screw tilt adjust. The printer takes one screw as a baseline and then continuously probes on the next point while you turn the screw until the height is near the one of the baseline. You get feedback how much and in which direction you have to turn.

I’m not familiar with the probe command, just I just looked it up in the documentation, and it makes no mention of it changing the z_offset. If you just want the z position, rather than the z_offset, you could try using the MANUAL_PROBE command, and then use {printer.manual_probe.z_position} to find the current Z position

As for getting the value in the original macro, you might be able to use a gcode_macro for a global variable, and storing the value there. I have no idea if it would work or not, but if you give me a little while, I will test it

1 Like

Actually, you can also find the position by using {printer.gcode_move.position.z}

1 Like

Thanks i got the time. I’m really surprised how active this page is. Thanks a lot! for your help.

I actually don’t want to use the MANUAL_PROBE command. Wouldn’t it be possible to use the value u get as a result when using the PROBE command?
image

That’s the Z position, not the offset. You should be able to read the value with {printer.gcode_move.position.z}, but again, in a separate macro. I’m just trying now to see how you might get the value back in the original macro

1 Like

I wasn’t able to get the return value, I had my doubts. Hopefully someone who actually knows what they are talking about will chime in with the correct answer

1 Like

Ok thx

I’m new to this so please don’t cry if i write complete shit. Is it possible to create a macro that returns a value?

It’s all good. We all started from scratch and one point or another. To answer your question, not that I am aware of. That’s why I was trying to store the return value in a global variable, but that didn’t work either. I will keep thinking about it, as I would really like to find a way to do it too.

1 Like

Just so you can imagine better what I want.

2:10

So I found this :


and then wrote this:

[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>[“gcode_macro SetBaseLine”].BaseLine-([“gcode_macro ScrewAdjust”].Tolerance)/2 and printer.gcode_move.position.z<[“gcode_macro SetBaseLine”].BaseLine+([“gcode_macro ScrewAdjust”].Tolerance)/2%}
{action_respond_info(“Perfect”)}
{% set InTolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Lower”)}
{% endif %}
{% if printer.gcode_move.position.z<[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Higher”)}
{% endif %}

[gcode_macro ScrewAdjust]
Tolerance: 0.01
ScrewPitch: 0.5
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for [“gcode_macro TestPoint”].InTolerance != True %}
PROBE
TestPoint
{% endfor %}

G1 X{43.71} Y{310} F6000
G1 X{279.91} Y{310} F6000

but I’m getting this Error. Any Ideas?

Error loading template ‘gcode_macro ScrewAdjust:gcode’: TemplateSyntaxError: can’t assign to ‘list’

So I found this:

and wrote this:

[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>[“gcode_macro SetBaseLine”].BaseLine-([“gcode_macro ScrewAdjust”].Tolerance)/2 and printer.gcode_move.position.z<[“gcode_macro SetBaseLine”].BaseLine+([“gcode_macro ScrewAdjust”].Tolerance)/2%}
{action_respond_info(“Perfect”)}
{% set InTolerance = True %}
{% endif %}
{% if printer.gcode_move.position.z>[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Lower”)}
{% endif %}
{% if printer.gcode_move.position.z<[“gcode_macro SetBaseLine”].BaseLine %}
{action_respond_info(“Higher”)}
{% endif %}

[gcode_macro ScrewAdjust]
Tolerance: 0.01
ScrewPitch: 0.5
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for [“gcode_macro TestPoint”].InTolerance != True %}
PROBE
TestPoint
{% endfor %}

G1 X{43.71} Y{310} F6000
G1 X{279.91} Y{310} F6000

Getting this error:
Error loading template ‘gcode_macro ScrewAdjust:gcode’: TemplateSyntaxError: can’t assign to ‘list’

First off, I know very little about Python or Jinja, but I think that you need to prefix the expression with “printer”

So this:

{% for [“gcode_macro TestPoint”].InTolerance != True %}

Should be this:

{% for printer[“gcode_macro TestPoint”].InTolerance != True %}

That’s how I use it, and how it is referenced in the image you posted above. Without that prefix, I think Jinja interprets the items within the square brackets to be a list

1 Like

[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
gcode:
G28
G1 X{43.71} Y{67.96} F6000
PROBE
SetBaseLine
G1 X{279.91} Y{67.96} F6000
{% for printer[“gcode_macro TestPoint”].InTolerance != True %}
PROBE
TestPoint
{% endfor %}

G1 X{43.71} Y{310} F6000
G1 X{279.91} Y{310} F6000

now i got this error:

Error loading template ‘gcode_macro ScrewAdjust:gcode’: TemplateSyntaxError: expected token ‘in’, got ‘[’

Yes, the syntax for the for loop is wrong, in fact the logic for that section of code is wrong. I can tell you why it is wrong, but I don’t know Python so I don’t know how to fix it.

The problem is that the for loop is expecting a list of values to cycle through, so something like

for a in [1 2 3 4 5]

(I don’t know if that syntax is correct in Python, but that’s sort of how I would do it in Perl) But you have given the for command a scalar value (printer[“gcode_macro TestPoint”].InTolerance) rather than a list., and that is why you are getting that error. I have to go out for a few hours, but I will take a look later and see if I can find the correct syntax for you, but hopefully someone who knows Python / Jinja will reply before then.

Also, as I said, the logic is wrong. Are you trying to store a list of points to be probed somewhere? If so, you need a list or an array, but currently you have a single scalar value. Assuming that you are trying to probe a list of points, you need to change the code to something like this.

{% for point_to_scan in [1 2 3 4] %}
  {% if printer[“gcode_macro TestPoint”].InTolerance(point_to_scan) != True %}
    PROBE
    TestPoint
  {% endif %}
{% endfor %}

Again, syntax will be wrong, but hopefully you will get what I mean

1 Like

Is there something like a while loop in JINJA2? I haven’t got a list so far. I just have these four movement commands to move to the points.