Integration! Home Assistant & BME280


I think it's time to finally connect this sensor to Home Assistant so we can actually display the data nicely, instead of just seeing raw numbers on a screen.
The easiest way would probably be to just flash Home Assistant OS directly onto my Raspberry Pi, but since I want to run a few other things on it, I decided to go with a containerized setup – because, let's be honest, I'm a big fan of containers!
Let's kick things off with the classic: installing Docker on Raspberry Pi OS (64-bit). First up, we'll uninstall any old packages. You can find up to date instructions HERE!
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
Next, let's add the Docker repositories to apt, which is what we'll use to pull down all the necessary packages:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Let's install all the necessary packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
And finally, let's add the current user to the docker group:
sudo usermod -aG docker $USER;
newgrp docker
Alright, Docker is installed, so now we can finally fire up the container with Home Assistant inside! (You can find more details here). To get the container running, we'll need two things: a folder to store the configuration and our timezone. Let's start by creating that configuration folder for Home Assistant.
mkdir ho_configuration
Since I'm currently in Portugal, my timezone is Europe/Lisbon. Make sure you swap that out for your own – you can find yours here. Also, remember to change /home/tomasz/ho_configuration
to your preferred folder. Now that we have all the info, let's fire up the Home Assistant container!
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=Europe/Lisbon \
-v /home/tomasz/ho_configuration:/config \
-v /run/dbus:/run/dbus:ro \
--network=host \
ghcr.io/home-assistant/home-assistant:stable
Once all the layers of our Home Assistant image have been pulled and installed, you should be able to access the graphical interface by typing localhost:8123 into your browser. Just create an account, fill in the basic details, and you should see the Home Assistant main screen.
Now we need to figure out how to bridge the gap between our console-based BME280 sensor readings and Home Assistant. There are a few ways to tackle this, but to brush up on my skills, I decided to go with a really neat communication standard called MQTT. It's super popular in the IoT world, already integrated with Home Assistant, and just plain simple to use.
MQTT is all about the Publish/Subscribe concept. My sensor will Publish two types of data – temperature and pressure – and Home Assistant will then Subscribe to that data and display it nicely. To handle all these subscribers and publishers, we need a central overseer, which is called a broker. I'll be using a super simple one called Mosquitto.
sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
The server's up and running, so now it's time to tweak that script I prepared in the previous post. The data we're collecting and displaying now needs to be published. You publish data to a topic, which is essentially just a string. Before we make any changes, let's install the Python package that supports MQTT, called paho-mqtt (you can find it here).
pip install paho-mqtt
My script will be publishing to localhost, where my Mosquitto server is running, using the default port 1883 and a default keepalive of 60s. I'll be sending data to two topics: bmp280/temp
and bmp280/press
– we'll use these two strings when configuring Home Assistant in the next step. For now, I'm just running the script in a continuously open command line.
#!/usr/bin/env python
import time
from smbus2 import SMBus
from bmp280 import BMP280
from datetime import datetime
import paho.mqtt.client as mqtt
# Initialise the BMP280
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)
# Initialise MQTT
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect("localhost", 1883, 60)
mqtt_client.loop_start()
while True:
now = datetime.now()
formatted = now.strftime("%y-%m-%d %H:%M:%S")
temperature = bmp280.get_temperature()
pressure = bmp280.get_pressure()
print(formatted,f" - {temperature:05.2f}*C {pressure:05.2f}hPa")
mqtt_client.publish("bmp280/temp", round(temperature, 2))
mqtt_client.publish("bmp280/press", round(pressure, 2))
time.sleep(60)
The data should be flowing to the broker every minute, so now we can dive into configuring Home Assistant, which, luckily, has plugin supporting MQTT . You'll need to head into Settings, then "Devices & services", click "Add Integration" in the bottom right corner, and search for MQTT. A basic configuration window will pop up where you just need to enter the hostname, which is simply "localhost"
Next, go into the MQTT integration, click the three dots next to "localhost," and then select "Add MQTT Device." Choose a name like our sensor's name, "BME280." After moving forward, pick "sensor" as the entity type and add "Temperature" as the first reading, along with the reading in Celsius. (Once you type "C" and click next, it should automatically suggest the correct unit, °C.) And most importantly, for the topic name, enter "BME280/temp."
Great, the measurement has been added correctly! You can add the pressure reading in a similar way. You'll see the option "Add another entity to BME280" in the current window, or you can do it directly from the MQTT integration window. When adding the pressure reading, just remember to use hPa as the unit and "BME280/press" as the topic name.
Both measurements are now added! Let's try displaying them on the main screen. Go to the Overview tab and click the pencil icon in the top right corner. In the bottom right, select "Add Card." Then, under the "Entity" tab, find both Pressure and Temperature from your BME280 sensor. And just like that, we've successfully got our BME280 sensor readings into Home Assistant.
I really encourage you to play around with the widgets for displaying values. There's a ton of possibilities there that even I haven't fully explored yet.
I left the sensor running for two days of measurements. During that time, the readings froze for a few hours and only unfroze after I manually searched for the sensor on the I2C bus and restarted the script. Both the script and how it's launched will naturally be improved, but that's for the next post! Below, you can see the measurements from those two days.
Subscribe to my newsletter
Read articles from Tomasz Meyer directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Tomasz Meyer
Tomasz Meyer
With 8+ years in embedded systems, I specialize in firmware development for ARM-based microcontrollers and wireless communication. I combine corporate best practices with startup agility, leading projects from concept to deployment.