For my 3D printer with Klipper and Moonraker I wanted to improve the code for my hardware-button.
I wanted to extend the on_press
code for the button
in moonraker.conf
to have several conditions, based on properties of the printer
object and the state of the relay from moonraker.conf called printer_psu
.
What's working currently is:
The button will power on the relay called "printer_psu" wired to the Raspberry Pi.
Code is looking like this:
on_press:
{% do call_method("machine.device_power.post_device", device="printer_psu", action="on") %}
I wanted to extend it to make multiple use-cases possible:
- Emergency-Stop if the printer is printing
- (else) Cooldown if temperature is above 50°C
- (else) Power off the relay, if it is powered
- (else if) Power on the relay
The code is looking like this:
[button power_button]
type: gpio
# Reserved for future use. Currently the only button type available is
# 'gpio', which is the default.
pin: gpiochip0/gpio26
# The gpio pin to watch for button events. The chip is optional, if
# omitted then the module will default to gpiochip0. The pin may be
# inverted by specifying a "!" may be prefix. Valid examples:
# gpiochip0/gpio26
# gpio26
# !gpiochip0/gpio26
# !gpio26
# Systems with libgpiod 1.5 or greater installed also support pullup and
# pulldown modes. Prefix a "^" to enable the internal pullup and a "~" to
# enable the internal pulldown:
# ^gpiochip0/gpio26
# ^gpio26
# ~gpiochip0/gpio26
# ~gpio26
# # Its also possible to invert a pin with the pullup/pulldown enabled
# ^!gpiochip0/gpio26
# ~!gpiochip0/gpio26
# This parameter must be provided
minimum_event_time: .05
# The minimum time (in seconds) between events to trigger a response. This is
# is used to debounce buttons. This value must be at least .01 seconds.
# The default is .05 seconds (50 milliseconds).
on_press:
{% if printer.idle_timeout.state == "Printing" %}
{% do call_method("printer.emergency_stop") %}
{% elif printer.heater_bed.temperature >= 50 or printer.extruder.temperature >= 50 %}
{% do call_method("printer.cooldown_start") %}
{% elif printer_psu.on %}
{% do call_method("machine.device_power.post_device", device="printer_psu", action="off") %}
{% else %}
{% do call_method("machine.device_power.post_device", device="printer_psu", action="on") %}
{% endif %}
But I get the exception in moonraker.log
: jinja2.exceptions.UndefinedError: 'printer' is undefined
I am currently unsure to what's possible with the moonraker.conf and don't find much of samples or documentation in the direction I would like to see.
Maybe someone over here has an idea.