Raspberry Pi CPU Fan Automation

@Ninuss I actually switched to a flicr case du to the fan noise. And the Flickr case cools the rpi passively, works like a charm

Thanks @Tobi0892 … I’ll think about your suggestion and think I’ll adopt it in the future. I don’t like only the fact that there are no GPIO openings in that chassis though. Before that, I played around with electronics a bit. I successfully got a working GPIO18 PWM pin on my RPi B +, also using an old IRLZ44N mosfet to toggle the separate fan circuit from the GPIOs to avoid damage. I used karansoi’s automation but there was a limit. The fan speed does not work properly without calibrating the fan start at a certain low power% of pwm. Maybe my fan is old, because, even calibrated, the initial current to be given to the fan to start spinning is always unpredictable for the rotation, even if Rpi gives it the correct duty cycle found earlier.

@Ninuss I am sorry was away so could not respond.
BTW is your query resolved. I am using one of the gpio these are 3.3 V so I used the mosfet.
The system is working. I am using gpio 17 to drive the gate of the mosfet. However the fan is ppm controlled as set by pwmled add-on

@colethegamer Yes you can use two fans instead of one as I have on my case.
These fans are rated for 5v. RPi GPIOs are 3.3 V compliant. I have used GPIO17 as a pwm switch. This GPIO outputs a ppm signal that is used to drive the N channel Mosfet. This way the 3.3v is used to control the 5 V fan. Of course use is made of the flyback diode to quench the voltage spikes out of the motor inductance at switch off…

would anyone here know of a way to regulate the official Raspberry fan case ? https://www.raspberrypi.org/products/raspberry-pi-4-case-fan/

Yup, using a simple switch:

# GPIO switch for the case fan
  - platform: rpi_gpio
    ports:
      14: CPU Fan

With the control wire of the fan connected to the GPIO #14

Then a couple of simple automations to turn it on and off:

alias: 'Fan Off 52C '
description: Turns the fan on if the CPU < 52C
trigger:
  - platform: numeric_state
    entity_id: sensor.cpu_temp
    for: '60'
    below: '52'
condition: []
action:
  - service: switch.turn_off
    entity_id: switch.cpu_fan
mode: single
alias: Fan On 70C
description: Turns the fan on if the CPU > 70C
trigger:
  - platform: numeric_state
    entity_id: sensor.cpu_temp
    above: '70'
    for: '60'
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.cpu_fan
mode: single

CPU temp comes from this sensor:

# CPU temperature, for fan
  - platform: command_line
    name: CPU Temp
    command: "/bin/cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ (value | int / 1000) | int }}'

1 Like

that’s really nice! thank you.
so we wouldn’t need anything else, no scripts to run before starting HA, all this can be done from within HA as is?

The instructions for the Fan case state the connections need to be to 4, 6 and 8 though…,. can we simply change that?

Obviously the power and ground need to be connected to one of the 5v and gnd pins, but other than that yes. I just kept them as per the diagram as it’s nice and simple, but I also had it running fine on GPIO 17 at one point too as a test.

wait, I think I misunderstood the diagram. pin 8 (the bottom of the 3 used) is GPIO 14 (TXD)… so that is exactly as you say isn’t it?

Yes, the set-up I currently have is matching the Pi Foundation set-up.

But in our case, if you want you can use a different control pin than #14 if you want. As I said, at one point I had it running on #17 (pin 11) as a test and it worked fine (with a suitable adjustment on the switch config of course).

undrstood, and thanks!

guess my confusion is common, and described at the bottom of this: Raspberry Pi GPIO - Home Assistant :wink:

btw, I guess there’s no such thing as setting a fan speed %?

Yes, it’s the age old “fun” of physical pin numbering vs BCM pin numbering (plus of course functional naming like TXD and RXD).

Concerning your other question - I think it’s only on or off, no percentage speed. I’ve not actually tried to put a variable voltage on the control lead and see what happens, but the thing looks so small and simple I expect it’s just a gating switch on the 5v/gnd connections to the fan and either on or off.

Hi
can you past the pwmled addon setup? do you have it all in configuration.yaml?

Hello, please help me, i can’t make this to work, I am very new to home assitant and i don’t understand a lot.
I copy paste the code but i get the next error:

Invalid config for [switch.command_line]: [name] is an invalid option for [switch.command_line]. Check: switch.command_line->name. (See ?, line ?).
Platform error switch.numeric_state - Integration 'numeric_state' not found.
Platform error switch.numeric_state - Integration 'numeric_state' not found.

this is the yaml config:

# Raspberry Pi Cooling Fan
switch:
  - platform: rpi_gpio
    ports:
      4: RPI Cooling Fan

  - platform: command_line
    name: CPU Temp
    command: "/bin/cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ (value | int / 1000) | int }}'
  
  - alias: 'RPI Cooling Fan einschalten'
    trigger:
    platform: numeric_state
    entity_id: sensor.cpu_temp
    above: 55
    action:
      service: switch.turn_on
      entity_id: switch.rpi_cooling_fan

  - alias: 'Pi Lüfter ausschalten'
    trigger:
    platform: numeric_state
    entity_id: sensor.cpu_temp
    below: 36
    action:
      service: switch.turn_off
      entity_id: switch.rpi_cooling_fan

The CPU temperature is a sensor, not a switch.

Just above the - platform: command_line put sensor: fully indented to the left (in-line with your existing switch: header).

sensor:
  - platform: command_line
    name: CPU Temp
    command: "/bin/cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ (value | int / 1000) | int }}'

Similarly the other two items to turn the fan on and off are automations, not switches.Those are better entered via the GUI rather than by changing the configuration.yaml file.

Be careful, rpi_gpio integration is deprecated and will be removed from HA as an official integration in June

True, although hopefully it will be replaced by an installable alternative.

Otherwise from what I’ve seen, several users who do have stuff hooked up to GPIO (more serious than just a case fan anyway) are gonna get grumpy…

Is the blue (connected to gpio 14) on Raspberry fan case not the same as RPM speed signal?
I’m trying to do the exact same thing with a 5V Noctua fan but I can’t get the rpi_gpio switch to do anything. The fan is running full speed as soon as the pi is powered. I would like to be able to turn on/off with a rpi_gpio switch.

On mine the fan comes on when the Pi is shut down, as the pin used goes high.

From what I can see the case fan is just on or off, it’s not speed controllable.

Thank you very much, it’s working now!