How to: MQTTv2 Eventbus rules

PaperUI.

Tried the .things as well, with a different name but same error…

Strange…
Are you sure you are using MQTT version 2 and not MQTT v1?

2.4.0 to be exact
Do I need to configure something in PaperUI (PublishTrigger, …) ?

2.4.0 is your openhab version, not the MQTT Binding version.

Can you open a karaf console and show the output of:

bundle:list | grep MQTT

Should show somthing like:

232 │ Active   │  80 │ 1.2.0                  │ Paho MQTT Client
234 │ Active   │  80 │ 0.10.0.oh240           │ Eclipse SmartHome MQTT Binding
235 │ Active   │  80 │ 0.10.0.oh240           │ Eclipse SmartHome MQTT Thing Binding
236 │ Active   │  80 │ 0.10.0.oh240           │ Eclipse SmartHome MQTT Transport Bundle

Yes in PaperUI, you need to add PublishTrigger Channel to your broker.

It looks identical:

225 x Active   x  80 x 1.2.0                  x Paho MQTT Client
226 x Active   x  80 x 0.10.0.oh240           x Eclipse SmartHome MQTT Binding
227 x Active   x  80 x 0.10.0.oh240           x Eclipse SmartHome MQTT Thing Binding
228 x Active   x  80 x 0.10.0.oh240           x Eclipse SmartHome MQTT Transport Bundle

About that Publish Trigger Channel. From my understanding this is needed to react on published messages on the broker. But I need to publish to the broker…

Did you try publishing to your broker with the mosquitto_pub command?

Your understanding of the Publish trigger channel is correct.
Once that channel is created you will notice all mqtt messages in karaf’s log:tail

Didn’t try that one.
But other items created via paperUI are working correctly.
I’m able to receive and publish messages with these items, it’s just the rule that gives me problems.
So there is nothing wrong with the broker itself.

For some reason it’s working now.
Unfortunatly I’m unable to pinpoint where it went wrong.
When I output the ‘actions’ variable to the console (logInfo) it still gives me a null value, but the broker is receiving messages.
The only difference is that I removed whitespaces before and after the equation mark, and I created a new broker connection (for testing) in PaperUI.
Now both of the broker connections are working.
Thanks for the help.

Glad it works!

Found why it wasn’t working, thanks to @vzorglub:

The variable ‘actions’ needs to be declared globally.
After this it works.

Sorry, but can you or @vzorglub please explain why the actions variable needs to be declared globally? As a programmer I would consider the declaration in the if blocks cleaner and would want to avoid global variables whereever possible.

In his case, he needed the action defined as a global because he was calling them inside timer lambdas.
You should be able to do have a local action.

@pascal

There’s an error in the “Publish state to broker” rule. There’s no such thing as “receivedCommand” in the case of a member changing. What you need to do is “triggeringItem.state.toString()” instead. Maybe you want to update your code.

For those who want to stick closer to the original publish/subscribe topics of the old evenbus (for example because your master or client is not openhab). Check the eventbus.cfg file for the original topics. Change the “publish to xxx” rules accordingly and instead in the subscribe “Subscribe to broker” rule change line “var mqttcommandorstate = mqtttopic.get(mqtttopic.length-1)” to “var mqttcommandorstate = mqtttopic.get(2)”

With these simply modifications I could re-use my old master which is a HomeControl Android App where I didn’t want to remap hundreds of mqtt paths

1 Like

@marcelser
I see what you mean. Corrected it.

1 Like

This rule works perfectly. I would like to extend it with a system start so that all values ​​of the items are sent when the system starts up. Is this possible?

Thnx

You shouldn’t need to. The rule will trigger when items change value regardless. That will happen at start-up to
You could add a trigger to the group building rule:

rule "populate dynamic group dg_AllItems"
when
        Time cron "0 0/1 * * * ?" or
        System started
then
        // add all items to the group
        ScriptServiceUtil.getItemRegistry.getItems().forEach[item | dg_AllItems.addMember(item) ]

        // remove all items which no longer exist
        dg_AllItems.members.filter(s | ScriptServiceUtil.getItemRegistry.getItems().filter[i | i.name.contains(s.name)].length == 0).forEach[item | dg_AllItems.removeMember(item)]

        // remove the group from itself (odd.)
        dg_AllItems.removeMember(dg_AllItems)

        //DEBUG dg_AllItems.members.forEach[ item | logInfo("dg_AllItems_member",item.name) ]
end

Thanx

It does not work unfortunately. I would like to send all statuses cyclically.

When I changed the cron to send every minute

rule “populate dynamic group dg_AllItems”
when
Time cron “1 * * * * ?”

    //Time cron "0 0/1 * * * ?"

then
// add all items to the group
ScriptServiceUtil.getItemRegistry.getItems().forEach[item | dg_AllItems.addMember(item) ]

    // remove all items which no longer exist
    dg_AllItems.members.filter(s | ScriptServiceUtil.getItemRegistry.getItems().filter[i | i.name.contains(s.name)].length == 0).forEach[item | dg_AllItems.removeMember(item)]

    // remove the group from itself (odd.)
    dg_AllItems.removeMember(dg_AllItems)

    //DEBUG dg_AllItems.members.forEach[ item | logInfo("dg_AllItems_member",item.name) ]

end

I’ll just ask if anyone has any ideas. In openHAB 2 I still used the Legacy 1.x binding for MQTT for a long time. It works stably. With this rule I have occasional problems that some things have to be sent out twice. Has anyone here had similar problems and a solution? Otherwise, the principle works with master and slave and vice versa.

So thank you very much for your suggestion.

Cloned my VM and then upgraded to openHAB 3. I hoped that the rule would work in the same way. The rule is at least displayed as being present. But with mosquitto_sub -v -h localhost -p 1883 -t ‘#’ I can’t see that anything has been published, even after status changes.

In my opinion, this can be due to two things and maybe someone here can help me. Either a difference between the bindings MQTT2 and MQTT3. Or differences between openHAB 2.5 (where it ran) and openHAB 3.

Off course I looked inside the MQTT 2.5+ Event Bus topic. The Rules DSL does not work either. Jython works.

I found Jython more cumbersome. I also adapted the mqtt_eb files a bit to create a solution similar to the one above. Unfortunately, it doesn’t work nearly as well as this rule in openHAB 2.

My first insight seems to be that the rule DSL does not work. Maybe someone can confirm this for me. Since I got a few things to work unstably in Jython, I am now simply trying out certain adjustments that I can take from https://github.com/openhab-scripters/openhab-helper-libraries/pull/257. My first attemp does not work either.

//Edit: I got the solution. I have to change

import org.eclipse.smarthome.model.script.ScriptServiceUtil

to

import org.openhab.core.model.script.ScriptServiceUtil

Your openhab.log should have told you about that.