# Math operations with variables in macros?

**URL:** <https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285>\
**Category:** General Discussion\
**Created:** [July 3, 2023, 6:19pm UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285 "2023-07-03T18:19:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Astro](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@Astro](https://klipper.discourse.group/u/Astro)\
**Post date:** [July 3, 2023, 6:19pm UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285/1 "2023-07-03T18:19:18Z")

</div>

Does anyone know the proper syntax for basic math operators when working with variables? I need to create a variable that is based on another variable divided by a number, but am not sure what the syntax is.

Ex:

[gcode\_macro TEST]  
variable\_test1: 3  
variable\_test2: {test1}\*3

This obviously is not working, but I don’t know what it should look like. I need to eventually create a function that relates the speed of one of my steppers to a time and then run another stepper based on that time. I just don’t know the syntax though.

Any help would be greatly appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![NAPCAL](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/napcal/32/1794_2.png) [@NAPCAL](https://klipper.discourse.group/u/NAPCAL)\
**Post date:** [July 3, 2023, 9:23pm UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285/2 "2023-07-03T21:23:14Z")

</div>

This might be helpful  
[https://www.klipper3d.org/Command\_Templates.html](https://www.klipper3d.org/Command_Templates.html)

You may find a few of these examples helpful

> <https://github.com/Klipper3d/klipper/blob/master/config/sample-macros.cfg>

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [July 3, 2023, 11:28pm UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285/3 "2023-07-03T23:28:47Z")

</div>

Also see here: [Template Designer Documentation — Jinja Documentation (2.10.x)](https://jinja.palletsprojects.com/en/2.10.x/templates/#math)

---

<div class="post-metadata">

**Author:** ![DaVinci10](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/davinci10/32/2828_2.png) [@DaVinci10](https://klipper.discourse.group/u/DaVinci10)\
**Post date:** [July 4, 2023, 2:16am UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285/4 "2023-07-04T02:16:31Z")

</div>

Here is a bad example:  
Where I was trying to do some math with setting a pwm 0 - 255 value for some LED’s based on temperature inputs.  
but everytime the temp would exceeds the max temp (overshoot) it would error saying led cant be set to above 1.0 (was going to next use the target instead of max but got busy doin other things than bling 😉

```auto
[gcode_macro turn_temp_leds_on]
gcode:
	# (Update all LEDs value temps)
	# SET_LED LED=chamber_light white={printer["temperature_sensor chamber_temp"].temperature |float / 1024 * 10} sync=0
	# SET_LED LED=lcd_light green={printer.heater_bed.temperature |float / 1024 * 10} sync=0
	# SET_LED LED=PIS_led red={printer.extruder.temperature |float / 1024 * 10} sync=0
    {% set extruder_temp_max = 280 %}
    {% set bed_temp_max = 120 %}
    {% set chamber_temp_max = 60 %}
    set_led LED=PIS_led RED={printer.extruder.temperature|int / extruder_temp_max|int|round(0)|int} sync=0
	set_led LED=lcd_light GREEN={printer.heater_bed.temperature|int / bed_temp_max|int|round(0)|int} sync=0
    set_led LED=chamber_light WHITE={printer["temperature_sensor heater_chamber"].temperature|int / chamber_temp_max|int|round(0)|int} sync=0 
# End Gcode

```

and then I found out LED\_Templates was the way to go with this endever… 😉

---

<div class="post-metadata">

**Author:** ![George](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/george/32/3585_2.png) [@George](https://klipper.discourse.group/u/George)\
**Post date:** [July 5, 2023, 2:48pm UTC](https://klipper.discourse.group/t/math-operations-with-variables-in-macros/9285/5 "2023-07-05T14:48:30Z")

</div>

Take a look at this [macro](https://github.com/digitalninja-ro/klipper-neopixel/tree/beta). It works only with neopixel but will do what you want.
