How/where to set up MQTT broker with Text-Files?

Dear all,

I would like to set up an openhabian 2.4 on Raspberry 3B with text files.
In addons.cfg one can set up the bindings to install - “mqtt” also …

But where can I set up the MQTT binding 2.4 itself?
It seem that for 2.4 binding there is no .cfg file (as it was for 1.x)?

Thanks,
Selter

You’ll need a .thing file for your broker.
Here’s a nice explanation.

There you can also define your things and their channels.

Thanks for the link.

So it might like this, right?

Bridge mqtt:broker:mosquitto "Mosquitto" @ "System" [ host="localhost", secure=false, username="wwwwwww", password="XXXXXX" ]
{
	
	Thing topic shelly1_1 "Shelly: Shelly1 #1" @ "MQTT" {
	Channels:
		Type switch : OG_bad_l_deckenlampe "Shelly1 #1_switch" [ stateTopic="shellies/shelly1-YYYYY/relay/0", commandTopic="shellies/shelly1-YYYYY/relay/0/command", on="on", off="off" ]
	}
}

Have to admit that I’m not the expert on MQTT (just got my first topics up and running a couple days ago).
I’m not able to test now but your Thing definition could cause an error.
According to the documentation, you should use the following syntax:
Thing <binding_id>:<type_id>:<thing_id> "Label" @ "Location" [ <parameters> ]

Then it could look like this:
Thing mqtt:topic:shelly1_1 "Shelly: Shelly1 #1" @ "MQTT"

The channel seems fine to me.
If you like it short, you may want to skip the state transformations on=“on” and off=“off”. I guess they don’t do anything here. But keeping them makes it easier if you want to change them later.

It’s a bit confusing - since in the same documentation example they use:

Thing topic sonoff_Dual_Thing "Light_Dual" @ "Sonoff" {  
    Channels:
      Type switch : PowerSwitch1  [ stateTopic="stat/sonoff_dual/POWER1" , commandTopic="cmnd/sonoff_dual/POWER1", on="ON", off="OFF"]
      Type switch : PowerSwitch2  [ stateTopic="stat/sonoff_dual/POWER2" , commandTopic="cmnd/sonoff_dual/POWER2", on="ON", off="OFF"]
      Type string : Version [stateTopic="stat/sonoff_dual/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]
      }     

Hmm, you’re right. Should work then. Did you try already?

Note: I have a dedicated broker running on another RPi.
This .things file (on my dedicated openhabian RPi) defines mqtt topics from a variety of ESP32 and RPi, with a variety of sensors (temperature, humidity, RCWL “radar” motion detectors, pressure, etc.) (I have an Aeotec MultiSensor 6, but they are far too expensive to deploy many, when compared to an ESP32 w/ DHT22 for well under $20 USD.)

(Note: the names with “thermostat” in them imply thermostat, but are in fact just temperature sensor. Originally planned to be thermostats, but design changed but haven’t updated the naming conventions…)

So, just for reference here is my .things file for my mqtt devices:

Bridge mqtt:broker:redpi_mqtt_broker [ host="<your broker ip here>", secure="AUTO", username="<your user>", password="<your password>" ]
{
    Thing topic drivesensor "Driveway Motion Detector" {
    Channels:
        Type number : DrvMotNum     "Drive Motion Num Channel"   [ stateTopic="driveway/motion"]
        Type string : DrvMotStr     "Drive Motion Str Channel"   [ stateTopic="driveway/stringtest"]
    }
    Thing topic thermostat1 "Thermostat 1" {
    Channels:
        Type string : temperature1  "Temperature One"   [ stateTopic="tstat1/temperature"]
        Type number : temperature1num  "Temperature One Num"   [ stateTopic="tstat1/temperature"]
        Type string : setpoint  "Set point"   [ commandTopic="tstat1/setpoint"]
        Type string : humidity1  "Humidity One"   [ stateTopic="tstat1/humidity"]
        Type string : request "tstat request"   [ stateTopic="tstat1/request"]
    }
    Thing topic thermostat6 "Thermostat 6" {
    Channels:
        Type string : temperature6  "Temperature Six"   [ stateTopic="tstat6/temperature"]
        Type number : temperature6num  "Temperature Six Num"   [ stateTopic="tstat6/temperature"]
        Type string : setpoint  "Set point"   [ commandTopic="tstat6/setpoint"]
        Type string : humidity  "Humidity Six"   [ stateTopic="tstat6/humidity"]
        Type string : request "tstat request"   [ stateTopic="tstat6/request"]
    }
    Thing topic thermostat7 "Thermostat 7" {
    Channels:
        Type string : temperature7  "Temperature Seven"   [ stateTopic="tstat7/temperature"]
        Type number : temperature7num  "Temperature Seven Num"   [ stateTopic="tstat7/temperature"]
        Type string : setpoint  "Set point"   [ commandTopic="tstat7/setpoint"]
        Type string : humidity  "Humidity Seven"   [ stateTopic="tstat7/humidity"]
        Type string : request "tstat request"   [ stateTopic="tstat7/request"]
    }
    Thing topic thermostat10 "Thermostat 10" {
    Channels:
        Type string : temperature10  "Temperature Ten"   [ stateTopic="tstat10/temperature"]
        Type number : temperature10num  "Temperature Ten Num"   [ stateTopic="tstat10/temperature"]
        Type string : setpoint  "Set point"   [ commandTopic="tstat10/setpoint"]
        Type string : humidity  "Humidity Seven"   [ stateTopic="tstat10/humidity"]
        Type string : request "tstat request"   [ stateTopic="tstat10/request"]
    }
    //Motion 70: not in use
    Thing topic motion70 "Motion sensor 70" {
    Channels:
        Type string : motion "Motion in garage" [ stateTopic="motion70/motion"]
    }
    //Motion 71: garage
    Thing topic motion71 "Motion sensor 71" {
    Channels:
        Type string : motion "Motion in garage" [ stateTopic="motion71/motion"]
    }

    Thing topic solarDHW "Solar DHW" {
    Channels:
        Type number : SolDHWPress  "Solar DHW Pressure"           [ stateTopic="SDHW/pressure"]
        Type number : SolDHWTemp   "Solar DHW Temperature"    [ stateTopic="SDHW/temperature"]
        Type string : LightState             "Light State"                          [ stateTopic="SDHW/lightstate"]
    }
}

Thanks @drmacro

Could you please let me know what is the advantage of a second RPi for MQTT broker?

I did a first test with text file configration - but got some errors:

[INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to 'localhost' with clientid paho308873053094 and file store '/var/lib/openhab2/mqtt/localhost'
==> /var/log/openhab2/events.log <==
[hingStatusInfoChangedEvent] - 'mqtt:broker:mosquitto' changed from OFFLINE to OFFLINE (COMMUNICATION_ERROR): Timeout
[hingStatusInfoChangedEvent] - 'mqtt:broker:mosquitto' changed from OFFLINE (COMMUNICATION_ERROR): Timeout to OFFLINE (COMMUNICATION_ERROR): Unable to connect to server

My biggest reason is to not have a single point of failure. That…and I had the broker running already.

Since I’ve never used the OH MQTT broker…I’m not sure what those error might be.

Please Note:
An MQTT Broker is needed AND the “MQTT Broker Thing” is Not an MQTT Broker!

So, his Bridge line needs to refer to an existing broker and these errors imply the Bridge line is wrong and/or the broker has not been created?

Good point - I think this is the problem.
Since I’ve installed the whole system based on text files the MQTT broker is missing. Which leads to the message Unable to connect to server

So, the question now is: how to set up the openHAB MQTT broker (for MQTT2.4 binding) with text files?
NB: I think it’s located in Configuration/Services/MQTT as “MQTT system broker connection / org.eclipse.smarthome.mqttbroker” in PaperUI.

You can install an external Broker ( like Mosquitto) or install the (openhab ) embedded broker.

So, if you have an MQTT broker already running on another device, you should refer to it, like this:

Bridge mqtt:broker:mosquitto [host="192.168.1.2", port="1883", secure="AUTO", username="--redacted--", password="--redacted--"]
{
   Thing topic thing-name "Description" {
       Channels:
           Type number : SomeChannel   "Some explanation"   [ stateTopic="<MQTT Topic>", transformationPattern="JSONPATH:$.something" ]
           ... you get the idea...
   }
}

Fill in the IP address of the other Pi (and port and userid/password if applicable). Then define your channel(s) and use the name “SomeChannel” in your item definition, like this:

Number MyItem "Description [%.3f]" <icon> (groups) {channel="mqtt:topic:mosquitto:thing-name:SomeChannel"}

Hope that gets you going.
BTW, the JSON transform is just an example.

1 Like

Can this be done with text files for “unattended”/automated installation also?

Just to make sure which one is the right syntax for MQTT2.4 binding?

This one:

… so for example: Thing topic shelly1_1

or this:

My example is copied directly from my working MQTT setup, only changed some names to make it generic and show the relation between name fields.

If you define the things within the parantheses of the bridge (MQTT broker) you can use the shorter thing definition. I do define my MQTT things in a separate file, therefore I need the longer syntax to make the reference to the binding and broker.

For the MQTT broker: Mosquitto can be installed via the Openhabian console.
sudo openhabian-config
Select
20 | Optional Components
then
23 | Mosquitto
and follow the installation instructions.

This will install MQTT on the same machine as OpenHAB, the broker IP should be 192.0.0.1 then.

Personally I don’t see this as a problem (so far). If my OpenHAB-Pi breaks down I don’t have any need for my sensors to reach the broker. (All my clients only exchange data with or via OpenHAB.)
Vice versa if I’d add another Pi for the broker then there was just another point of failure. I’d have two Raspberries that need to run perfectly to keep the system working. In my understanding that makes the system less reliable. But maybe I missed a point here.

I don’t think you can install a MQTT broker via text file as this is basically not an OpenHAB-internal part. But maybe @David_Graeff can shed some light on this matter.

Out of curiosity and to provide more useful support: What’s the background for your limitation to text files? Can you use the console? (e.g. with Putty)

Ok - I’ve installed the broker via console. Now everything works as expected. It seems it’s the only way to install it …

While setting up a new openhab installation on SSD, I would like to get rid of the mixture of text and Paper UI configuration as it was in the past.
Text files seems for me the better approach when it comes to backup or installing a new system from scratch.

Great to hear its working now.
Indeed I personally also prefer the file based configuration. Not sure if its actually true but at least I have the illusion of having full control over the configuration and knowing exactly where to look for something. :wink:

2 Likes