PWM Fan control with ESP32

I’m still messing around with this. I initially had my automation using a mess of embedded IF/THEN/ELSE to switch the fan in steps. I never really liked it but I didn’t know what else I could do, but then I found this post:

It shows how to use a data template to control the fan percentage based on CPU temperature.

I have recreated that in my fan control automation and come up with this:

alias: Datastore Fan Curve
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.datastore_temp
condition: []
action:
  - service: fan.set_percentage
    data_template:
      percentage: >
        {% set temperature = states('sensor.datastore_temp') | int %}  {% if
        temperature > 59 %}
          100
        {% elif temperature > 40 and temperature < 60 %}
          {{ 4.5*temperature-170 }}
        {% else %}
          0
        {% endif %}
    target:
      entity_id: fan.fan_140mm
mode: single

What this does is:

  • If Temperature <41°C, Fan is off
  • If Temperature is between 40°C and 60°C, Fan is on at 14.5% and speed increases 4.5% for every 1°C above 41°C
  • If Temperature >60°C, Fan is 100%

This seems more like what I was setting out to achieve rather than stepped control as I had done before.

3 Likes