MQTT Binding (v1.11) Getting Started 101

don’t we need a… Step 0. Install the Mosquitto MQTT Broker:

sudo apt-get update
sudo apt-get install mosquitto

:slight_smile:
@rlkoshak @vzorglub

Under “A few resources first…” it already says:

An MQTT broker is needed to proceed! For testing purposes you can use an open one but it’s better to install one locally. A good option is Eclipse Mosquitto on a normal Linux system. Mosquitto is part of the optional components of openHABian

So it is already listed as a prerequisite. I don’t know if installation instructions for Mosquitto on all the different OS’s would be in scope for this tutorial. And since the rest of the tutorial is OS independent I’d be hesistant to just add the instructions for apt based Linux.

2 Likes

Thank you - got it working!

Full credit to you for taking the initiative, if I only had this when when I stated…:wink:

Just an FYI, I had a power failure at my house while on holidays and when I got back, Openhabian was up and running but none of my MQTT items were working.

After uninstalling the binding, pulling my hair out, et cetera, I did what I should have done at the beggining - restarted Mosquitto - and everything started working again.

One thing that did change was I now get this error (none of my items had changed post the power failure for this to begin occuring):

2018-07-21 11:40:04.558 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'Xiaomi_Switch_Ceiling'

The item this relates to is this:

Switch Xiaomi_Switch_Ceiling "In Line Fan Switch [%s]" { mqtt=">[broker:zigbee2mqtt/0x00158d000215fcfb/set:command:*:default], <[broker:zigbee2mqtt/0x00158d000215fcfb:state:default" }

Any thoughts as to why this error is popping up?

When Items are first initialized after OH starts they get initialized to NULL, meaning basically an unknown state. The NULL states is meaningless to devices so OH won’t try to send it.

You need to figure out why Xiaomi_Switch_Ceiling is NULL and get it set to ON or OFF. Most use persistence with restoreOnStartup to initialized items with whatever state they were is when oh went down.

Dumb question… Is this still current for openHAB 2…
I been trying to get MQTT working without success and i previously ignored this page as i was thinking the (V1.11) title was for an old version?

Want to check before i follow this guide…

No go ahead a follow this.
It works. But don’t miss a step…

1 Like

Thanks @ThomDietrich, Great Tutorial! Just my $.02: one thing I would suggest is, in Step 6 is to give an example as to how to publish a simple message from a client. For example, for those using mosquitto on OpenHABian, it would be:

[08:05:51] openhabian@openHABianPi:~$ mosquitto_pub -t "testing/mqtt/topic" -m "ON"
[08:05:53] openhabian@openHABianPi:~$ mosquitto_pub -t "testing/mqtt/topic" -m "OFF"

some people may not be familiar with the syntax in the command line for publishing a message, and the above commands are not listed in any of the linked documentation. This would at least get them started with a basic usage command and test the install. They can then go and search to learn how to write more in-depth / complex messages, configure Last Will topic/message, wildcards, etc.

~John

Anyone here that is in a position to clarify whether the “Embedded MQTT Broker” (available in Paper UI under Add-ons / Misc) is now “production ready” to replace a separate Mosquitto installation?

BTW; I am setting up a clean system based on version 2.3.0 stable.

Hi
I was running openHAB 2.1 on openhabian. My MQTT server (Synology box) decide to stop working, so I decided to upgrade openhabian to the openHAB 2.3 version and in addition, install a few optional apps like Mosquitto.
I performed the upgrade using the openhabian tool in the following order 01,02,03 then selected 20 and added items such as Log viewer, InfluxDB…, NodeRE and Mosquitto.
The only upgrade/install that encountered an error was Mosquitto.

The following packages have unmet dependencies:
_ mosquitto : Depends: libwebsockets3 (>= 1.2) but it is not installable_
E: Unable to correct problems, you have held broken packages.

How do I overcome the libwebsockets3 problem?

Thanks,
Neil.

I wasn’t aware that Synology supported openHABian. This error seems to point in that direction. I don’t have any advice to offer beyond a Google search for how to install Mosquitto on Synology and doing it by hand.

Hi Rich,
Sorry, I wasn’t clear enough. My Synology box was only the MQTT server. It played no other part in openHAB.

openHAB is installed on a RPi 3 ModelB+ with the standard openhabian Pi image. I have been running it this way for a few years now.
Now that MQTT on the Synology has died, I decided to implement the openhabian option of Mosquitto on my OH Pi and this is where the error now appears on my OH Pi install after running the Mosquitto menu option.
Neil.

Ok, that is different and very wired.

I imagine you get the same error if you run sudo apt-get install mosquitto?

Did you install openHAB2 with openHABian in the first place? Which openHABian version did you use if so?

Maybe instead of searching for hours, the easiest solution would be to backup your configuration (use the openHAB backup script) and start from scratch (i.e. copy the actual openhABian version to a new (or formatted) SD Card and do a complete installation. Afterwards recover from backup.

Hi Rich,
I will be away for the next two weeks.

I will give it a go when I get back.

Hi @NW27,

This is a known issue with mosquitto on debian stretch, see https://www.raspberrypi.org/forums/viewtopic.php?t=206743 which is a pretty helpful post in general.

About halfway down someone flags the same issue and offers a solution of running:

sudo apt install aptitude; aptitude install mosquitto libmosquitto1 mosquitto-clients

It allows you to clean up the install and chose an alternative version to ensure compatibility, I have rolled back to mosquitto 1.4.10 on my pi1 successfully after running into the same problem. I have also successfully posted messages to Openhab and to a newer mosquitto broker so it should all work.

Hope that helps.

Andrew

1 Like

sorry for reopening but mine won’t start working …

my steps till yet:
0. install mosquitto

  1. install openhab2, mqtt binding and service (an action I do not find anymore)
  2. configured openhab2 for mqtt
    file service/mqtt.cfg
    broker.url=tcp://openhab.example.com:1883 broker.clientId=openhab
  3. restart openhab2, restart mosquitto, start mosquitto client
    4.I can watch openhab2 connecting to the broker
  4. after creation of an item (and restarts and combinations of new installations of service and binding), I won’t get a message. The item is copy&pasted as in the first post:
    file items/home.item
    ‘//test
    Switch MQTT_Test “Testing…” { mqtt="<[broker:testing/mqtt/topic:state:default], >[broker:testing/mqtt/back-topic:command:*:default]" }’
    file sitemaps/home.sitemap

    sitemap home label=“Home” {
    Frame label=“Test” {
    Switch item=MQTT_Test
    }
    }

I got to this problem, as I was using mqtt in my rules, where I got the following error message:
‘The name ‘publish’ cannot be resolved to an item or type; line …’
so what am I missing?
thanks for help in advance

Are you using openHAB 2.4? Have you read the release notes and breaking changes? Just asking :wink:

yes and I think no. I am sorry, but where do I get them?
The first post I have read as awake as I am still am.

(I am a bit of frustrated, as I am already some hours about it, again …)

edit:
you are talking about this wiki-entry?