# Accessing Updated Macro Variable Values

**URL:** <https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676>\
**Category:** General Discussion\
**Created:** [March 27, 2025, 1:56am UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676 "2025-03-27T01:56:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mykepredko](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/mykepredko/32/2085_2.png) [@mykepredko](https://klipper.discourse.group/u/mykepredko)\
**Post date:** [March 27, 2025, 1:56am UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/1 "2025-03-27T01:56:16Z")

</div>

### Basic Information:

Printer Model: All  
 MCU / Printerboard: All  
 Host / SBC: All  
 klippy.log: N/A for now

As part of my functional test code macro, I want to keep track of the executed test steps and to do this, I wanted to use a counter.

I couldn’t find an example and I seem to remember somebody saying that if you are updating a macro variable, I had to do it in another macro.

The current code I’m working with is:

```auto
[respond]
default_type: echo

[gcode_macro test_number] ### Executing Test Number
variable_testnumber: 1 
gcode:

[gcode_macro run_increment_test_number]
gcode:
   RESPOND TYPE=command MSG="Current Test Number {printer["gcode_macro test_number"].testnumber}"
   increment_test_number
   RESPOND TYPE=command MSG="Test Number after increment {printer["gcode_macro test_number"].testnumber}"

[gcode_macro increment_test_number]
gcode:
   SET_GCODE_VARIABLE MACRO=test_number VARIABLE=testnumber VALUE={ printer["gcode_macro test_number"].testnumber + 1 }

```

Now, when I execute “Run Increment Test\_Number” for the first time and look in the console window, I get:

 ![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/3X/e/1/e1405ec729d17bd198ca8f9c118866282439bda6.png)

Which seems to indcate that the `testnumber` variable isn’t updated.

But, when I execute the macro again, I get:

 ![image](https://global.discourse-cdn.com/free1/uploads/klipper/original/3X/7/e/7ee1e113bc79719bd0eb94a1a48c705526907ae6.png)

It appears that the `testnumber` variable was executed but, when accessing it within the macro, it doesn’t appear to be updated.

Anybody have any hints for me?

Thanx!

---

<div class="post-metadata">

**Author:** ![garethky](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/garethky/32/956_2.png) [@garethky](https://klipper.discourse.group/u/garethky)\
**Post date:** [March 27, 2025, 2:17am UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/2 "2025-03-27T02:17:30Z")

</div>

Yep! Macros are not programs they are templates.

The first macro is templated before any of the calls to the second macro ever run. If you were thinking of it like a program: non of the sub routines are called before the main routine exits.

Make a third macro that prints the number and call it.

You can look at what I recently did in [here](https://klipper.discourse.group/t/z-thermal-adjust-for-nozzles/22457). It’s capturing data in a loop and then calculating a result.

---

<div class="post-metadata">

**Author:** ![mykepredko](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/mykepredko/32/2085_2.png) [@mykepredko](https://klipper.discourse.group/u/mykepredko)\
**Post date:** [March 27, 2025, 5:44am UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/3 "2025-03-27T05:44:23Z")

</div>

Thanx, I think I understand it now.

I changed my macro code to:

```auto
[gcode_macro test_number] ### Executing Test Number
variable_testnumber: 1 
gcode:

[gcode_macro increment]
variable_original: 0
variable_updated: 0
gcode:
   RESPOND TYPE=command MSG="Current Test Number { printer["gcode_macro test_number"].testnumber }"
   {% set original = printer["gcode_macro test_number"].testnumber %}
   RESPOND TYPE=command MSG="Original Test Number { original }"

   SET_GCODE_VARIABLE MACRO=test_number VARIABLE=testnumber VALUE={ printer["gcode_macro test_number"].testnumber + 1 }

   {% set updated = printer["gcode_macro test_number"].testnumber %}

   RESPOND TYPE=command MSG="Updated Test Number { updated } <== Should not be changed from the 'Original'"
   {% if original == updated %}
     RESPOND TYPE=command MSG="Test Number NOT Updated"
   {% else %}
     RESPOND TYPE=command MSG="Test Number UPDATED"
   {% endif %}
   print_final_test_number

[gcode_macro print_final_test_number]
gcode:
   RESPOND TYPE=command MSG="Test Number after increment { printer["gcode_macro test_number"].testnumber }"

```

In the `increment` macro, everything after the `{% set ...` statement is me checking to see if the value in the macro changes after the incrementing (as expected, it doesn’t). After these statements, I invoke the separate macro to print the final number.

* * *

The big concern I had was how to structure the macro code so that I could run individual tests and a) stop after the failing one (ie Stop On First Fail) and b) have a value for the test operator/debug technician that indicates which test failed.

Using the knowledge above, I came up with the macro/code format of:

```auto
[gcode_macro bump_number]
variable_bumpnumber: 0
variable_bumpok: 1
gcode:

[gcode_macro bump]
gcode:
   SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpnumber VALUE={ 0 }
   SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpok VALUE={ 1 }
   bump1
   bump2
   bump3
   print_final_bump_number

[gcode_macro bump1]   
gcode:
   {% if printer["gcode_macro bump_number"].bumpok %}
      RESPOND TYPE=command MSG="bump1: Current Bump Number { printer["gcode_macro bump_number"].bumpnumber }"
      SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpnumber VALUE={ printer["gcode_macro bump_number"].bumpnumber + 1 }
#$# Test Code Here
   {% endif %}
   
[gcode_macro bump2]   
gcode:
   {% if printer["gcode_macro bump_number"].bumpok %}
      RESPOND TYPE=command MSG="bump2: Current Bump Number { printer["gcode_macro bump_number"].bumpnumber }"
      SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpnumber VALUE={ printer["gcode_macro bump_number"].bumpnumber + 1 }
#$# Test Code Here
      SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpok VALUE={ 0 } #$# <== Simulate a Test Fail Here
   {% endif %}
   
[gcode_macro bump3]   
gcode:
   {% if printer["gcode_macro bump_number"].bumpok %}
      RESPOND TYPE=command MSG="bump3: Current Bump Number { printer["gcode_macro bump_number"].bumpnumber }"
      SET_GCODE_VARIABLE MACRO=bump_number VARIABLE=bumpnumber VALUE={ printer["gcode_macro bump_number"].bumpnumber + 1 }
#$# Test Code Here
   {% endif %}

[gcode_macro print_final_bump_number]
gcode:
   RESPOND TYPE=command MSG="Bump Number after three simulated tests { printer["gcode_macro bump_number"].bumpnumber }"

```

The test code for each individual test is placed at the `#$#` line in the `bump#` macros. If the test fails the `bumpok` variable is set to `0` so that subsequent tests are not run.

I’m putting this code in here for your comments as well as a reference for anybody else looking to do something similar.

---

<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:** [March 27, 2025, 10:41am UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/4 "2025-03-27T10:41:42Z")

</div>

This is one of the main features of [DynamicMacros](https://github.com/3dcoded/DynamicMacros). By using three empty lines, or a [custom delimeter](https://dynamicmacros.3dcoded.xyz/features/receivingvariables/#custom-delimeters) (highly recommended), you can split the macro and receive variable updates. Note that this doesn’t work inside if statements or for loops.

DynamicMacro version of your macro:

```yaml
[gcode_macro run_increment_test_number]
gcode:
   RESPOND TYPE=command MSG="Current Test Number {printer["gcode_macro test_number"].testnumber}"
   increment_test_number

   RESPOND TYPE=command MSG="Test Number after increment {printer["gcode_macro test_number"].testnumber}"

```

> [@Updating Macros without Restarting Klipper](https://klipper.discourse.group/t/updating-macros-without-restarting-klipper/17663):
>
> I recently created [DynamicMacros](https://3dcoded.github.io/DynamicMacros/), a way to update simple macros without restarting Klipper. It works by having the “dynamic” macros in a separate file, not included in your printer.cfg. Then, a [dynamicmacros] config section references that file. Whenever a dynamic macro is reloaded with the DYNAMIC\_MACRO command, it reads the provided configuration files and executes the macro. This way, you can edit your macros without restarting Klipper. Currently, dynamic macros support the following: Upd…

---

<div class="post-metadata">

**Author:** ![mykepredko](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/mykepredko/32/2085_2.png) [@mykepredko](https://klipper.discourse.group/u/mykepredko)\
**Post date:** [March 27, 2025, 1:58pm UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/5 "2025-03-27T13:58:46Z")

</div>

Thanx for the suggestion.

I’m not going to use this because the manufacturer is going to support the tests and, right now, they’re at their limit with Klipper right now. I don’t want to add any non-incorporated packages into the mix.

The approach I have is modular and, I think, understandable for anybody familiar with programming.

Thanx again for the suggestion.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/64419bf2aac6639e6ef5cef200dcd456b0a01ac3.png) [@system](https://klipper.discourse.group/u/system)\
**Post date:** [April 26, 2025, 11:58pm UTC](https://klipper.discourse.group/t/accessing-updated-macro-variable-values/22676/6 "2025-04-26T23:58:56Z")

</div>

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