[SOLVED] Getting MQTT to do its job with OpenHAB

  • Platform information:

    • Hardware: Rpi3
    • OS: OpenHABian Linux typhon 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
    • Java Runtime Environment: default Openhabian just updated
    • openHAB version: fully update
  • Issue of the topic: I have installed the default broker through the openhabian-config menu and its definately running as I can tail -f /var/log/mosquitto/mosquitto.log which show lots of the following:

    1569027436: New connection from ::1 on port 1883.
    1569027436: New client connected from ::1 as mosqpub|2718-typhon (c1, k60, u’openhabian’).
    1569027436: Client mosqpub|2718-typhon disconnected.
    1569027539: New connection from ::1 on port 1883.

I have created a service file mqtt.cfg in /etc/openhabian2/service as such:

mosquitto.url=tcp://192.168.7.50:1883
mosquitto.qos=1
mosquitto.retain=true
mosquitto.async=false
mosquitto.user=openhabian
mosquitto.pwd=PASSWORD

I’ve created a Thing that looks like this:

thing

Although i’m not quite sure what I’m meant to do with the channels.

I created an item in /etc/openhab2/items/ in the file sensors.item

Number mqtt_kitchen_gas “Gas Level [%.1f]” {mqtt="<[mosquitto:Home/Floor1/Kitchen/Gas_Sensor:state:default]"}

I posted a message in the terminal

mosquitto_pub -u openhabian --pw PASSWORD -t "Home/Floor1/Kitchen/Gas_Sensor" -m 22

but as you can see from the above log, nothing is showing in the mosquitto.log.

I was trying to find a simple guide just to understand how to set MQTT up on the OpenHAB. I used this https://www.smarthomeblog.net/mqtt-openhab/ but I think its too basic and I am missing something fundamental. Surely, I shoudl be able to publish to MQTT and be able to see it come up in the mosquitto.log without even having to configure OpenHAB (i.e. i first need to test that Mosquitto is receiving published messages)?

Any help or pointing to a good and complete guide on how to set MQTT on OpenHAB would be appreciated.

THanks

You did a lot reading, but sadly you stumbled on the “version problem”. The link does give a good explanation of how mqtt works but it is gives instructions for the setup of the version 1 mqtt binding ( while there was no version 2 nobody called the initial one version 1!).
Version 2 works with things, you have created the broker thing ( which acts as the bridge between the broker and openHAB and does NOT need any channels). Now you need to create things for each of your devices.
If you are on stable version (2.4) you should use the Generic mqtt things. If you are on more recent versions ( milestone or snapshot) you could use one of the two other types which allow autodiscovedy if your mqtt devices do follow the respective syntax.
Reading the actual documentation of mqtt would help as well.

After creating mqtt things with channels you can link the channels to items, however your already created items need the change to the version 2 syntax.

Hi,

thanks for replying.

I can see the MQTT broker is online (in the list of things) but is that sufficient to assume its working? How do I debug this? The link to the guide that I posted had the example of posting a message to the broker as I copied above in my previous message. But how do I know if the broker received that message. It seems that

tail -f /var/log/mosquitto/mosquitto.log

is not providing me with the info I need. Maybe this is to do with the level of debug to be added to the mosquitto conf file?

Basically, I don’t have a MQTT thing yet so I’m first trying to work out if the OpenHAB setup is working before I go to the extent of programming all my sensors to publish to MQTT (they currently send a request to Apache server to post sensor reading to mysql but that is superseded now that I have discovered that you have OpenHAB-MQTT-InfoDB-Garfana. seems a lot more versitle and powerful).

So, from what you are saying, I should create a generic thing with maybe number value and then publish to that thing using the string:

mosquitto_pub -u openhabian --pw PASSWORD -t "Home/Floor1/Kitchen/Gas_Sensor" -m 22

??

Checking if messages are running from/to the server a mqtt-viewer like “MQTTfx” is suggested. That way you can see if and what messages are running, even if openHAB didn’t subscribe correctly.
The log messages from mosquitto you showed report some reconnects, that could be related to the incorrect setup with Version 1 syntax and a sole MQTT broker thing.

You probably are referring to InfluxDB- Grafana. InfluxDB is are strong database and Grafana is a good graphic-tool to show graphs from that database.
For a start I would not use such migthy tools, espacially on a single Raspi3. I’m using rrd4j as a database with the build-in tools to shows graphs and my Raspi3 is running good with it (>100 days without reboot), see my sitemap graph below.


or from HABPanel:

Thanks Opus, the MQTTfx app is really neat. I can publish “Home/Floor1/Kitchen/Gas_Sensor” with a numeric value of 9 (e.g. gas level) and subsequently subscribe to it and see it pop up, which means the broker is working.

The challenge is to get it working on OpenHAB. I’ve given up using PaperUI as I find it confusing so going to basics with config files.
Here they are below. I don’t have a gas sensor but I can use MQTTfx as a virtual gas sensor by publishing

Home/Floor1/Kitchen/Gas_Sensor 9

I guess that is the point on MQTTfx, isn’t it?

This is the mqtt.things file for the broker config:

Bridge mqtt:broker:mymqtt "My MQTT Broker: Mosquitto"
[
    host="192.168.7.57",
    port=1883,
    secure="AUTO",
    username="OpenH2",
    password="XXX"
]

This is the gas_sensors.things file for my virtual gas sensor in the kitchen:

Thing mqtt:topic:mymqtt:mymqttSensor  "MQTT Thing: MQTT Gas Sensor"
    (mqtt:broker:mymqtt) @ "Kitchen Gas Sensor"
{
    Channels:
        Type string : number "Number"
            [
                stateTopic = "Home/Floor1/Kitchen/Gas_Sensor",
                commandTopic = "Home/Floor1/Kitchen/Gas_Sensor"
            ]
}

This is the sensors.items

Number mymqttSensor "Gas Level [%1f]"
{
    channel = "mqtt:topic:mymqtt:mymqttSensor:number"
}

For some annoying reason most likely to do with the above config files, the PaperUI status of the sensor under things is OFFLINE. Any thoughts as to where I may be going wrong?

Let me guess, you are on the 2.4 stable release version and you did not restart openHAB or at least the mqtt bundle after creating/changing to mqtt things files ( it might only be the broker thing file).
This is a known bug in this version and it is solved in the actual milestone and snapshot versions.
I am on this version as well, as long as there is no need to change the file(s) it is running fine!
Note: IMHO for a sensor you need only the stateTopic.

Mmm, not sure it went the way i was hoping …

sudo service openhab2 restart

sudo service mosquitto restart

The broker came back online under Things in the PaperUI while the sensors things disappeared :frowning:

What did the log say when openhab restarted?

2019-09-21 17:08:20.347 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'gas_sensor.things' has errors, therefore ignoring it: [8,13]: mismatched input ']' expecting RULE_ID

Ok, its online now. It was the comma that i forgot to delete after I removed the CommandTopic. Now the Thing is online and I’ve linked it to the item. Its showing on the panel although no number/data is showing. I must be posting incorrectly with MQTTfx so it’s probably not picking up.

sensor_things2

I’m wondering if this has something to do with the fact that I’m asking to expect a number but actually it shoudl be a string? I’ve also tried the following:

Thing mqtt:topic:gassensor  "MQTT Thing: Gas Sensor"
     (mqtt:broker:mymqtt) @ "Kitchen Gas Sensor"
{
    Channels:
        Type string : sensor "Concentration"
            [
                stateTopic = "sensor/state"
            ]
}

and the following for the sensor.items

 String Gas_Sensor "Gas Level [%1f]"
 {
     channel = "mqtt:broker:topic:gassensor:sensor"
  }

Interestingly, everytime I sudo services openhab2 restart, it all links nicely and shows in the PaperUI panel. Its just the number that is not being picked up when I publish

sensor/state 6

on the MQTTfx app. Not sure I know how to debug this …

Is it maybe to do with the JSON transformation thing???

But isn’t your broker Thing called mymqtt ?

I thought I needed to simplify things so I thought I would simulate a switch using the example in the MQTT Bindings documentation.

Only one Things file like this:

Bridge mqtt:broker:mymqtt "My MQTT Broker: Mosquitto"
[
    host="192.168.7.57",
    port=1883,
    secure="AUTO",
    username="openH2",
   password="XXXX"
 ]

 {
         Thing mqtt:topic:mything  "MQTT Thing: Gas Sensor" {
         Channels:
                Type switch : lamp "Kitchen lamp" [ stateTopic="lamp/enabled",  commandTopic="lamp/enabled/set" ] 
        }
 }

and an Items file like this:

 Switch Kitchen_Light "Kitchen Light" {channel="mqtt:broker:topic:mything:lamp"}

Interestingly, when I publish the following on MQTTfx:

lamp/enabled 1

for ON status or

lamp/enabled 0

for OFF status, I can see the PaperUI panel switch going on and off respectively. This proves that my broker is working and that I can publish from another machine using MQTTfx to emulate my kitchen lamp.

Now I need to work out how I publish sensor data like temperature or humidity. It has to be either the string or number data type. I’ll mark this thread as soled or it’ll end to infinity :slightly_smiling_face:

I’ll keep you posted. Thank you for your help to this point.