Macro Creation Tutorial

The clue is four lines earlier:

{% if 'bed_mesh' not in printer %}

bed_mesh is an attribute of the printer object, which is why this line checks to see if the printer object has a bed_mesh attribute before trying to interact with it.

When it comes to actually interacting with it, you have to access it as an attribute of the printer object: printer.bed_mesh

So the offending line should be revised to read:

{% if printer.bed_mesh.profile_name is none %}

Oh my gosh, I’m such a dumb… Thank you. I haven’t been able to test it with the revised code, but that is it I bet. I forgot the freaking printer object…

I’m struggling with syntax to combine literal strings with numeric variables. Have googled till my fingers are sore, looked at a zillion examples, and tried every imaginable combination of quotes, apostrophes, brackets, braces, plus signs, tildes, ("'{}+~) and more. I have tried combining into new string variables first, etc. Nothing works. Either Klipper throws an error when restarting, or an error when running the macro, or I just get blank results.
Here is my latest of many attempts. Should be pretty obvious what I’m attempting.

[gcode_macro SEND_THIS]
gcode:
    # This Works!
    #SHOW_THIS S="This works without any parameters"
    # But none of of this does.  What is syntax to include variables??
    SHOW_THIS S='The bed is currently at ' + {bed_temp} + ' degrees Celsius.'
    #{ set qq = "Hello World" }
    #qq: "Hello World"
    #qq: 'The bed is currently at' + {bed_temp} + "Degrees Celsius" %}
    #qq: ["The bed is currently at " + {bed_temp} + "Degrees Celsius"]
    #SHOW_THIS {qq}
    #SHOW_THIS S='The bed is ' ~ bed_temp
    
[gcode_macro SHOW_THIS]
gcode:
	M117 {params.S}

You’re overcomplicating things. If you want to include the value of the variable bed_temp, the syntax is { bed_temp }.

Everything outside of Jinja delimiters is a static string, so you can’t (and don’t need to) put operators or extra quotes or anything else to indicate concatenation.

    SHOW_THIS S="The bed is currently at {bed_temp} degrees Celsius."

If you really want to do the concatenation in Jinja it’s possible, but you have to enclose the whole expression in the Jinja delimiters and overall it’s going to be less readable.

    SHOW_THIS S="{ "The bed is currently at" ~ bed_temp ~ "degrees Celsius." }"

@garethky Hi! I have 4 servos, controlling each separately is not very convenient and not so elegant. I’ve been thinking about your way of using objects for my task for a while, but I can’t find a way. Now I have:

[delayed_gcode _TOGGLE_SERVO1]
gcode:
    {% set unit_data = printer['gcode_macro SERVO_MODE_U1'] %}
    {% set servo_angle = unit_data.servo_angle %}
    
    # { action_respond_info("----------_TOGGLE_SERVO1---------- servo_angle  %s" %(servo_angle))}
    {% if servo_angle == printer['gcode_macro SERVO_MODE_U1'].servo_open_angle %}
        SET_SERVO SERVO=srv0 ANGLE={printer['gcode_macro SERVO_MODE_U1'].servo_closed_angle}
        SET_GCODE_VARIABLE MACRO=SERVO_MODE_U1 VARIABLE=servo_angle VALUE={printer['gcode_macro SERVO_MODE_U1'].servo_closed_angle}
        UPDATE_DELAYED_GCODE ID=_TOGGLE_SERVO1 DURATION={printer['gcode_macro SERVO_MODE_U1'].servo_closed_time}
    {% elif servo_angle == printer['gcode_macro SERVO_MODE_U1'].servo_closed_angle %}
        SET_SERVO SERVO=srv0 ANGLE={printer['gcode_macro SERVO_MODE_U1'].servo_open_angle}
        SET_GCODE_VARIABLE MACRO=SERVO_MODE_U1 VARIABLE=servo_angle VALUE={printer['gcode_macro SERVO_MODE_U1'].servo_open_angle}
        UPDATE_DELAYED_GCODE ID=_TOGGLE_SERVO1 DURATION={printer['gcode_macro SERVO_MODE_U1'].servo_open_time}
    {%endif%}

but, I see it like this

    {%set servos = {1: {'servo_name' : 'servo_u1',
                            'duration'      : 10,
                            'servo_open'    : 5,
                            'servo_closed'  : 60,
                            'state'         : 'open'
                        },
                    2: {'servo_name'        : 'servo_u2',
                            'duration'      : 20,
                            'servo_open'    : 5,
                            'servo_closed'  : 60,
                            'state'         : 'open'
                        }
} %}

and then
{% for servo in servos -%}
with the transmission of the servo number and the corresponding conditions through a variable of the complex data type. Do you think there is a way to implement this?