# What is the trick to using gcode\_buttons in a macro?

**URL:** <https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804>\
**Category:** General Discussion\
**Created:** [September 14, 2024, 8:44am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804 "2024-09-14T08:44:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 14, 2024, 8:44am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/1 "2024-09-14T08:44:31Z")

</div>

Gday Guys,

I’m trying to measure nozzle offsets (x+,x-,y+,y-,z) using a series of microswitches attached to the bed. I thought that this would be simple task but I have run into the issue of the variables being evaluated at the beginning of the macro execution. I have tried having multiple macros one for querying the switches and another for preforming the step logic but no luck, i have also tried writing to a variable using press\_gcode: but this hasn’t worked either.

Other people seem to be able to do nozzle probing with macros. so what is the trick ?

for info I have a EBB42 board on my heat bed setup as a canbus [MCU] and all the microswitches all query correctly from the console.

regards  
Bryden Quirk

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 14, 2024, 9:51pm UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/2 "2024-09-14T21:51:10Z")

</div>

> I have run into the issue of the variables being evaluated at the beginning of the macro execution.

I recommend using [DynamicMacros](https://github.com/3DCoded/DynamicMacros). It fixes this problem.

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 15, 2024, 5:44am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/3 "2024-09-15T05:44:25Z")

</div>

I gave it a try it installed and setup ok but i still seem to have the same problem.

This is the code im testing it with in my dynamic.cfg file

```auto
[gcode_macro SWITCH_TEST]
gcode:
  {% for step in range(10) %}
  QUERY_BUTTON BUTTON=zswitch
  G4 P2000
      {% if printer["gcode_button zswitch"].state == 'PRESSED' %}
          M118 PRE SSED !!
      {% else %}
          M118 NOT PRES SED !!
    {% endif %}
   {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 15, 2024, 10:47am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/4 "2024-09-15T10:47:32Z")

</div>

If you want a `printer` object to be updated in a Dynamic Macro, you need to have three empty lines before it. See [Receiving Variable Updates - Klipper Dynamic Macros](https://3dcoded.github.io/DynamicMacros/features/receivingvariables/).

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 15, 2024, 11:58am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/5 "2024-09-15T11:58:26Z")

</div>

Please use the `Preformatted Text` feature of the forum editor for your code snippets.

![Format](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/6/643ab9def96b03089cfb0c194628129fcc96b3f8.png)

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 15, 2024, 1:25pm UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/6 "2024-09-15T13:25:22Z")

</div>

ok I finally found the correct combination of spaces, tabs and carriage returns to get kipper to start without an error with this code

```auto
[gcode_macro SWITCH_TEST]
gcode:
  {% for step in range(10) %}
   QUERY_BUTTON BUTTON=zswitch
   G4 P2000     
      {% if printer["gcode_button zswitch"].state == 'PRESSED' %}
      M118 PRE SSED !!
      {% else %}
          M118 NOT PRES SED !!
      {% endif %}
      {% endfor %}

```

except that it seems to have stopped my loop from working correctly  
I assume that this is because the whitespaces are resetting the step variable somehow

I retried using the example loop structure from the recursion section of the dynamic macros documentation [Using Dynamic Macros Features - Klipper Dynamic Macros](https://3dcoded.github.io/DynamicMacros/tutorial/usingfeatures/)

```auto
[gcode_macro COUNT]
gcode:
    {% set num = params.NUM|default(1)|int %}
    {% if num <= 10 %}
    QUERY_BUTTON BUTTON=zswitch
    G4 P2000
          {% if printer["gcode_button zswitch"].state == 'PRESSED' %}
        M118 PRE SSED !!
        {% else %}
        M118 NOT PRES SED !!
        {% endif %}
        COUNT NUM={num+1} # Count up 1
    {% else %}
       RESPOND MSG="Done Counting"
    {% endif %}

```

This however had the odd effect of only ever rehoming the printer and doing nothing more ?.

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 15, 2024, 8:17pm UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/7 "2024-09-15T20:17:31Z")

</div>

DynamicMacros wouldn’t home the printer unless the GCode in a macro says to do that. Maybe your `zswitch` button thinks its being pressed?

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 16, 2024, 12:57am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/8 "2024-09-16T00:57:03Z")

</div>

Oh my its gotten even weirder.

the example code itself without modification causes the printer to home  
code from [Example Macros - Klipper Dynamic Macros](https://3dcoded.github.io/DynamicMacros/examples/)

> Blockquote

```auto
[gcode_macro COUNT]
gcode:
    {% set num = params.NUM|default(1)|int %}
    {% if num <= 10 %}
        RESPOND MSG={num}
        COUNT NUM={num+1} # Count up 1
    {% else %}
        RESPOND MSG="Done Counting"
    {% endif %}

```

it seems the three whitespaces kills internal variables somehow  
and the recursive loop breaks klipper somehow.

Sadly the trick to using buttons inside macros remains elusive.

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 16, 2024, 10:18am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/9 "2024-09-16T10:18:17Z")

</div>

> it seems the three whitespaces kills internal variables somehow

This is a known issue and is solved by using the special `update()` function, described at the botton of [Receiving Variable Updates - Klipper Dynamic Macros](https://3dcoded.github.io/DynamicMacros/features/receivingvariables/).

> and the recursive loop breaks klipper somehow.

This is really strange. I’ve looked through the DynamicMacros code and all I can say is that DynamicMacros won’t home the printer unless there’s a `G28` command in a macro it’s instructed to call.

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 16, 2024, 10:23am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/10 "2024-09-16T10:23:48Z")

</div>

SUCESS!!!

I discovered that the behavior of the macro would vary depending on where in the dynamic.cfg the macro was pasted.

It turns out I had another example macro with a g28 command in it.  
this macro had a space before the “[” so it was " [gcode\_macro DYNAMIC\_MOVE]" insted of “[gcode\_macro DYNAMIC\_MOVE]”  
This was causing some weird behavior

The following code correctly choses its conditional statement depending on the switch’s physical state

> Blockquote  
> [gcode\_macro SWITCH\_TEST]  
> gcode:  
> {% set num = params.NUM|default(1)|int %}  
> {% if num \<= 10 %}  
> QUERY\_BUTTON BUTTON=zswitch  
> G4 P2000  
> {% if printer[“gcode\_button zswitch”].state == ‘PRESSED’ %}  
> M118 PRE SSED !!  
> {% else %}  
> M118 NOT PRES SED !!  
> {% endif %}  
> SWITCH\_TEST NUM={num+1} # Count up 1  
> {% else %}  
> RESPOND MSG=“Done Counting”  
> {% endif %}

Im still getting used to the whitespace syntax. and getting the formatting in a suitable state to run without errors.

Ill come back and update this post with a more polished working example for the benefit of future googlers.

Thankyou for your help kind sir !

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 16, 2024, 10:41am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/11 "2024-09-16T10:41:16Z")

</div>

> Blockquote

is not

`Preformatted Text.`

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 21, 2024, 8:09am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/12 "2024-09-21T08:09:02Z")

</div>

OK So i can sucessfully decend to the z limit switch and stop using the dynamic macro

> Blockquote  
> [gcode\_macro Z\_DEC]  
> gcode:  
> G91  
> {% set num = params.NUM|default(1)|int %}  
> {% if num \<= 100 %}  
> G4 P100  
> QUERY\_BUTTON BUTTON=zswitch  
> {% if printer[“gcode\_button zswitch”].state == ‘PRESSED’ %}  
> {% set num = 100 %}  
> {% else %}  
> G1 Z-0.05  
> #M118 {printer.toolhead.position.z}  
> Z\_DEC NUM={num+1} # Count u  
> {% endif %}  
> {% else %}  
> RESPOND MSG=“Done Decrementing Z”  
> {% endif %}  
> G90

And that stops the Z axis at the correct location but im still having a heck of a time trying to get that Zposition back up to the macro that called it

> Blockquote  
> [gcode\_macro LOOP\_TEST]  
> gcode:  
> {% for step in range(4) %}  
> Z\_DEC  
> M118 {printer.toolhead.position.z}  
> G91  
> G1 Z+3.0  
> G90  
> G4 P3000  
> {% endfor %}

This loop functions but unsupprisingly reports back the z position at the start.  
if i put three carriage returns in like this

> Blockquote  
> [gcode\_macro LOOP\_TEST]  
> gcode:  
> {% for step in range(4) %}  
> Z\_DEC

```
M118 {printer.toolhead.position.z}
G91
G1 Z+3.0
G90
G4 P3000
{% endfor %}

```

I get  
Error loading template ‘LOOP\_TEST’: jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endfor’ or ‘else’. The innermost block that needs to be closed is ‘for’.

it seams that any time i put the three (ive allso tried adding just two incase the existing newline counts) inside a macro that has any jinja expressions it throws that error

I tried using get\_macro\_variables() and SET\_DYNAMIC\_VARIABLE but they allways seem to return blank data as dose update()

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 21, 2024, 8:17am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/13 "2024-09-21T08:17:41Z")

</div>

**PLEASE!**

Use `Preformatted Text` for code snippets, not Blockquote:

![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/8/8a42d83ee1cf17d01aa025e482a72744519102bf.png)

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 21, 2024, 8:31am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/14 "2024-09-21T08:31:38Z")

</div>

what are you talking about ?

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 21, 2024, 8:33am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/15 "2024-09-21T08:33:05Z")

</div>

![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/f/f46af7bea53275b13191d89ed6ace75b076bdbee.png)

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 21, 2024, 8:47am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/16 "2024-09-21T08:47:08Z")

</div>

> [@bquirky](#):
>
> ```auto
> M118 {printer.toolhead.position.z}
> G91
> G1 Z+3.0
> G90
> G4 P3000
> {% endfor %}
> 
> ```

How did you manage to format that?

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 21, 2024, 9:01am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/17 "2024-09-21T09:01:44Z")

</div>

ive got no idea i just pushed the " button and pasted the code in… thats not the whole code snippit either theres suposed to be three caragige returns that form part of the code, honestly i have no idea what the deal is with jinja formatting.

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [September 21, 2024, 9:13am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/18 "2024-09-21T09:13:09Z")

</div>

![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/3/3eca39195bbc8e0f24f32d2f87a0c97c40f4b2e1.png)

You should have the marked icon somewhere when posting. This is essential as otherwise important formatting like indents etc get lost

---

<div class="post-metadata">

**Author:** ![bquirky](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@bquirky](https://klipper.discourse.group/u/bquirky)\
**Post date:** [September 21, 2024, 9:15am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/19 "2024-09-21T09:15:11Z")

</div>

this is what ive got

 ![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/9/9ab1aac8bfbf8c0ed517b642a1e9f063523709c5.png)

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [September 21, 2024, 9:17am UTC](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804/20 "2024-09-21T09:17:29Z")

</div>

This is strange. Is it hidden behind the ![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/c/c315bd03134c7755a3505df6fcabec2c686013fb.png)?

[Next page](https://klipper.discourse.group/t/what-is-the-trick-to-using-gcode-buttons-in-a-macro/18804.md?page=2)
