Monitor your Raspberry Pis in Home Assistant using MQTT

Disclosure: This post contains affiliate links. If you click through and make a purchase, I will earn a commission, at no additional cost to you. Read my full disclosure here.

A simple Python script allowing you to monitor all the Raspberry Pis you might have running in your smart home has been shared by GitHub user Sennevds. This can be useful if you have multiple Raspberry Pis doing different things, for example, if you have one as an ad-blocker and another one as a Zigbee hub.

Using this simple script, you will be able to monitor vital system information, such as CPU usage and temperature. For all the nerds who like stats, you will of course also be able to display the system information using bars and graphs in the Home Assistant dashboard.

How the Raspberry Pi monitoring script works

The script, which is written in Python, runs every 60 seconds and sends system information data to an MQTT broker. If your Home Assistant has access to the same MQTT broker, it will automatically pull that data using MQTT auto-discovery. You can then display the system information from the Raspberry Pi in your Home Assistant dashboard and, of course, use it in automations and scripts.

The script gathers data on the following:

  • CPU: Usage and temperate
  • Memory: Usage
  • Storage: Usage, swap usage, and external drive usage
  • Networking: Wi-Fi signal strength
  • Raspberry Pi: Power status, last boot, last message received timestamp, and number of upgrades pending

CanaKit Raspberry Pi 4 Extreme Kit - Aluminum Edition (8GB RAM)

GeeekPi Raspberry Pi 4 8GB Starter Kit - 64GB Edition, Raspberry Pi 4 Case with PWM Fan, Raspberry Pi 18W 5V 3.6A Power Supply with ON/Off Switch, HDMI Cables for Raspberry Pi 4B (8GB RAM)

CanaKit Raspberry Pi 4 4GB Starter PRO Kit - 4GB RAM

CanaKit Raspberry Pi 4 4GB Basic Kit with PiSwitch (4GB RAM)

Product
CanaKit Raspberry Pi 4 Extreme Kit - Aluminum Edition

GeeekPi Raspberry Pi 4 8GB Starter Kit

CanaKit Raspberry Pi 4 4GB Starter PRO Kit

CanaKit Raspberry Pi 4 Basic Kit

Model
Raspberry Pi 4 Model B (8 GB)

Raspberry Pi 4 Model B (8 GB)

Raspberry Pi 4 Model B (4 GB)

Raspberry Pi 4 Model B (8 GB)

PSU
Power supply with noise filter

USB-C power supply (5V/3.6A)

Power supply with noise filter

Power supply with noise filter

Accessories
Premium Aluminum Case with Built-In Passive Heat Sink Cooling

Case; cooling fan wit four aluminium heatsinks, 64 GB microSD card, two HDMI cables

Premium High-Gloss Case with Integrated Fan Mount, Low Noise Bearing System Fan

Set of 3 aluminium heat sinks

Price
$189.99

$279.99

Price not available

Price not available

Amazon Prime

Amazon Prime

-

-

CanaKit Raspberry Pi 4 Extreme Kit - Aluminum Edition (8GB RAM)

Product
CanaKit Raspberry Pi 4 Extreme Kit - Aluminum Edition

Model
Raspberry Pi 4 Model B (8 GB)

PSU
Power supply with noise filter

Accessories
Premium Aluminum Case with Built-In Passive Heat Sink Cooling

Price
$189.99

Amazon Prime

GeeekPi Raspberry Pi 4 8GB Starter Kit - 64GB Edition, Raspberry Pi 4 Case with PWM Fan, Raspberry Pi 18W 5V 3.6A Power Supply with ON/Off Switch, HDMI Cables for Raspberry Pi 4B (8GB RAM)

Product
GeeekPi Raspberry Pi 4 8GB Starter Kit

Model
Raspberry Pi 4 Model B (8 GB)

PSU
USB-C power supply (5V/3.6A)

Accessories
Case; cooling fan wit four aluminium heatsinks, 64 GB microSD card, two HDMI cables

Price
$279.99

Amazon Prime

CanaKit Raspberry Pi 4 4GB Starter PRO Kit - 4GB RAM

Product
CanaKit Raspberry Pi 4 4GB Starter PRO Kit

Model
Raspberry Pi 4 Model B (4 GB)

PSU
Power supply with noise filter

Accessories
Premium High-Gloss Case with Integrated Fan Mount, Low Noise Bearing System Fan

Price
Price not available

-

CanaKit Raspberry Pi 4 4GB Basic Kit with PiSwitch (4GB RAM)

Product
CanaKit Raspberry Pi 4 Basic Kit

Model
Raspberry Pi 4 Model B (8 GB)

PSU
Power supply with noise filter

Accessories
Set of 3 aluminium heat sinks

Price
Price not available

-

For the script to work, you will have to edit a file, which is written in YAML. There are a few required parameters such as information on the MQTT broker, a client ID and name, and the time zone you are in. Other parameters are optional, such as the update interval and whether the Wi-Fi signal strength should be measured or not.

How to use the system information data

Thanks to the MQTT auto-discover, you won’t have to make any adjustments in Home Assistant, besides adding the following lines to your configuration.yaml:

mqtt:
  discovery: true
  discovery_prefix: homeassistant

Having the system information of your Raspberry Pis available in Home Assistant can be very useful. For starters, you won’t have to log in to the Raspberry Pis every time you want to check on how they are doing. You can also have a notification sent to your smartphone every time the CPU of a Raspberry Pi exceeds a limit you have set or if the storage is nearing its full capacity.

Displaying Raspberry Pi system information in Home Assistant
Displaying Raspberry Pi system information in Home Assistant (source: GitHub)

The creator of this script has also shared their own personal Lovelace configuration. They use a combination of the vertical-stack-in-card, the mini-graph-card, and the bar-card. I do have to admit, having the system information of Raspberry Pis displayed in Home Assistant does look nice.

Leave a comment

Share to...