Controlling a CPU/case fan with GPIO PWM based on CPU temperature

Hi! really good feature! I am trying to add to my current setup (Raspberry pi 4 with Home Assistant OS 2022.12.6) but I can’t.
I don’t have any error apparently in logs or actions, but trying to manually set the “brigthness” of the fan just doesn’t work, I tried using the GPIO14 (TX/RX) and the GPIO 18(PWM0) but the fan always run with the same speed (I guess maximum speed), any idea?

I didn’t proceed with step 5 due to this

I have already checked I have installed the addon pigpio, and the custome component raspbery pi gpio pwm via HACS. Enabled the CPU sensor:

- platform: command_line
  name: CPU Temp
  command: "cat /sys/class/thermal/thermal_zone0/temp"
  unit_of_measurement: "°C"
  value_template: "{{ value | multiply(0.001) | round(1)  }}"
  scan_interval: 10

and the pwm fan as a light:

- platform: rpi_gpio_pwm
  leds:
    - name: RPI Cooling Fan
      pin: 14

But the fan entity just doesn’t do anything

Hi,
I installed the two custom integrations as mentioned in the post, Otherwise, it does not work.

thanks so much i have modified this to pin 14, so that it works with this PIHAT that i already have. EP-0152 - 52Pi Wiki

i noticed i could also use the 4 LEDS by calling repeat functions of the pins [ RGB1, RGB2 …]

appreciate the guidance with GPIOs on ha.

Did you get a way to work?

Nope :frowning:
I don’t know how to proceed or if this setup it is just deprecated

Thank you everyone that chimed in on this topic. I did get my CPU fan to work, but the information is spread out in a way that it is hard to follow. Here is my attempt to make it easier for others to set this up. I am running HA 2023.1.2, OS 9.3

  1. Install the Home Assistant Raspberry Pi GPIO PWM custom integration
    a. You must have HACS installed already. If you do not, please follow the instructions in the HACS website.
    b. From your HA menu, go to HACS->Integrations, click the ellipses (…) on the top right, select Custom Repositories
    c. Paste the following under Repository: https://github.com/RedMeKool/HA-Raspberry-pi-GPIO-PWM
    d. Under Category select integration then click Add.
    e. On the integration page, click the Download button on the bottom right.

  2. Install the PIGPIO daemon
    a. From your HA menu, go to Settings->Add-ons, then click on the Add-on Store button on the bottom right.
    b. Click the elepsis (…) on the top right, select Repositories
    c. Paste the following in field and click add: https://github.com/Poeschl/Hassio-Addons
    d. You should be back at the Add-ons screen now. Scroll ALL THE WAY DOWN and under the Poeschl Home Assistant Add-ons click the pigpio add-on and click install.
    e. Click on Start on the PIGPIO add-on screen.

– This may be a good point to restart your HA and make sure everything is working so far. –

  1. Add the CPU sensor
    a. Edit the config/configuration.yaml (you will need to SSH or have some editor for the files. I use the File Editor add-on)
    b. Add the following sensor under the sensor property. Note the spacing in YAML is really important.
sensor:
  - platform: command_line
    name: CPU Temp
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: "{{ value | multiply(0.001) | round(1)  }}"
    scan_interval: 10

c. Save the file and restart. If you have the developer tools enabled it is better to check the configuration before restarting.
d. Verify you now have a CPU temp sensor by going to Settings->Devices & Services->Entities, search for CPU Temp, click on it and view the CPU temp under the info tab.

  1. Add the fan control. For this example, I have my PI 4B PWM controllable fan’s TXD cable connected to GPIO-14.
    a. Edit the config/configuration.yaml
    b. Add the following lines to add the CPU fan control
light:
  - platform: rpi_gpio_pwm
    leds:
      - name: RPI Cooling Fan
        pin: 14

c. Save the file and restart. If you have the developer tools enabled it is better to check the configuration before restarting.
d. Verify you can now control the CPU fan by going to Settings->Devices & Services->Entities, search for RPI Cooling Fan, click on it, view the info tab, then use the slider to change the CPU fan speed.

  1. Tie it all together - I wanted a very simplistic way to use these entities, so I created 3 automations to control the fan (off, mid, max). Obviously, something that controls it dynamically is the way to go, but I wanted to keep this super simple for others to follow.
    a. Create a new automation that runs every minute as follows. With this example you can just create any other fan controls. Note that checking every minute is not a great way to control a CPU fan.
    Trigger: Time pattern *, *, 0 (runs every minute) - Why this? You can use a trigger on just CPU temp, but it was hard to force CPU temp for testing.
    Condition 1: Numeric state, entity = CPU fan, above 55
    Condition 2: Numeric state, entity = CPU fan, below 70
    Action: Service = light.turn_on, target = light.rpi_cooling_fan, brightness = 130 (this is the fan speed)

Here is the YAML for this automation:

alias: RPI CPU Fan Mid
description: ""
trigger:
  - platform: time_pattern
    hours: "*"
    minutes: "*"
    seconds: "0"
condition:
  - condition: numeric_state
    entity_id: sensor.cpu_temp
    below: 70
  - condition: numeric_state
    entity_id: sensor.cpu_temp
    above: 55
action:
  - service: light.turn_on
    data:
      brightness: 130
    target:
      entity_id: light.rpi_cooling_fan
mode: single
  1. Dynamic CPU control - You can use automations to achieve dynamic fan control based on temperature. With this example, you should have another automation to shut down the fan if you are below a temperature threshold of 55C. Here is the automation I created in YAML. Note the brightness is 0-255, and the calculation sets the fan speed to 100% once the temperature is higher than 90C.
alias: Dynamic RPI CPU Fan Control
description: ""
trigger:
  - platform: time_pattern
    hours: "*"
    minutes: "*"
    seconds: /20
condition:
  - condition: numeric_state
    entity_id: sensor.cpu_temp
    above: 55
action:
  - service: light.turn_on
    data:
      brightness: |
        {{states('sensor.cpu_temp') / 90 * 255 | int }}
    target:
      entity_id: light.rpi_cooling_fan
mode: single

– And there you have it. My aim was to have the CPU fan off as much as possible since my CPU temp is always low (<50C). I hope this helps everyone!

12 Likes

Thank you for your explantion! Unfortunatly for me is not working so, I am starting to think in my case it may be all about the fan I bougth, could you share us the fan you own? thank you!

This is the fan I purchased: GeeekPi Raspberry Pi 4 Aluminum Heatsink with PWM Controllable Fan, Raspberry Pi 4 Armor Lite Heatsink with PWM Speed Control Fan for Raspberry Pi 4 Model B

1 Like

Thanks a lot, it was my fan, with this one you share everything works like a charm!
Thank you all!

Finally, I added a dinamyc percentage value for the brightness/speed fan. Here it is my automation. I have to mention the range I decided to use for the temperature is between 50-70°C and with a speed between 10-100% so finally my linear function is: speed=4.5*temperature-215

alias: "[FAN] Cold RPI"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cpu_temp
condition: []
action:
  - service: light.turn_on
    data_template:
      transition: 5
      brightness_pct: >
        {% set temperature = states('sensor.cpu_temp') | int %}  {% if
        temperature > 70 %}
          100
        {% elif temperature > 50 and temperature < 70 %}
          {{ 4.5*temperature-215 }}
        {% else %}
          0
        {% endif %}
    target:
      entity_id: light.rpi_cooling_fan
mode: single
4 Likes

I used to have this setup working on my CM4 module on a PITray Mini using pin 14.

Got my HA Yellow today and set it up the same, as it also has pin 14 available on J11 but doesn’t seem to be working!

Did anyone try using PIGPIO and Pi GPIO PWM on the HA Yellow with success?

Thanks for posting this clarification. Like others, I get an error at this step.

light:
  - platform: rpi_gpio_pwm
    leds:
      - name: RPI Cooling Fan
        pin: 14

Does anyone else get this?
" Platform error light.rpi_gpio_pwm - Integration ‘rpi_gpio_pwm’ not found."

If you’ve hit a snag, make sure to follow this step…

I know that my fan start spinning at ~7 (tested on Arduino PWM pin). But here the minimum is 172 as well. If this is the nature of this add-on, then I have to find another way. 172 is definitely too much for ventilation.

It’s easier that I think. There is an optional parameter: frequency.
So, if on Arduino work well, I check data sheet to find PWM frequency and set the same on RPi:

light:
  - platform: rpi_gpio_pwm
    leds:
      - name: RPi Cooling Fan
        frequency: 490
        pin: 13

And now fan start spinning from 9 (~4%).

Bonus: automation for control speed based on temperature and processor load:

alias: RPi CPU Fan Control
description: ""
trigger:
  - platform: time_pattern
    hours: "*"
    minutes: "*"
    seconds: /45
condition: []
action:
  - service: light.turn_on
    data:
      brightness: >
        {% set res = {
           "temp": states('sensor.processor_temperature') | float(),
           "load": states('sensor.load_5m') | float() } %} 
        {{ (15, (255, (res.temp * 4 * res.load) | round()) | min) | max }}
    target:
      entity_id: light.rpi_cooling_fan
mode: single

Brightness return number between 15 and 255.

It took me now more than a year to finally get this done. I started, got close to finish, pin support got removed, lost interest, but now picked it up again.
This thread is really helpful - I would like to contribute by adding my configuration. I am pretty much doing what all of you are doing except for the automations and display. I am using the “climate” / “generic thermostat” display which I find more helpful in regards to what I like to see. Code and Screenshot below:

Screenshot 2023-04-25 131148

climate: 
  - platform: generic_thermostat 
    name: RPI Cooling Fan Controller 
    heater: light.rpi_cooling_fan 
    target_sensor: sensor.cpu_temperature 
    min_temp: 55 
    max_temp: 80 
    ac_mode: true 
    target_temp: 55 
    cold_tolerance: 0.1 
    hot_tolerance: 0.1 
    min_cycle_duration: 
      seconds: 30 
    keep_alive: 
      minutes: 5 
    initial_hvac_mode: "cool"

I just installed a noctua fan and I haven’t been able to control it, I tried pin 14 or 8 for GPIO14, I tried pin 18 or 12 for GPIO18, still nothing, runs full speed…

How to check what is actual fan speed?

Is there any way to get this working on x86 platform? I get the error ‘pigpio not supported.’ My PC fan is going at 100% all the time

Core updates happen in my installation of HA. Please let me know to install software to control the fan speed?