# Indirectly indexing through extruder objects

**URL:** <https://klipper.discourse.group/t/indirectly-indexing-through-extruder-objects/16869>\
**Category:** General Discussion\
**Created:** [June 5, 2024, 10:24pm UTC](https://klipper.discourse.group/t/indirectly-indexing-through-extruder-objects/16869 "2024-06-05T22:24:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trxgt05](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trxgt05](https://klipper.discourse.group/u/trxgt05)\
**Post date:** [June 5, 2024, 10:24pm UTC](https://klipper.discourse.group/t/indirectly-indexing-through-extruder-objects/16869/1 "2024-06-05T22:24:33Z")

</div>

I’m have a tool changing printer with multiple extruders that I’m trying to write a macro that dynamically stores the current temperature of a given extruder when called. I know this can be done with an if statement and already have it working with something like this:

```auto
{% if tool == "0" %}
   SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder.temperature}
{% elif tool == "1" %}
  SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder1.temperature}
{% elif tool == "2" %}
  SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder2.temperature}
{% endif %}

```

I’ve been trying to clean up code and not have to write so many cases for such things and I’ve found I can call macros dynamically like the one below will call a macro called macro2

```auto
[gcode_macro test1]
variable_index:2
gcode:
  macro{index}

```

The trouble I’m having is being able to do something like that with objects doesn’t work not matter what I try. None of the attempts in the example will work and I get syntax errors. Is this even possible?

```auto
[gcode_macro test2]
variable_tool: 1
variable_curr_temp: 0
gcode:
  #first attempt was to combine it all
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={printer.extruder{tool}.temperature}

#1st attempt to break it down but I believe it was actually trying to store "printer.extruder1.temperature" into the variable
  {% set ext_name = "extruder"~tool %}
  {% set val = "printer."~ext_name~".temperature" %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={val}

  #tried again but I think it was basically the same as above
  {% set ext_name = "extruder"~tool %}
  {% set val = "printer."~ext_name~".temperature" %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE=val

  #I also tried is this way thinking I might get somewhere but it still wouldn't compile
  {% set ext = printer.extruder{tool} %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={ext.temperature}

```

Like I said, I know I can do it with IF statements, but I want to find a way to write it such that a case does not have to be made for every extruder and thus more universal

---

<div class="post-metadata">

**Author:** ![trxgt05](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trxgt05](https://klipper.discourse.group/u/trxgt05)\
**Post date:** [June 6, 2024, 4:56pm UTC](https://klipper.discourse.group/t/indirectly-indexing-through-extruder-objects/16869/2 "2024-06-06T16:56:03Z")

</div>

Nevermind, I found it. You put the dynamic part in square brackets.

```auto
    {% set ext = "extruder"~tool %}
    SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer[ext].temperature}

```

---

<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:** [July 7, 2024, 2:56am UTC](https://klipper.discourse.group/t/indirectly-indexing-through-extruder-objects/16869/3 "2024-07-07T02:56:38Z")

</div>

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