RackedPrice

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.

Make and tinker1 hourEasy

Drafted with AI assistance, fact-checked and approved by RackedPrice.

  1. Plug a BME280 sensor into your Pi with a Qwiic cable.

    Turn on I2C first, as our sensors how-to shows. Then i2cdetect -y 1 should show the sensor at 77.

  2. Use a long cable so the Pi's warmth doesn't fool the sensor.
  3. 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-mqtt
  4. Turn 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.)
  5. 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.py
    import 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.py
    Start on every boot
    (crontab -l 2>/dev/null; echo "@reboot sleep 30 && $HOME/sensors/bin/python $HOME/sensors/room.py") | crontab -

Want every detail? Read the full guide

Build 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.

Build Home Assistant room sensors with a Raspberry Pi (no soldering): every step and a priced parts list

Shopping (optional)

What you'll need

SparkFun doesn't share prices with us, so check the price on its page.

More make and tinker how-tos