Math operations with variables in macros?

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.

This might be helpful
https://www.klipper3d.org/Command_Templates.html

You may find a few of these examples helpful

Also see here: Template Designer Documentation — Jinja Documentation (2.10.x)

1 Like

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 :wink:

[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… :wink:

Take a look at this macro. It works only with neopixel but will do what you want.

1 Like