Displaying CPU temperature in Home Assistant

CPU temperature screenshot in Home Assistant

Earlier in the week, I mentioned that I’d bought a new case to keep my Raspberry Pi cool. So that I can keep an eye on the CPU temperature, and hope that it doesn’t overheat again, I’ve added this as a sensor in Home Assistant.

Adding a CPU temperature sensor to Home Assistant isn’t as straightforward as most other integrations. It’s not included in the System monitor integration, for example. When I was searching for solutions, I found all sorts, including one which required installing several packages from Aptitude, a script, and an MQTT broker. To be fair, that was designed to monitor multiple Raspberry Pis remotely; all I needed was just to poll the CPU temperature of the device that Home Assistant was running on.

Command line integration

Whilst not as straightforward as adding an integration, there is a relatively simple way of adding a CPU temperature sensor using the Command line integration. Indeed, it’s one of the given usage examples, which I’ve copied below:

# Example configuration.yaml entry
command_line:
  - sensor:
      name: CPU Temperature
      command: "cat /sys/class/thermal/thermal_zone0/temp"
      # If errors occur, make sure configuration file is encoded as UTF-8
      unit_of_measurement: "°C"
      value_template: "{{ value | multiply(0.001) | round(1) }}"

You’ll need to add this to your configuration.yaml file, and then restart Home Assistant. You should now have a ‘CPU Temperature’ entity, which you can add to a card to display on your home dashboard.

As well as running the command, the example code also adds the relevant units, and also reformats the numbers. If you open a terminal window and run the cat /sys/class/thermal/thermal_zone0/temp command on its own, you’ll get a value like ‘36511’. So, the value_template line divides the number by 1000 (or specifically multiplies it by 0.001, but the result is the same), and rounds it to one decimal place.

Once you’ve got the entity into Home Assistant, you can log your CPU temperature over time, and even display it as a graph if you want. You can also build automations, so that you can receive notifications when your CPU temperature is too high. For a Raspberry Pi, ‘too high’ is 85°C.

Raspberry Pi cooling fan and case

A photo of my Raspberry Pi in its new case with a cooling fan

We finally had some nice, warm weather a couple of weeks ago. Which was nice for being outside, but not so nice for my poor little Raspberry Pi, which couldn’t keep cool. So, I’ve upgraded it with this cooling fan and case from The Pi Hut.

Until now, I’ve been using the standard, official Raspberry Pi case. Which does the job of keeping dust out off the main board and protecting it, but not much else. As it’s plastic, it doesn’t do a good job of heat dissipation.

This was a problem last week, as my Raspberry Pi was getting so hot that it was shutting itself down to prevent damage. The impact of this was that my phone couldn’t access the internet, as I use AdGuard Home as the DNS server.

Cooling options

When it comes to cooling down a computer, there are three ways of doing it. Most use all three:

  • Ventilation – letting air flow through the computer
  • Active cooling – using a fan (or water cooling system) to move hot air away
  • Heat dissipation – using heat sinks to help get heat away from components like the processor

The Raspberry Pi has an ARM processor which is a RISC (Reduced Instruction Set Computer) type, and these tend to run cooler than CISC (Complete Instruction Set Computer) processors like those from AMD and Intel. This is why just about every smartphone uses ARM processors, as otherwise they would probably need fans too. Let’s just say Intel’s attempts at putting x86 processors into smartphones didn’t go well.

If you’re not doing anything too taxing with your Raspberry Pi, then the lack of cooling in the official case probably isn’t going to cause you issues. Indeed, I’d only had occasional issues until the recent warm weather. In addition to Home Assistant and AdGuard, my Raspberry Pi also runs Plex, MariaDB, Nextcloud, Calibre-Web, a Matter server and Nginx Proxy Manager, and it manages all these okay normally.

Cooling case

I ordered the new case from The Pi Hut on a Friday, and it arrived through my letterbox within 24 hours. It’s only £10 and includes a fan, which plugs into your Raspberry Pi’s GPIO (General Purpose Input and Output) ports for power. It comes in a tiny package, because there’s some assembly involved; essentially, you get four acrylic plates, the fan, and lots of screws. Each acrylic plate has been laser cut, and peeling off the protective film and popping out the tabs is quite satisfying.

You’ll need a screwdriver; I have one from the iFixit Essential Electronics Toolkit which seems to be the same one used in the assembly guide. Preparing the plates actually takes longer than the assembly – there’s not many screws and it all slots together.

Up and running with the cooling fan

Once assembled, I re-connected all the cables and booted my Raspberry Pi up. And then had to unscrew the top and re-connect the cooling fan wires as I’d put them in backwards. But after that, the fan whirred into action and I could feel a slight breeze coming out.

The fan is whisper quiet; whilst my hearing isn’t the best, I couldn’t hear it at all. It’s also worth noting that I run my Raspberry Pi on Debian, and not the official Raspberry Pi OS, and yet the fan still works. In other words, there’s no special drivers or scripts to install to get it to work.

If heat is still a problem, then there’s still plenty of space inside the case to add heat sinks, and again, The Pi Hut has you covered. In the meantime, I’ll see how I get on with this new case.

Water cooling?

I mentioned liquid cooling, which is more effective than a cooling fan, and sometimes used for high performance gaming computers. Of course, someone has done this for their Raspberry Pi, and the video is embedded above. The cooling system is huge compared to the Raspberry Pi board, which suggests that just because you can, doesn’t mean you should.