How can I add BME280 Sensor in an Openhab Docker

Platform information:

  • Hardware: arm64/8GB RAM/128GB storage (Raspberry Pi5 8GB Ram)
  • OS: Raspberry PiOS 64-Bit (Debian 12/Bookworm)
    → Gnome 43.9
  • OH running in docker-container
  • openHAB version: 4.1.1

I am trying to find out how can I add the GY-BME280 3 in 1 Sensor to my Openhab Container on Portainer. So the Sensor is connected to my Raspberry Pi5 with I2C and I can read the temperatur, pressure and humidity via python script on the Terminal. Futhermore the Sensor is connected to the same board as the Openhab container is running.
Later I also want to do other python scripts to be executet for instance to reboot, shutdown, read information or controll other things on the GPIO pins.

What are ways to run things like that?
I heared someting like MQTT, but what is is exectly? Could it be useful? And how much power a MQTT need?

The entire purpose of containerization is isolation. The software running inside a Docker container only has acres l access to what you explicitly give it access to.

It’s also going to only have just enough stuff in it to run the one service.

So you’re OH container isn’t going to have access to the I2C bus unless you can pass that hardware through to the container (you’ll have to search online for weather that’s supported and how to do it). And the OH container doesn’t include Python so you’ll need to create a custom image that includes Python or use a startup script that installs it (see the docs for details).

That’s your option to run this inside the OH container but those are not your only options.

MQTT is a messaging protocol. If you’re Python script runs as a daemon you can Send messages to it and it can send messages to OH through the MQTT protocol.

You can see an example of this at GitHub - rkoshak/sensorReporter: A python based service that receives sensor inputs and publishes them in various ways..

But the main takeaway is that as far as OH is concerned, the Python script is running on a whole separate machine. And the Python script is always running as a service listening for and publishing messages.

MQTT is very lightweight. It was designed and built for microcontrollers and industrial control devices to communicate.

But if “power” is your concern, you should not be running OH on Docker with Portainer on an RPi. That’s requiring a whole lot of extra resources you don’t strictly need to be consuming, more than running an MQTT message broker is going to consume.

1 Like

Connecting the sensor to an ESP is pretty neat (and also really flexible). This is my description of getting MQTT working. Here you find a description of connecting a device to an ESP. And here is a description of getting such a device via MQTT into openHAB.

1 Like

Next to an easy solution, which it is, is it also a very scalable solution. If you go the (Arduino) ESP route, also add wifi, you can use lots more sensors. Because 1 thing is sure, once you have 1 sensor, you’ll likely to want much more.

1 Like

True. I bought IKEA’s Vindrikting sensor and retrofitted it with an ESP, CO2, humidity, temperature and particle sensor. All in a really nice enclosure. :slight_smile:

After a few research I found this site, where a guy use a python script to watch out for messages and execute them. Therfore I tried my own “short” python script. So I only have used it to send a IR-Signal for a receiver. Futhermore open one terminal for running the python file. The second is for watching messages. And the third one is for execute a commend to try if it would even work.

In the terminal the python script worked and I got a light up form the receiver, but now I don’t know how to do a button to send the exact same message. I have created a “Thing” on openhab with mqtt binding, ad a channel and link it to a model. All sort of action from the button don’t work with this Thing. The only message I can get is from a toggle, but it isn’t even right, because at the and of the message is every time “ON”. So it also don’t work.
How can I make a button to send the right message?
I also don’t want to use a extra device, which I don’t have (ESP32), and I don’t think I can use Tasmota on the Raspberry Pi.
Hope my english was not to bad.

1. Terminal Command
python Openhab_Mqtt.py
2. Terminal Command
mosquitto_sub -t "#" -v -u xxx -P xxx
3. Terminal Command
mosquitto_pub -t "frame/monitor" -m "send_signal" -u xxx -P xxx
Python Script
import paho.mqtt.client as mqtt
import os
import subprocess

def IR_Signal():
    os.system("irsend SEND_ONCE pioneer_vsx-301 KEY_POWER")
    print ( "Signal Send!" )


def on_connect(client, userdata, flags, rc):
  print("Connected to MQTT broker")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
    if msg.payload.decode() == "send_signal":
        print("Calling script to send signal...")
        IR_Signal()

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed to topic : " + str(mid) +" with QoS" + str(granted_qos))

client = mqtt.Client()

client.username_pw_set( "userxxx" , "passwordxxx" )

client.connect( "192.16x.xxx.xxx", 1883, 60)

client.subscribe( "frame/monitor" , qos=1)

client.on_connect = on_connect

client.on_message = on_message

client.loop_forever()
"Thing"

Things < + < MQTT Binding < Generic MQTT Thing (Select Bridge and Label “Sending a IR signal”)< Create Thing

"Channel"

Things < “Sending a IR signal” < Chanels < Add Channel <
Text Value
MQTT State Topic:“frame/monitor”
MQTT Command Topic:“send_signal”
Label:Testing IR Sending
Channel Identifier: send_ir_signal