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:
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
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.
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. –
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.
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.
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
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.
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)
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!
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!
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
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:
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:
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…