# Display\_template for fan control

**URL:** <https://klipper.discourse.group/t/display-template-for-fan-control/20375>\
**Category:** Macros\
**Created:** [December 1, 2024, 4:28pm UTC](https://klipper.discourse.group/t/display-template-for-fan-control/20375 "2024-12-01T16:28:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [December 1, 2024, 4:28pm UTC](https://klipper.discourse.group/t/display-template-for-fan-control/20375/1 "2024-12-01T16:28:06Z")

</div>

Using the quite new `display_template` feature for fans, I have created a fan control for my electronics bay.

It works similar to what is known from today’s PC mainboards, i.e. you set some temperature interpolation points with a corresponding fan power and the template constantly evaluates this and controls the fan depending on the readings of a sensor in the electronics bay.

```python
[display_template FAN_BAY]
text:
    # This is the sensor object of the thermistor used to control the fan
    {% set temp = printer['temperature_sensor electronics_bay'].temperature %}

    # Set the interpolation points here:
    {% set points = [
        [0, 0.3],
        [35, 0.3],
        [50, 0.5],
        [60, 0.8],
        [61, 1.0]
    ] %}

    {% if temp < points[0][0] %}
        { "0.0" | trim }
    {% else %}
        {% if points|length > 0 %}
            {% set value = 0 %}
            {% for i in range((points | length) - 1) %}
                {% set t1, v1 = points[i] %}
                {% set t2, v2 = points[i + 1] %}
                {% if t1 <= temp < t2 %}
                    {% set value = v1 + (temp - t1) * (v2 - v1) / (t2 - t1) %}
                    { value | round(3) | trim }
                {% endif %}
            {% endfor %}

            {% if temp >= points[-1][0] %}
                {% set value = points[-1][1] %}
                { value | round(3) | trim }
            {% endif %}
        {% else %}
            { "0.0" | trim }
        {% endif %}
    {% endif %}

## Cooling Fan of the electronics bay
[fan_generic electronics_bay]
pin: !PD14
max_power: 1
shutdown_speed: 0.0
cycle_time: 0.00004

[temperature_sensor electronics_bay]
sensor_type: Generic 3950
sensor_pin: PF4

# Start the electronics bay fan control
[delayed_gcode _START_BAY_FAN]
initial_duration: 1.
gcode:
  SET_FAN_SPEED FAN=electronics_bay TEMPLATE=FAN_BAY

```

The settings above give a control curve that looks like:

 ![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/3X/7/9/791ac06eb33f1d02210a24475986dab3a5ca0e79.png)
