Having some trouble with variables

Basic Information:

Printer Model: Ender 5 plus
MCU / Printerboard: BTT Manta M8P with BTT mini 12864
klippy.log

Fill out above information and in all cases attach your klippy.log file. Pasting your printer.cfg is not needed
Be sure to check our Knowledge Base and in particular this and this post

Describe your issue: I have setup and extra lights menu for changing colours for my BTT mini 12864. I wanted the selected colour to persist after shutdown so I save the colour variable. I am trying to setup some conditions at startup but my if syntax isn’t correct so the green macro runs even though the colour value is 2.

Any help would be appreciated:

Thanks

Set RGB values with macro on boot up.

Index 1 = display, Index 2 and 3 = Knob

[delayed_gcode setdisplayneopixel]
initial_duration: 1
gcode:
{% set svv = printer.save_variables.variables %}
{% set colour = svv.colour %}
{% if colour != β€œ1” %}
M117 {colour}
green_light
{% elif colour != β€œ2” %}
M117 {colour}
red_light
{% endif %}

[menu __main __lights]
type: list
name: lights

[menu __main __lights __green]
type: command
name: green
gcode:
SAVE_VARIABLE VARIABLE=colour VALUE={1}

	green_light	

[menu __main __lights __red]
type: command
name: red
gcode:
SAVE_VARIABLE VARIABLE=colour VALUE={2}

	red_light	

[menu __main __lights __blue]
type: command
name: blue
gcode:
SAVE_VARIABLE VARIABLE=colour VALUE={3}
blue_light

[menu __main __lights __orange]
type: command
name: orange
gcode:
SAVE_VARIABLE VARIABLE=colour VALUE={4}
orange_light

[menu __main __lights __yellow]
type: command
name: yellow
gcode:
SAVE_VARIABLE VARIABLE=colour VALUE={5}
yellow_light

[gcode_macro green_light]
gcode:
SET_LED LED=btt_mini12864 RED=0 GREEN=1 BLUE=0 INDEX=1 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=0 GREEN=1 BLUE=0 INDEX=2 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=0 GREEN=1 BLUE=0 INDEX=3

[gcode_macro red_light]
gcode:
SET_LED LED=btt_mini12864 RED=1 GREEN=0 BLUE=0 INDEX=1 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=1 GREEN=0 BLUE=0 INDEX=2 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=1 GREEN=0 BLUE=0 INDEX=3

[gcode_macro blue_light]
gcode:
SET_LED LED=btt_mini12864 RED=0 GREEN=0 BLUE=1 INDEX=1 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=0 GREEN=0 BLUE=1 INDEX=2 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=0 GREEN=0 BLUE=1 INDEX=3

[gcode_macro orange_light]
gcode:
SET_LED LED=btt_mini12864 RED=1 GREEN=.6 BLUE=0 INDEX=1 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=1 GREEN=.6 BLUE=0 INDEX=2 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=1 GREEN=.6 BLUE=0 INDEX=3

[gcode_macro yellow_light]
gcode:
SET_LED LED=btt_mini12864 RED=.98 GREEN=.93 BLUE=.65 INDEX=1 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=.98 GREEN=.93 BLUE=.65 INDEX=2 TRANSMIT=0
SET_LED LED=btt_mini12864 RED=.98 GREEN=.93 BLUE=.65 INDEX=3

[save_variables]
filename:~/variables.cfg

Please attach the klippy.log as a file to your next post

klippy.log (2.1 MB)
Klippy log attached

Of course:

	{% if colour != "1" %}
	M117 {colour}
	green_light
	{% elif colour != "2" %}
	M117 {colour}
	red_light
	{% endif %}

2 != 1 so the first comparison is true and green_light is called

What happens with svv.colour = 1 ?

when I select green from the menu and svv.colour is set to 1, the green_light executes and the display is green and M117 displays 1

I changed to this and neither macro runs - the display reverts to the initialize state below and svv.colour is not displayed:

{% if colour == β€œ1” %}
M117 {colour}
green_light
{% elif colour == β€œ2” %}
M117 {colour}
red_light
{% endif %}

[neopixel btt_mini12864]
pin: EXP1_6
chain_count: 3
color_order: RGB
initial_RED: 0.4
initial_GREEN: 0.4
initial_BLUE: 0.4

Is it because you are comparing to a string, not an integer.

Thanks Eddy

That was the issue - works like a charm now!

1 Like