How to Publish Items to MQTT Using MQTT Binding

  • Platform information:

    • Hardware: RPi
    • OS: Openhabian
    • openHAB version: 2.5.1-2
  • Issue of the topic:
    I have spent countless hours trying to understand how the MQTT binding works and can’t seem to figure it out. I have read and re-read the wiki. I understand that MQTT has a broker and a client. I also understand that their have been two versions of the MQTT binding within openhab. I am using the Embedded Broker included in Openhab 2.5 and I am using the MQTT System Broker Connection. All settings here are default. My broker host is 127.0.0.1 for everything.

Do I have to create a rule to check the MQTT server? If so where do I put that?

I am trying to create a simple switch that will publish to MQTT.

MQTT : Topic : Value (correct me if this is the wrong structure)
Openhab / Relay / 1 / Value

  • Items configuration related to the issue
    /Relays

Switch channel1 “Channel 1” { channel=“mqtt:topic:openhab:relay:1” }
Switch channel2 “Channel 2” { channel=“mqtt:topic:openhab:relay:2” }
Switch channel3 “Channel 3” { channel=“mqtt:topic:openhab:relay:3” }
Switch channel4 “Channel 4” { channel=“mqtt:topic:openhab:relay:4” }

*Things File
Bridge mqtt:broker:openhab [ host="127.0.0.1” ] {

         Thing topic relays {

            Channels:
           Type switch : channel1 "Relay Channel 1" [ stateTopic="openhab/relay/1",
                  commandTopic="openhab/relay/1/set", on="ON", off="OFF" ]
           Type switch : channel2 "Relay Channel 2" [ stateTopic="openhab/relay/2",
                   commandTopic="openhab/relay/2/set", on="ON", off="OFF" ]
           Type switch : channel3 "Relay Channel 3" [ stateTopic="openhab/relay/3",
                   commandTopic="openhab/relay/3/set", on="ON", off="OFF" ]
           Type switch : channel4 "Relay Channel 4" [ stateTopic="openhab/relay/4",
                   commandTopic="openhab/relay/4/set", on="ON", off="OFF" ]

             }
      }
  • Sitemap configuration related to the issue

sitemap defualt label=“Outlets”
{
Frame label=“Outlet Control1” {
Switch item=channel1
Switch item=channel2
Switch item=channel3
Switch item=channel4
}
}

Thank you! Any advice is greatly appreciated!!!

If you look in openhab.log , you should find messages from the binding about connecting to the broker.

I was in the middle of reboot hell there… Ha!

Here is what the log said relating to MQTT

020-02-01 01:22:29.301 [WARN ] [g.mqtt.handler.AbstractBrokerHandler] - 
Tried to unsubscribe org.openhab.binding.mqtt.homeassistant.internal.discovery.HomeAssistantDiscovery@190e4 2e
from  discovery topic +/+/$homie on broker mqtt:broker:1d93a54d but topic not registered at all. Check discovery logic!

2020-02-01 01:22:30.941 [ERROR] [org.openhab.binding.mqtt            ] - bundle 
 org.openhab.binding.mqtt:2.5.1 (262) 
[org.openhab.binding.mqtt.internal.discovery.MqttServiceDiscoveryService(268)] : The 
activate method has thrown an exception
java.lang.IllegalArgumentException: ID segment 'Openhab MQTT Broker' contains invalid 
characters. Each segment of the ID must match the pattern [A-Za-z0-9_-]*.

Also I see this…

2020-01-31 18:40:14.671 [ERROR] [org.openhab.binding.mqtt            ] - bundle org.openhab.binding.mqtt:2.5.1 (262)[org.openhab.binding.mqtt.internal.discovery.MqttServiceDiscoveryService(268)] : The activate method has thrown an exception

Also this:

2020-01-31 18:40:19.056 [INFO ] [.reconnect.PeriodicReconnectStrategy] - Try to restore connection to '127.0.0.1'. Next attempt in 60000ms

2020-01-31 18:40:19.908 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to '127.0.0.1' with clientid 7e5f5fba-7429-4c2e-add5-0804fbfa11bb

2020-01-31 18:49:00.254 [WARN ] [.MqttBrokerConnectionServiceInstance] - MqttBroker connection configuration faulty: ID segment '127.0.0.1' contains invalid characters. Each segment of the ID must match the pattern [A-Za-z0-9_-]*.

2020-01-31 18:49:15.397 [WARN ] [.MqttBrokerConnectionServiceInstance] - Ignore existing broker connection configuration for: 127.0.0.1

Here is more from the log…

2020-01-31 19:26:24.124 [.ItemChannelLinkAddedEvent] - Link ‘channel1-mqtt:topic:openhab:relays:1’ has been added.

2020-01-31 19:26:24.174 [.ItemChannelLinkAddedEvent] - Link ‘channel2-mqtt:topic:openhab:relays:2’ has been added.

2020-01-31 19:26:24.213 [.ItemChannelLinkAddedEvent] - Link ‘channel3-mqtt:topic:openhab:relays:3’ has been added.

2020-01-31 19:26:24.255 [.ItemChannelLinkAddedEvent] - Link ‘channel4-mqtt:topic:openhab:relays:4’ has been added.

Save yourself some time and uninstall this as it’s no longer supported.

You can install mosquitto broker from sudo openhabian-config tool then add the mqtt binding (2.x version) found in PaperUI under Bindings.

After that stop OH, clean the cache, restart OH and after fully restarted (watch the logs, frontail) you may need to reboot once or twice if you see lots of errors in logs.

Clean cache commands:
sudo systemctl stop openhab2

sudo openhab-cli clean-cache

sudo systemctl start openhab2

No need to stop OH for reboot just use:
sudo reboot

3 Likes

For those trying to resolve issues in MQTT…

Here is exactly what I did to fix it.

I removed the old MQTT binding and Broker from Things in Paper UI.

Next from terminal, I stopped Openhab2 and cleaned the cache:

Clean cache commands:
sudo systemctl stop openhab2

sudo openhab-cli clean-cache

Next I ran:
sudo openhabian-config

Select Optional Components

Select Mosquitto

Once that finishes… Exit config.

Reboot Openhabian.
Sudo Reboot

Open the PaperUI

Select Bindings

Install:

MQTT Embedded Broker
MQTT system broker connection

You’ll have to still go into Services and configure each of those. Also you will need to likely install a Generic MQTT Thing for any custom items.

Good luck!

Thanks for all of the help!!!