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

Hi Phil. No, I didn’t check it out. Should have before posting really.
I still can’t get the fan pwm control working (I have had to manually override this for now -Pi was climbing into the 55-60 degree area. it’s a shame, the solution above was really nice), but life360 is now reconfigured at least.
Will persist further, thanks for the information.

As the original poster of this thread, I tested it now with release 2022.7, and can confirm that it does not work. When the PWM add-on is installed with HACS, Home Assistant fails to load properly.

I have given up on this now and installed a hardware PWM fan controller (costed almost nothing on Ali Express), but will keep an eye on this thread in case someone else comes up with a working solution. I will update the original post about the issues.

I appreciate this thread! Thanks @VauxhallViva for keeping it updated, a simple thing liek controling a fan through a pin is currently becoming science.

The noise of the raspi 4 fan is really annoying. I´m looking for a solution to avoid it, would you mind to tell me what fan is it or maybe the link to aliexpress? i have search it but i,m not sure what i am looking for.
Thaks a lot

I got it working again!

First I found an update in HACS for the GPIO integrations i had installed (i believe this one is the right one)

Second, I updated the config based on errors I’d get in the log. The commented lines were removed or replaced:

light:
  - platform: rpi_gpio_pwm
    leds:
      - name: RPI Cooling Fan (rpi_gpio_pwm)
#       driver: gpio
#       pins: [18]
        pin: 18
#        type: simple

(In other words, remove driver, replace pins with pin.) Not sure if type had to be removed or what it does but for now it’s working.

My fan seems quieter now so i’m not sure if it’s running to full capacity ( more specifically, i thought that I’d hear the noise up to a certain point but I may be misremembering)

2 Likes

What a star!! Yep, thanks to your instructions, got it back working again, although, for some inexplicable reason, the PWM figures needed retuning (Mine now only starts spinning at 172, max 255). It was min 80 odd before.
Still, happier now I have a reactive system capable of coping with this weather we are having. Tomorrow, it’s forecast 39 degrees! +1 to you

Nice, thanks @jofotoo for the fix and for keeping this thread updated!

@anacoll , this is the fan regulator I am using:

Thank you!! I just have read that jofotoo fixed it so first i will try to configure the fan without the controller.

On a related note, I can’t really figure out how to record the PWM settings so I can view the graph in HA. What I would like to do is actually chart the CPU temperature, along with the PWM settings I choose for specific temperatures, to see if the figures are optimal.
I set these figures in an automation - Is there a way to dump them into a ‘sensor’ for viewing?

I´m almost done, just need to automate it using the cpu temperature. How can i do it? you post some kind of script, but in te automate HA sheet i can´t find a way to do it.
I´m a noob in HA, sorry if my question is so symple or obvious.
Thank you

@anacoll, the automations are not scripts, just the YAML version of the automations. When you create an automation, you will by default use the “visual” editor, but if you click the “3 dots” button you can choose the “YAML” editor, and there you can copy/paste the code from this page. Or you can just continue to use the visual editor, the YAML text should give you enough hints to setup the automation, just look at the sections and it should be more or less clear. Hope that helps!

I can’t make it work, as soon as I add gpio in lights.yaml,ha config checker spins forever and can’t restart

Thank you for this. sorry if I write wrong, but I use a translator
I have a problem, it looks like it doesn’t load the Python scripts.
from the services menu the only available items are:
Python Scripts: rpi_fan_pwm
Python Scripts: Reload

on the registers he writes to me: Error executing script: expected int for dictionary value @ data[‘brightness’]
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 224, in execute
exec(compiled.code, restricted_globals)
File “rpi_fan_pwm.py”, line 3, in

@ [elsalaoo] I got this too. Try the following (I may get this a little wrong, am typing from memory here!):

  • Ensure you have the latest version of PIGPI
  • Ensure you have the latest version of HA-Raspberry-pi-GPIO-PWM. When updated, it looks like it places the python scripts in a different folder (this for me fixed the eternal yaml checking issues)

Are you making changes to the configuration.yaml file as @jofotoo above suggested? It’s important to comment out the DRIVER, PINS and TYPE entries.

==edit== and of course, you will need to recalibrate your automation values. the fan now starts spinning at a MUCH higher value (for mine, it’s a minimum of 172)

Thanks a lot for the info, now it is working and the fan is noisy just when it has to be. Since I bought the raspi I have not been able to use it because of the fan noise, but now I’m ready to start my HA adventure!!
Thank you!!

lot of thanks for answering! in my Case I had to manually delete the folder of custom integration and, after rebooting, installed again. now it works! very happy now

same problem here. My fan makes a buzzing noise which is the frequency of the pwm.
Has anyone any idea on how to solve this?
Maybe is it caused by the fan? which fan would you recommend for this so there is no buzzing noise?

Have you tried recalibrating the PWM figures you pass to your python script (from automation)? Mine now needs a minimum of 172 to even start spinning. Not sure why this is, but wonder if the PWM implementation has changed over the past few months.

Hey All ,

I tried following all the steps but I get the following errors when rebooting :

Failed to restart Home Assistant

The system cannot restart because the configuration is not valid: Platform error light.rpi_gpio_pwm - Integration ‘rpi_gpio_pwm’ not found.

I think the issue is the second part of the config :

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

Now I already have a “light” section in my config and adding as is throws up a duplicate error when trying to save the config file so I have added it to my light section :

light:
  - platform: group
    name: Vacation Lights
    entities:
      - light.ella_bedroom_side_light
      - light.ella_bedroom_main_lights_2
      - light.guest_side_light
      - light.guestroom_main_lights
      - light.master_bed_sitansh_light
      - light.master_bed_rena_light
      - light.master_bedroom_main_lights
      - light.office_main_lights
      - light.lounge_go
      - light.lounge_spot
      - light.lounge_main_lights
      - light.hallway_main_lights
      - light.kitchen_undercounter_lights_2
      - light.dining_room_table_lights_4
      - light.office_main_lights
      - light.landing_main_lights
light:
  - platform: rpi_gpio_pwm
    leds:
      - name: RPI Cooling Fan
        pin: 18

But the above causes the error on restart.

I’m not developer of know much about json, so love it if someone can help me out

Cheers