MQTT Binding : Simplest Configuration Doesn't Work (OpenHAB Documentation Example)

Hi There :slight_smile:

The Simplest Configuration of OpenHAB/MQTT-Binding, According To a OpenHAB Documentation Example, Just Refuse To Work :frowning:

& Although I’ve Put Through a Detailed OpenHAB PR (#4589) Regarding This Issue,
The MQTT-Binding Maintainer Seem To Believe That This Isn’t a OpenHAB Bug.

Did I Miss/Mess Anything Important… ? :slight_smile:

Many if not most of the users on this forum are successfully using the MQTT binding.

The appropriate place to get help with configuration and problems like these (as @watou suggested twice) is here on this forum. Only after a discussion here and consensus reached should an issue be raised on github.

The problem is one of configuration, not in the code. Therefore a github issue is not appropriate and why it was closed.

Have you verified in the logs that the MQTT binding is successfully connecting to the broker?
Have you use a test tool like mqtt_sub to verify that messages are being posted to the broker’s topics?
Have you verified there are no error in the log when sending/receiving messages?
Have you configured your Items exactly as @watou suggested in the thread on github?

I’m by no means a mqtt guru, but with my setup I can’t have the leading / before the openhab.

e.g. {mqtt=">[broker:openhab/test123456/cmd:command…

In any case, you’d need to post some logs so someone can review what the errors actually are before figuring it out further.

1 Like

Yes, I’ve Verified The OpenHAB/MQTT-Binding<>MQTT-Broker Connection :
Both By Inspecting The OpenHAB Log ( See : An Exemplary Log Snippet (As On PR #4589) ) & By Using a MQTT Client (Mosquitto mosquitto_sub) To Inspect The Defined ‘state’ MQTT-Topic.,

Also, I’ve Configured My Items EXACTLY As Suggested By @watou .


No Messages Are Posted By OpenHAB To Any Of The Defined MQTT-Topics.,

Also, As Can Be Seen In The Aforementioned OpenHAB Log Snippet, OpenHAB Seem NOT To Connect - At All - To The ‘cmd’ MQTT-Topic (Contrary To The ‘state’ MQTT-Topic - To Which It Successfully Connects).


Did I Miss/Mess Anything… ? :slight_smile:

Well, I Already Did Post Relevant Log Files & Configuration Files - On PR #4589, For Anyone Who Wishes To Kindly Assist :slight_smile:

& I’d Gladly Elaborate - As Required To Solve This Mystery (It Is For Me, At Least)…


Update :

Kindly Pardon Me For Missing The Omit-The-Leading-Forward-Slash ("/") Tip.,
While It Didn’t Work For Me, Thanks Nevertheless ! :slight_smile:

I have MQTT working with openHAB 2 and it was very easy to get going and it is extremely reliable.

I found MQTT.fx to be a really useful tool to check that I had two way traffic and that it was all working as expected.

It might help us to help you, if you posted the contents of your mqtt.cfg file and also some samples of your item definitions on this forum.

With The Hope That It Might Provide Some Useful Clue,

Here Is The Relevant Debugging Output Of The Mosquitto MQTT-Broker :

/var/log/mosquitto/mosquitto.log

1472095499: mosquitto version 1.4.9 (build date 2016-08-13 08:57:43+0300) starting
1472095499: Config loaded from /etc/mosquitto/mosquitto.conf.
1472095499: Opening ipv4 listen socket on port 1883.
1472095499: Opening ipv6 listen socket on port 1883.
<...>
1472097488: New connection from 10.0.0.1 on port 1883.
1472097488: New client connected from 10.0.0.1 as openhab2 (c1, k60).
1472097488: Sending CONNACK to openhab2 (0, 0)
1472097488: Received SUBSCRIBE from openhab2
1472097488:     /openhab/test1234/state (QoS 0)
1472097488: openhab2 0 /openhab/test1234/state
1472097488: Sending SUBACK to openhab2
1472097548: Received PINGREQ from openhab2
1472097548: Sending PINGRESP to openhab2

It is proper forum etiquette to post everything the volunteers need to help you with your problem here in this thread. Don’t make us go somewhere else to see your code, logs, etc. About 90% of the time I’m posting from my phone. It is all but impossible to keep switching back and forth between two pages to follow the conversation. Others simply wont bother.

As @pahansen said, he and I have encountered difficulty using topics that start with ‘/’. This is actually documented as undesirable on the mosquitto site.

Remove the leading ‘/’ and make sure your publishers and subscribers are using identical topics.

If that doesn’t work, post your configs and Items here.

Fair Is Fair :slight_smile:

OpenHAB2 Config. :

/etc/openhab2/services/mqtt.cfg

broker.url=tcp://10.0.0.1:1883
broker.clientId=openhab2

/etc/openhab2/sitemaps/main1.sitemap

sitemap main1 label="Main Menu"
{
        Frame {
                Switch item=lamp1 label="Test123 - Lamp1"
        }
}

/etc/openhab2/items/test1234.items

Switch lamp1 "Test123-Lamp1" (all) {mqtt=">[broker:openhab/test123/cmd:command:ON:1],>[broker:openhab/test123/cmd:command:OFF:0],<[broker:openhab/test123/state:state:default"}`

Mosquitto MQTT-Broker Config. :

/etc/mosquitto/mosquitto.conf

max_inflight_messages 1

queue_qos0_messages true

message_size_limit 1048576

autosave_interval 1800

persistence true

persistence_file mosquitto.db

persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

log_type all

connection_messages true

log_timestamp true

allow_anonymous true

Have you tried it without the following on then end of your item definition?

,<[broker:openhab/test123/state:state:default"

I wonder whether trying to return the state in the item definition might be your problem??

I have a similar item setup in my development system but without the state part and it still displays the switch status in the UI. My definition is:

Switch mySwitch "HVAC ON/OFF" <heating> (GF_Mancave , Lights) { mqtt =
">[mqttService:myHouse/services/light:command:ON:1],>[mqttService:myHouse/services/light:command:OFF:0]" }

I Just Did & It Only Caused OpenHAB To Unsubscribe From The ‘state’ MQTT-Topic… :cry:

I would expect that.

What you are trying to do with the command sections of the item is “publish” the command. The thing that you are trying to control to should be subscribed to the same topic.

EDIT: with MQTT.fx running you should be able to view the traffic live by subscribing to the same topic.

You can deal with subscribing to the state topic once you get the command working (if needed).

EDIT2:

You have the item defined in both your sitemap and items file. This might be another issue, but I am not sure.

In my items, I have a line as follows which defines an item group:

Group GF_Mancave "Man Cave" <office> (gGF)

I then have the item as defined in the post above, so it is only defined once.

The relevant sitemap entry for me is as follows:

Group item=gGF label="Ground Floor" icon="groundfloor"

For Watching The MQTT Activity, I Currently Use : Mosquitto’ Log File & Mosquitto’ mosquitto_sub Utility.
I’ll Most Probably Give a Graphic Counterpart, Such As MQTT.fx, a Try Later On.

& Shouldn’t OpenHAB Subscribe To The ‘cmd’ MQTT-Topic… Just Like It Does (Did) To The ‘state’ One ?


My .sitemap Syntax Was Shamelessly Copied From The OpenHAB “demo” One :

https://github.com/openhab/openhab-distro/blob/master/features/openhab-demo-resources/src/main/resources/sitemaps/demo.sitemap

( See Also : https://github.com/openhab/openhab/wiki/Explanation-of-Sitemaps#element-switch )

I am no expert on MQTT, but I don’t see the need to subscribe to a topic being published to get commands to work.

Anyway, it has to be something simple. Hopefully someone else will jump in soon and help you solve the issue.

Well, I Do Believe That OpenHAB Should Subscribe To The ‘cmd’ MQTT-Topic, For Publishing Commands Thru It & Yes, It Has To Be Some Glitch Somewhere…

Thanks A Bunch For Trying To Help ! & If You Figure Something Out Of This, Kindly Update :slight_smile:

Can’t check my own config right now, but two things jumped out:

  1. in Mqtt.cfg you define your clientid as openhab2
  2. in your .items file you’re using openhab/

I would try making them the same, either both openhab2 or openhab.

Cheers
J

No, a subscription is only needed to read messages. A publisher does not subscribe to a the topic it sends messages to.

It isn’t a problem. Putting Items on the sitemap does not create new Items or interfere with existing Items. It does let you control how it appears in ways not possible when only using groups on the sittemap (e.g show a Switch as text, hide Items when they are a certain state, etc.

The “openhab/” is just the first part of the topic name and has no relationship with the clientid

Here is another example working MQTT publishing Item:

Switch  T_D_Garage1_MQTT "Garage Door 1"        <garagedoor> { mqtt=">[mosquitto:actuators/garage1:command:*:default]"}

It doesn’t get any simpler than that. When the switch receives a command it publishes ON or OFF to actuators/garage1 on the mosquitto broker (as defined in the config).

Thanks For The Above Clarification :+1:

& a Quick Recap -

OpenHAB Configuration - Items/Sitemaps :

.items

Switch lamp1 "Test123-Lamp1" (all) {mqtt=">[broker:openhab/test123/cmd:command:*:default],<[broker:openhab/test123/state:state:default"}
Switch lamp3 "Test12345-Lamp3" (all) {mqtt=">[broker:openhab/test12345/cmd:command:*:default]"}

.sitemaps

sitemap main1 label="Main Menu"
{
        Frame {
                Switch item=lamp1 label="Lamp1"
                Switch item=lamp3 label="Lamp3"
        }
}

Summary -

As On PR #4589, While OpenHAB Seem To Be Able To Successfully Communicate With The Mosquitto MQTT-Broker, The OpenHAB Web-UI (Basic) Still Shows No Items At All :

Where is your sitemap file? That screenshot indicates that there is no sitemap file or a default one. In any case, your sitemap file is not being used.

It’s At : /etc/openhab2/sitemaps

I’m Using The .deb CI Package (Converted To .tar.gz & Manually Installed).