How to make fan power on link to motor move

Describe your issue:

Dear all,
i’m a newer for klipper. I wanna fan power on when the extruder motor moves. I tried to use [controller fan], but the fan power on with the extruder stepper activation instead of moving.

This is the correct setting, and Klipper does not offer any alternatives.

This is by design. As soon as you activate the steppers, the stepper drivers and motors will start heating up, and this setting is intended to counter that effect.

If you are very unhappy with this behavior, an alternative is the display_template functionality. With this, you could tie the fan control to other conditions like printing, though it is not straightforward and requires some macro coding. See this for an unrelated example; the overarching idea should become clear.

THANKS! Sineos.
I will try to code some macro to solve this problem if this action could be achieved. But this is difficult for me, i never have a experience to code new macro.

Hi! Sineos

I made a macro to solve this, but there are some erro i can’t solve. Could you please do me a favor?
The attachment is my printer.cfg. I try to use the step, count number and E position as the power-on condition for the fan. I thought E position may be the right one. However, I have no ideal to solve the erro of variable .
klippy.log (2.4 MB)

Depending on your requirements, you might want to experiment with:

# Either printing, or paused or active SD card
{% if printer.idle_timeout.state|upper == "PRINTING" or printer.pause_resume.is_paused or printer.virtual_sdcard.is_active %}

# OR: Printer able to extrude, i.e. Extruder at min temperature:
{% if printer.extruder.can_extrude|lower == 'true' %}

# OR: If you want to react when printer goes to idle
{% if printer.idle_timeout.state|upper == "IDLE" %}

For the current macro, it has some issues. Should probably look like (untested)

[gcode_macro EXTRUDER_FAN_MONITOR]
variable_last_position: 0
gcode:
    {% set last_position = printer["gcode_macro EXTRUDER_FAN_MONITOR"].last_position %}
    {% set current_pos = printer.extruder.position %}
    {% set delta = current_pos - last_position %}

    {% if delta > 0 %}
        SET_FAN_SPEED FAN=extruder_fan SPEED=1.0
    {% else %}
        SET_FAN_SPEED FAN=extruder_fan SPEED=0
    {% endif %}

    SET_GCODE_VARIABLE MACRO=EXTRUDER_FAN_MONITOR VARIABLE=last_position VALUE={current_pos}

But this is not going to work, as a macro is only evaluated once, when you call it. This is why I pointed to the example with the display_template. Once you “start” the template, it is continuously evaluated and will react to changes.

This macro includes UPDATE_DELAYED_GCODE to continuously evaluated the stepper_extruder state, but the fan still started when the extruder stepper is activated. And there is a erro about variable when i click the macro in mainsail .
[gcode_macro EXTRUDER_FAN_MONITOR]

初始化变量

variable_last_position: 0
gcode:
# 获取当前挤出机步进位置
{% set current_pos = printer.stepper_extruder.position %}
# 计算位移增量
{% set delta = current_pos - var.last_position %}

# 判断增量方向控制风扇
{% if delta > 0 %}
    SET_FAN_SPEED FAN=extruder_fan SPEED=1.0
{% else %}
    SET_FAN_SPEED FAN=extruder_fan SPEED=0
{% endif %}

# 更新位置记录
SET_GCODE_VARIABLE MACRO=EXTRUDER_FAN_MONITOR VARIABLE=last_position VALUE={current_pos}

[gcode_macro ACTIVATE_MONITOR]
gcode:
# 启动监控(建议加入打印开始宏)
UPDATE_DELAYED_GCODE ID=extruder_monitor DURATION=0.1 CODE=EXTRUDER_FAN_MONITOR

[delayed_gcode extruder_monitor]
gcode:
EXTRUDER_FAN_MONITOR
# 设置100ms轮询间隔
UPDATE_DELAYED_GCODE ID=extruder_monitor DURATION=0.1 CODE=EXTRUDER_FAN_MONITOR

I have never tried constructing such a loop this way. It sounds like a good way to crash Klipper, and a frequency of 0.1 seems far too aggressive.
Exactly for such intentions, the display_template exists. YMMV