openHAB2 MQTT2 binding - multiple brokers

The documentation on MQTT2 is decribing how to use actions with the new binding.
When using this, I do get an unexpected behaviour when having more than one broker.
Depending on the sequence in the code, the messages are send to the wrong broker.

The following rule works as expected:

// mqtttest.rules
// https://www.openhab.org/blog/2018-12-16-mqtt-arrives-in-the-modern-openhab-2-x-architecture.html

val String filename = "mqtttest.rules"

//----------------------------------------------------------------------------------------------------------------------
rule "MQTT test"
when
    Item MQTT_Test_switch changed from OFF to ON
then
    val myBroker20  = getActions("mqtt","mqtt:broker:MqttPandora20")
    logInfo(filename, "myBroker20.publishMQTT")
    myBroker20.publishMQTT  ("house/test",          "myBroker20") 

    val myBroker75  = getActions("mqtt","mqtt:broker:MqttOpenHAB75")
    logInfo(filename, "myBroker75.publishMQTT")
    myBroker75.publishMQTT  ("house/test",          "myBroker75") 
    // RESULT: Messages are send to the correct broker
end

In the next example both messages are send to the second broker:

//----------------------------------------------------------------------------------------------------------------------
rule "MQTT test"
when
    Item MQTT_Test_switch changed from ON to OFF
then
    val myBroker20  = getActions("mqtt","mqtt:broker:MqttPandora20")
    val myBroker75  = getActions("mqtt","mqtt:broker:MqttOpenHAB75")

    logInfo(filename, "myBroker20.publishMQTT")
    myBroker20.publishMQTT  ("house/test",          "myBroker20") 

    logInfo(filename, "myBroker75.publishMQTT")
    myBroker75.publishMQTT  ("house/test",          "myBroker75") 
    // RESULT: Both Messages are send to the second broker
end

The brokers are defined in a separate .thing file:

// bridge.things

Bridge mqtt:broker:MqttPandora20 "MQTT Broker Pandora" @ "MQTT Pandora" [ 
  host="192.168.1.20",
  port=1883,
  secure="AUTO",
  qos=0,
  retain=false,
  clientid="Oh2Mqtt2Thing20",
  keep_alive_time=30000,
  reconnect_time=60000,
  username="mqttusr20",
  password="mqttpass20"
]

// Bridge mqtt:broker:MqttOpenHAB [ host="localhost", port=1883, secure="AUTO", username="openhab", password="nxt2008" ]
Bridge mqtt:broker:MqttOpenHAB75 "MQTT Broker OpenHAB" @ "MQTT OpenHAB" [ 
  host="192.168.1.75",
  port=1883,
  secure="AUTO",
  qos=0,
  retain=false,
  clientid="Oh2Mqtt2Thing75",
  keep_alive_time=30000,
  reconnect_time=60000,
  username="mqttusr75",
  password="mqttpass75"
]

May be I am interpreting the documentation incorrectly as I was assuming that the two instances are independent.

@Kai A bug in getActions?

This is indeed a (conceptional) bug in getActions:frowning:
I have created an issue for it.

2 Likes

And I have created a PR as a fix for it.

2 Likes

Should be included in latest distro 1487 - please test!

The issue is fixed in the release: openHAB 2.5.0~S1487-1 (Build #1487)

Thanks a lot for the very quick resolution.