Disclosure: We earn a commission from some links on this page. Details
Make a room temperature sensor
See a room's temperature and humidity in Home Assistant, from a sensor you put together.
Drafted with AI assistance, fact-checked and approved by RackedPrice.
Plug a BME280 sensor into your Pi with a Qwiic cable.
Turn on I2C first, as our sensors how-to shows. Then
i2cdetect -y 1should show the sensor at 77.- Use a long cable so the Pi's warmth doesn't fool the sensor.
Install SparkFun's BME280 Python library, linked from the sensor's page.
Raspberry Pi OS wants extra Python libraries in their own folder (a "virtual environment"). These two lines make one called sensors and install the sensor library and an MQTT library into it.
python3 -m venv ~/sensors~/sensors/bin/pip install sparkfun-qwiic-bme280 paho-mqttTurn on MQTT in Home Assistant, so it can receive the readings.
- Settings > Add-ons > Add-on store > Mosquitto broker > Install, then Start.
- Settings > Devices & services: MQTT shows under Discovered. Click Add.
- Settings > People > Users > Add user: make one called sensors with a password. (No Users tab? Turn on Advanced mode in your profile.)
Send the readings to Home Assistant with this small script.
- Save it as room.py with
nano ~/sensors/room.py, paste it, and change the password and room name. Then run it with the second command. - Two new sensors appear in Home Assistant under Settings > Devices & services > MQTT. It sends a reading every minute.
- To start it on every boot, add it to cron with the third command.
room.pyimport json, time import paho.mqtt.client as mqtt import qwiic_bme280 HA = "homeassistant.local" # or your Home Assistant's address USER, PASSWORD = "sensors", "your-password" ROOM = "office" sensor = qwiic_bme280.QwiicBme280() sensor.begin() c = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) c.username_pw_set(USER, PASSWORD) c.connect(HA, 1883) c.loop_start() for key, unit in (("temperature", "°C"), ("humidity", "%")): c.publish(f"homeassistant/sensor/{ROOM}_{key}/config", json.dumps({ "name": f"{ROOM} {key}", "unique_id": f"{ROOM}_{key}", "device_class": key, "unit_of_measurement": unit, "state_topic": f"home/{ROOM}/climate", "value_template": "{{ value_json.%s }}" % key}), retain=True) while True: c.publish(f"home/{ROOM}/climate", json.dumps({ "temperature": round(sensor.temperature_celsius, 1), "humidity": round(sensor.humidity, 1)})) time.sleep(60)Run it~/sensors/bin/python ~/sensors/room.pyStart on every boot(crontab -l 2>/dev/null; echo "@reboot sleep 30 && $HOME/sensors/bin/python $HOME/sensors/room.py") | crontab -- Save it as room.py with
Want every detail? Read the full guide
Build the whole project
Temperature, humidity and real CO2 readings in Home Assistant from plug-in SparkFun sensors on a Raspberry Pi, plus an optional status screen and power monitoring.
What you'll need
- BME280 sensor (Qwiic)Check price at SparkFun
- Qwiic pHAT v2.0Check price at SparkFun
- Qwiic Cable KitCheck price at SparkFun
- Raspberry Pi 5 2GBSee prices
SparkFun doesn't share prices with us, so check the price on its page.
More make and tinker how-tos
Set up your first Raspberry Pi
A tiny computer for learning, projects and small home servers.
30 minutesPlug sensors into a Pi (no soldering)
SparkFun's Qwiic cables click in one way only. No soldering, no wiring mistakes.
20 minutesGet an alert when a room gets stuffy
Rising CO2 means stale air. A CO2 sensor tells you when to open a window.
1 hour