# Using gcode variables doesn't work

**URL:** <https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530>\
**Category:** General Discussion\
**Created:** [February 9, 2023, 3:47am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530 "2023-02-09T03:47:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://klipper.discourse.group/u/Florian)\
**Post date:** [February 9, 2023, 3:47am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/1 "2023-02-09T03:47:57Z")

</div>

Hi. I am working on a set of macros, were I set a gcode variable in one macro (read as parameter of the macro call and stored in a gcode variable using the SET\_GCODE\_VARIABLE command), so that it can be used in a set of other macros. The intention is that the value of the gcode variable is temporarily saved for use in other macros until I call the macro to set the gcose variable again and potentially change it. After the call to change the gcode variable, the other macros should consider the new value once called.

I attach my code snippet here:

```auto
[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
variable_printer_profile: 0
gcode:
        # Read Profile parameter, standard profile is default
        {% set P = params.Profile|default(1)|int %}

        # Save current printer profile variable to be used across macros
        SET_GCODE_VARIABLE MACRO=SET_PRINTER_PROFILE VARIABLE=printer_profile VALUE={P}

        # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
        IMPLEMENT_PRINTER_PROFILE

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
description: Macro called after printer profile was chosen
gcode:
        # Retrieve active printer profile
        {% set Profile = printer["gcode_macro SET_PRINTER_PROFILE"].printer_profile %}

       .... and so on....

```

Unfortunately, the gcode variable doesn’t change and stays the same as the default value. Just wondering if I misinterpreted how gcode variable work or if my syntax is wrong or if you simply cannot set a gcode variable via a macro parameter. I hope the klipper gods can help 🙂

Cheers  
Florian

---

<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:** [February 9, 2023, 6:13am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/2 "2023-02-09T06:13:47Z")

</div>

You may have a look on this thread:

> [@I'd like to live better with Global Values](https://klipper.discourse.group/t/id-like-to-live-better-with-global-values/6524):
>
> This is an offshoot of getting the homing working on my custom CoreXY printer. As I was working on and testing the macros I kept forgetting that with a CoreXY, you need to change both the A and B steppers with the same values (@Sineos pointed this out to me at least once). This was a pain when you’re working with sensorless homing. So, after a bit of research and playing around, I figured out how to define what are essentially global values in an outside gcode macro and use them in the X and …

---

<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:** [February 9, 2023, 7:36am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/3 "2023-02-09T07:36:50Z")

</div>

I didn’t answer the question directly as I would have thought the best/most proper answer for this question was when `IMPLEMENT_PRINTER_PROFILE` was invoked, parameters from `SET_PRINTER_PROFILE` were passed to it and customize the execution of the gcode\_macro.

Unfortunately, I’m still pretty shaky on writing my own macros, so I didn’t want to put down something that wouldn’t work and my printers are all doing long jobs so I couldn’t test it out before posting. Sorry.

---

<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:** [February 9, 2023, 8:16am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/4 "2023-02-09T08:16:06Z")

</div>

But maybe it helps to find a solution…

---

<div class="post-metadata">

**Author:** ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://klipper.discourse.group/u/Florian)\
**Post date:** [February 9, 2023, 9:39am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/5 "2023-02-09T09:39:34Z")

</div>

Thanks for your comments, very appreciated and great to know that this community sticks together. I guess the main difference to your previous discussions/topics is that I want to assign a parameter of the SET\_PRINTER\_PROFILE gcode macro as printer variable. If I hard code the variable in the macro, I can change the value without issue. passing a parameter to be stored as printer variable is the issue…I am not even sure if this is possible at all. Maybe the data type or default value assignment is the issue…???

---

<div class="post-metadata">

**Author:** ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://klipper.discourse.group/u/Florian)\
**Post date:** [February 9, 2023, 2:50pm UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/6 "2023-02-09T14:50:30Z")

</div>

So, the mystery is solved…Turns out, parameters of macros need to be defined in ALLCAPS.  
I just changed the parameter name from “Profile” to “PROFILE” and, voila…works.

```auto
[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
variable_printer_profile: None
gcode:
        # Read Profile parameter, standard profile is default
        {% set P = params.PROFILE|default(1) %}

        # Save current printer profile variable to be used across macros
        SET_GCODE_VARIABLE MACRO=SET_PRINTER_PROFILE VARIABLE=printer_profile VALUE={P}
                  
        # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
        IMPLEMENT_PRINTER_PROFILE

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
gcode:
        # Retrieve active printer profile
        {% set Profile = printer["gcode_macro SET_PRINTER_PROFILE"].printer_profile %}

        .... and so on....

```

In case any of you have a similar use case, beware of capitals 😛

---

<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:** [February 9, 2023, 5:29pm UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/7 "2023-02-09T17:29:30Z")

</div>

One of my print jobs finished, so I took a few moments to look at this.

This is the way that I would implement the functionality set out in the operation of the `SET_PRINTER_PROFILE`/`IMPLEMENT_PRINTER_PROFILE` macros:

```auto
# 2023.02.09 - Macro Testing
[respond]

[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
gcode:
    # Read Profile parameter, standard profile is default
    {% set P = params.PROFILE|default(1) %}
                  
    # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
    IMPLEMENT_PRINTER_PROFILE PROFILE={P}

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
gcode:
    # Read Profile parameter
    {% set P = params.PROFILE|int %}

    {% if 0 == P %}
        RESPOND TYPE=echo MSG="Silent Profile"
    {% elif 1 == P %}
        RESPOND TYPE=echo MSG="Standard Profile"
    {% elif 2 == P %}
        RESPOND TYPE=echo MSG="Sport Profile"
    {% elif 3 == P %}
        RESPOND TYPE=echo MSG="Ludricous Profile"
    {% else %}
        RESPOND TYPE=echo MSG="UNKNOWN Profile"
    {% endif %}

```

Now, there may be the requirement for saving the “printer\_profile” variable in other macros, but for what’s outlined here in the question, I would think that passing the `PROFILE` parameter would be more appropriate as it more closely models what is considered the “better” programming model of:

```auto
SET_PRINTER_PROFILE( PROFILE Profile ) {

  if ( undefined Profile ) {
    Profile = 1;
  }

  IMPLEMENT_PRINTER_PROFILE( Profile );

}

IMPLEMENT_PRINTER_PROFILE( PROFILE Profile ) {

// Do something with "Profile"

}

```

Rather than the model which is followed by @Florian 's macros and is somewhat “old school”:

```auto
PROFILE Printer_Profile = 1;

SET_PRINTER_PROFILE( PROFILE Profile ) {

  if ( !undefined Profile ) {
    Printer_Profile = Profile;
  }

  IMPLEMENT_PRINTER_PROFILE();

}

IMPLEMENT_PRINTER_PROFILE() {

// Do something with "Printer_Profile"

}

```

The pseudo-code here is a bit clumsy as I’m trying to illustrate how the macros work but I think the point comes across.

---

<div class="post-metadata">

**Author:** ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://klipper.discourse.group/u/Florian)\
**Post date:** [February 10, 2023, 1:19am UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/8 "2023-02-10T01:19:37Z")

</div>

Thanks @mykepredko. I absolutely agree with you that, using parameters for passing one or several variables from one to another macro (or to many other macros), is the cleaner and more straight forward way.

The reason why I went down the SET\_GCODE\_VARIABLE route is because I am using the Profile-Variable to send/alter M220 and M204 commands during runtime. This way, I will have one default speed slicer profile and the print speed is managed on the printer.

Once the whole bunch of macros is finished, it shall allow switching speed profiles at runtime, similar to what my Bambu Labs X1 Carbin does. My aim is to share all of this once it properly works. Now I am working on switching stealthChop on and off depending on the profile chosen…

Cheers  
Florian

---

<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:** [March 27, 2025, 5:48pm UTC](https://klipper.discourse.group/t/using-gcode-variables-doesnt-work/6530/9 "2025-03-27T17:48:33Z")

</div>


