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

I think this has something to do with the version of HA-Raspberry-pi-GPIO-PWM. I had the same issues, but updated, and it fixed the yaml validation stuff.
Oddly, I seem to have two declared instances of the same thing (and two separate directories in my CONFIG/custom_components Folder)
image

Not sure which one is actually ‘active’, and the other ‘non-active’, so left them

Evening peeps

Any one offer up some advice ? pigpio is a up to date and install it as per instructions - same for GPIO PWM which in stalled from here
its when add the temp sensor and light device that I run into issues

Any help would be awesome

Cheers

that link looks suspiciously like its not a pwm gpio plugin (it should be https://github.com/RedMeKool/HA-Raspberry-pi-GPIO-PWM). Have you followed the instructions correctly?

Remove the second usage of ‘light:’ the ‘- platform’ is sufficient.
That works in my config.

Ok so I was afraid you were gonna say this.
When I try installing through HACS, I search for “ha-rpi_gpio_pwm” I find nothing. But if I search for “gpio” I find to integrations to install :

  1. Raspberry Pi GPIO
  2. Raspberry RF GPIO

See attached screen shot.

So what am I doing wrong ? Why cant I find the correct integration ?
I have read the install guide, but I still cant find what I’m doing wrong.

Cheers

Aah, I see the issue. Check this out, it may help:
In HACS, go to the three dots top right

Repo = https://github.com/RedMeKool/HA-Raspberry-pi-GPIO-PWM

Once this is installed, click on the integration in HACS and it will tell you quite a bit about configuration.
Hope this helps

Thanks fredu - really helpful. I did not occur to me this was a custom directory.
So I got a few steps closer. Both addon installs gone ok and the config file updated without any issues.

But now I’m getting the following error in the logs on startup :slight_smile:

Error executing script: expected int for dictionary value @ data['brightness']

Also when I go to test the python script as a service I am unable to set a value for the fan speed.

Also on startup my shim fan does not power up - I’m assuming its because I need to create an automation to turn it on ? but I cant because of the above issue

Thoughts ?

Yup. I think there is a bug with the service call ui - it doesnt show the data part. I too, can’t add the data, unless you switch to YAML mode and enter it directly.

service: python_script.rpi_fan_pwm
data: {fan_speed:210}

It’s wise to configure a startup automation, and set it to some value

The error message above is odd; it looks like you have an automation but are supplying the wrong data. Did you try everything in section 5 above (right at the top of this post), with the service data part set to fan_speed: 128 (or something of your choosing)?

Filling in the data {} portion was the final piece of the puzzle for me on HAOS 2022.9.7. I only used the initial 3-line Python script. I then created a pair of automations via the web UI to turn the fan on and off. The trigger for each is a Numeric State of CPU Temperature. The action for each is a Call Service for Python Scripts: rpi_fan_pwm. The service data is set as fan_speed: 140 for 60%, and fan_speed: 0 for off.

I’m using a pair of Noctua NF-A4x10 5V PWM fans, an Ice Tower Cooler, and a custom case (1 fan ventilates the case). 60% drops the pi temp from 40C to 35C very fast with no noise. Using developer tools helped to find the maximum noise-free speed. I changed the trigger temps to 60 on, 40 off after testing.

I tried it and thank you so much for this. It works for me on RPi 4 with Case Fan (RPi 4) pretty well. I’ve a small note - the correct folder for Python script is "python_scripts" :slight_smile: eg. with underscore.

1 Like

Anyone got this to work? When calling the Python Script with a set fan_speed, nothing changes and by using the brightness slider using the entity card.

Edit: Logs below

Error executing script: expected int for dictionary value @ data['brightness']
14:29:13 – (ERROR) Python Scripts - message first occurred at 14:25:55 and shows up 2 times

Hmmm

I have no error messages, the ‘light’ turns on and off without error. The script sets the ‘light’ brightness so my software seems installed fine… Just the fan doesn’t spin at any brightness. Here is my setup, any suggestions…

Does frequency have an effect?

Add-on

Added and running, its log says it is running.

HACS
Raspberry Pi GPIO PWM installed

Python (saved as rpi_fan_pwm.py)

fan_speed = data.get("fan_speed")
service_data = {"entity_id": "light.rpi_cooling_fan", "brightness": fan_speed}
hass.services.call("light", "turn_on", service_data)

Python Script call

service: python_script.rpi_fan_pwm
data:
  fan_speed: 254

Config yaml

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

Pin 18 is GPIO 24 isn’t it??

Fan connected to pin 18 + and 20 gnd

No errors listed in main log.

Running 2022.11.1 on Pi4

In the config you should be addressing the GPIO pin, not the physical pin number, so it should be 24. For example, I’m using the official RPi4 Case Fan and it’s connected to the physical pin #8, which is GPIO 14, and I got it working using “pin: 14”.

1 Like

Also, I just signed up to say this guide was extremely helpful, thank you @VauxhallViva and also to @fredu for how to install the GPIO-PWM integration through HACS, that was the step I got stuck on :+1:

Thanks a lot @VauxhallViva and @fredu, I finally got the PWM control working with the help of your posts! Very satisfying when something works.

When my Pi reboots the fan doesn’t start though - I need to switch it on manually. Any suggestions on how to have it start automatically?

Cheers!

Thanks, it was so confusing with one integration using the word ‘port’ and another using ‘pin’.
It turned out that they both meant GPIO number.!!

That should be handled by the “Set Fan Speed on Startup” automation in the OP. If you have that set up and it’s not working, but you can manually turn the fan on or set the fan speed, check the step before that, creating the python script to set fan speed.

1 Like

Thanks very much for coming back @DanaLexa.

I think my issue is with the python script. When I try to call the python_script.rpi_fan_pwm service, it says that it doesn’t exist.

I created the rpi_fan_pwm.py file inside a python_scripts folder - can you confirm if I created this folder in the right place?

Cheers

Is this an Addon you use to see these folders if yes how it’s called?

Yes it is an add-on called Studio Code Server.