Before you read this guide, please note that GPIO support was dropped from HA since release 2022.4. The solution still works, but needs an add-on installed through HACS. Try at your own risk, as there is always a chance of breaking HA when installing non-supported add-ons.
Last update 2022-07-17. Big thanks to everyone who contributed with comments and solutions to issues that occurred after GPIO support was dropped
This guide is for those that are running Home Assistant OS on a Raspberry (or similar platform) with a case that includes a PWM-controlled CPU fan and where that fan is normally controlled by installing some software. I am using a Geekworm NASPI case, which has a X-C1 power management board that controls the fan using GPIO pin 18, which seems to be more or less standard also for other case fans.
If you are running HA Core, this guide is not for you, as you can simply install the software needed to control the fan speed.
Some parts of this guide is information I found in this thread:
1. Install pigpio add-on
There is an excellent collection of add-ons called âPoeschl Home Assistant Add-onsâ, one of them is pigpio which will let you send PWM data on a GPIO pin.
To install, open the âAdd-ons, Backups and Supervisorâ from the Configuration and then click on ADD-ON STORE button. Now you need to add the repository by clicking the menu button (the tree-dots button in the top right corner) and add the following URL:
https://github.com/Poeschl/Hassio-Addons
Then find the pigpio add-in and install it. Make sure itâs set to start on boot. When itâs installed, start it.
2. Install GPIO PWM add-on
Since version 2022.4 of Home Assistant the support for GPIO has been removed. You will therefor need to install a custom add-on to be able to control the fan speed using PWM, using the link below. This add-on can be installed manually, or via HACS.
3. Add CPU temp sensor and PWM light devices
This step requires changing the file configuration.yaml. If you donât know how to do this, refer to this guide:
https://www.home-assistant.io/getting-started/configuration/
Add the following lines to the configuration:
# RPi fan control
sensor:
- platform: command_line
name: CPU Temperature
command: "cat /sys/class/thermal/thermal_zone0/temp"
unit_of_measurement: "°C"
value_template: '{{ value | multiply(0.001) | round(1) }}'
scan_interval: 10
light:
- platform: rpi_gpio_pwm
leds:
- name: RPI Cooling Fan
pin: 18
As you will later also need to add a Python script, you will also need one more entry to enable Python scripts:
# Enable python scripts
python_script:
Save your changes and reload the configuration. You an already now control the fan and monitor the CPU temperature. Simply add an âEntitiesâ card with the CPU temperature sensor (sensor.cpu_temperature) and the fan control (light.rpi_cooling_fan).
Turning on the fan will set it to full speed, but if you click the CPU fan entity, you can now adjust the âbrightnessâ (the pigpio add-on was designed to control LEDâsâŚ) but in fact this now controls the fan speed!
You should play around with the best value to keep your CPU at a comfortable temperature. In my case I set this to 50%, which keeps the CPU at around 40 degrees C.
4. Create Python script to set fan speed
To be able to set the fan speed from automations, I use a simple Python script. The script should be created in folder âpython-scriptsâ, and in my setup I saved it 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)
Make sure you either restart HA, or reload Python scripts. You should then test the script from the Developer Tools:
Please note that parameter fan_speed is a value between 0 and 255, where 255 = full speed. Check that the fan speed is set accordingly.
5. Automations
You can now control the fan with some simple automations. This is how I control mine.
Set fan speed on startup:
alias: Set CPU fan speed on startup
description: ''
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: python_script.rpi_fan_pwm
data:
fan_speed: 128
mode: single
Set fan to high speed:
alias: CPU fan high speed
description: Turn up fan speed when CPU is over 55 degrees
trigger:
- platform: numeric_state
entity_id: sensor.cpu_temperature
above: '55'
condition: []
action:
- service: python_script.rpi_fan_pwm
data:
fan_speed: 255
mode: single
Set fan back to normal speed:
alias: CPU fan normal speed
description: Set CPU fan to normal speed when temp is below 40
trigger:
- platform: numeric_state
entity_id: sensor.cpu_temperature
below: '38'
condition: []
action:
- service: python_script.rpi_fan_pwm
data:
fan_speed: 128
mode: single