Macro Help - Bed mesh load

Basic Information:

Prototype Cartesian Machine
BTT Octopus Max EZ
BTT Pad 7
klippy.log

Fill out above information and in all cases attach your klippy.log file (use zip to compress it, if too big). Pasting your printer.cfg is not needed
Be sure to check our “Knowledge Base” Category first. Most relevant items, e.g. error messages, are covered there

Describe your issue:

My goal is to load a bed mesh based on bed temp coming from the slicer parameters and if this bed mesh is not stored, perform a bed mesh calibrate and save the profile as the bed temp parameter. Is there a conditional statement that can be issued to check if the bed mesh profile exists? Or to catch the klipper error created when bed_mesh_profile load=XXX is called and the named profile doesn’t exist?

Sorry in advance, I can’t currently upload the klippy.log as the printer is at work.

https://www.klipper3d.org/Status_Reference.html#bed_mesh

Thanks for the reference. I am assuming that profiles would be an array of text names that would have to be looped through to check if a given profile name exists. You would happen to know the correct syntax to reference the bed_mesh profiles so I can check if a profile exists in a macro?

https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-tests.in

I appreciate your responses. As I’m not a python expert like you, I am actually just looking for a conditional statement for determining if a bed mesh profile exists which seem pretty simple, as klipper saves them somewhere, but I don’t know where or how to reference in a macro.

The only example that I have found includes this, but it is referring to saved variables for different types of plates and meshes based on temp.
{% if printer.configfile.config["bed_mesh " + mesh_name] is defined %}
I don’t know if printer.configfile.config[“bed_mesh”+names ] contains all the bed mesh profiles that are saved.

Based on your jinga reference, I still really don’t know what I would use for the conditional statement. Something like this?

{% if variable_mesh_name.in(“mesh_name”: any: bed_mesh.profile[Any] %}

Thanks.

The printer.bed_mesh.profiles variable is an object that maps all stored profile names to its configuration, including profiles that haven’t yet been saved to the persistent config by SAVE_CONFIG. You can test if a profile exists with the following statement:

{% if mesh_name in printer.bed_mesh.profiles %}
  ... do something
{% endif %}

Thank you. I figured it had to be easy.