Format to test for profile name in start script

What is the problem?

I want to run different start scripts depending on which printer profile is being used but I cant work out how to reference the profile name. I think the script syntax is OK, it doesn't create any errors. It should be something like:-
{% if printer_profile.name == "profile name" %}
GCODE to be actioned if the current profile has that name
{% else %}
GCODE to be actioned if not that profile name
{% endif %}
but that doesn't work. The syntax works fine but I cant get a true result for the profile name no matter what I try.

What did you already try to solve it?

Looking though the forum there are a couple of entries saying its possible but none detail the correct syntax and another that explains the printer_profile attributes have not been documented, it links to code but I just dont understand enough about the code to get the information I need.
Link to comment here.

Have you tried running in safe mode?

Not relevant

Did running in safe mode solve the problem?

Not applicable

Complete Logs

Logs show nothing.

Additional information about your setup

Octoprint Version 1.5.2
Octopi Version 0.17.0, running on Raspberry Pi 3 Model B Plus Rev 1.3

Thanks

Hello @stewl!

Hmm - now I'm curious...

Honestly, if you can afford it, take another Pi for the second printer. Benefit: two prints at the same time.

Also, printer related commands belong as much as possible into the start code of the slicer.

Take out the if clause for a minute, and enter something like this:

M117 {{ printer_profile.name }}

It could also be that the parameters are passed as a dictionary too, in which case

M117 {{ printer_profile["name"] }}

That will make absolutely sure that the name is what you expect - I can't see how that doesn't work, but debugging has to be done.

Hi @Ewald_Ikemann I have a Pi for every printer but this one is a Snapmaker 3in1 that does CNC and laser as well as print. I was wanting to use OctoPrint to at least monitor stuff or possibly run some non print work. The 3D printing is also "managed" in some strange ways. I have an OctoPrint script that actually does bed leveling and uses the probe to bed distance to set the head height - which it the standard processes don't do. When I saw you could use If statements in the GCODE scrips I thought that should be easy then I spent ages searching for the format or an example and then trying every combination I could think off. If it's not a runner I will just have to put the code into cura profiles.

Then it makes sense.

@Charlie_powell, great idea.
M117 {{ printer_profile.name }} returned the correct profile name Snap3DPrintLevel
Now what am I doing wrong with the if statement:
{% if printer_profile.name == "snap3Dprintlevel" %}
which always returns false.

It is definitely case sensitive, so the capitals would have to be exactly as it has been returned from that M117.

Thank you, sorted, correct format is indeed
{% if printer_profile.name == "Snap3DPrintLevel" %}
working just fine.
Thanks again for your help.

2 Likes

Having got this to work for the single profile it would be great if I could get it to do something else if the profile being used is not the one in the "if" statement using say "else", "elseif" or two separate "if" statements.
Can this be done? I have tried a number of options but unless the profile is the one specified in the first "if" statement everything is ignored.

Here are two of the options I have tried, I have also tried

;bed level and Z offsite profile v1
{% if printer_profile.name == "Snap3DPrintLevel" %}
G21 ;set units to millimetre
M140 S65 ; set bed temp for bed level
G90 ;set absolute positioning
G28 ;home need as printer sometimes "lost"
M190 S65 ; wait for bed temp
;Auto Level
G1029 A ;start levelling
G91; relative positioning for Z offset move
;*****************
G0 Z-3.8; Device specific Z offset, reduce to bring print closer to the bed (bigger minus number). (equivalent to M851)
;*****************
G1029 S ;save data
G1029 D0 ;end levelling
G0 Z10; move up 10mm
G90; restore absolute positioning
{% endif %}

{% if printer_profile.name == "Snap3DPrintNoLevel" %}
G90 ;absolute positioning
G21 ;set units to millimetre
M140 S65 ; start bed heat to save time
G28 ;home to match levelling script and to avoid some bed crash scenarios

{% endif %}

;bed level and Z offsite profile v2
{% if printer_profile.name == "Snap3DPrintLevel" %}
G21 ;set units to millimetre
M140 S65 ; set bed temp for bed level
G90 ;set absolute positioning
G28 ;home need as printer sometimes "lost"
M190 S65 ; wait for bed temp
;Auto Level
G1029 A ;start levelling
G91; relative positioning for Z offset move
;*****************
G0 Z-3.8; Device specific Z offset, reduce to bring print closer to the bed (bigger minus number). (equivalent to M851)
;*****************
G1029 S ;save data
G1029 D0 ;end levelling
G0 Z10; move up 10mm
G90; restore absolute positioning

{% else %}

G90 ;absolute positioning
G21 ;set units to millimetre
M140 S65 ; start bed heat to save time
G28 ;home to match levelling script and to avoid some bed crash scenarios

{% endif %}