[SOLVED] OpenHAB2 using MQTT Binding - receiving a published string on a subscribed topic

Hi all,

I am on openHAB latest running on an Ubuntu container. Jdk8 headless from apt. I have mosquitto running on a different container.

MQTT seems to be working fine using the MQTT binding which I have setup to look at the Mosquitto container IP address. I have taken the leap for this setup and am configuring everything in PaperUI (I used to only use the config files). Using the HomeAssistant style autodiscovery and so far my Sonoff Basics, Sonoff 4ch, Hue and Google devices are working well in the Control tab of PaperUI.

I’ve hit a stumbling block when trying to setup a Sonoff RF 433 Bridge (Tasmota 6.6.0). The only Thing showing up is status, nothing for the 16 RF inputs; I sortof expected this.

The channel in the MQTT Binding that I have added subscribes to:

sonoff-rf/tele/RESULT

Am I right in assuming the only way to configure the RF is to do it the old fashioned way using the config files?

I am receiving MQTT messages from the RF and it is showing up in Karaf:

15:49:57.034 [INFO ] [smarthome.event.ChannelTriggeredEvent] - mqtt:systemBroker:mosquitto:1 triggered {“RfReceived”:{“Sync”:17230,“Low”:550,“High”:1670,“Data”:“623386”,“RfKey”:1}}

For info, the only bit I am interested is the value of “RfKey” which will be from 1 to 16.

How do I extract this string, so that I can then do the usual JSONPATH transform on it which I am comfortable with?

I tried writing a rule (I have others working fine on Things/Items that were auto discovered), which does fire, but the “receivedEvent” is always coming through as null:

rule "RF Channel 1"
  when Channel "mqtt:systemBroker:mosquitto:1" triggered
then
    rf_ch_1.postUpdate(transform("JSONPATH", "$RfReceived.RfKey", receivedEvent))
end

The error in Karaf:

16:03:54.236 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'RF Channel 1': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.transform.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null

and the corresponding item for completeness:

Switch rf_ch_1

Happy to provide more info if I’ve missed anything.

Ideally I’d love to configure the payload condition in the MQTT Binding Channel to only fire if RfKey == 1 but I think you can only put values to compare against, and not a JSON parse? Then I could have a channel for each of the 16 keys.

Using MQTT - Bindings | openHAB for reference.

Kind regards

Simon

Not at all. This can be done using PaperUI. But I think we need more information.

First of all, that Channel you created on the MQTT Broker Thing cannot be linked to an Item. Instead it can be used to directly trigger a Rule. So your Rule will be triggered for ALL messages published to that topic, not just the one RfKey. Your rule doesn’t make clear whether you understand that or not.

Hmmmm. There might be a bug here. Did you define a separator character on the Channel?

image

I can confirm that that works.

Are you certain that receivedEvent is always null or is it the JSONPATH transformation that is returning null? Add logging to verify.

I would expect the JSONPATH to be $.RfReceived.RfKey (notice the dot between $ and Rf).

You can and I would argue should do this, but you will need to manually create a Generic MQTT Thing and separate Channels for each RfKey. Then use MQTT 2.5 M1+ How to implement the equivalent to MQTT1 REGEX filters so the Channel only processes the messages for one of the relays. Then you can link ordinary Switch Items to the individual Channels.

1 Like

Rich,

You are brilliant, thank you so much.

For anyone else trawling the forum who stumbles upon this, what I did based upon the advice above:

  1. Got rid of the .items and .things files. I’m going to use PaperUI ONLY, except for rules.
  2. Manually added a “Generic MQTT Thing” and selected my existing broker.
  3. Added a channel with state topic sonoff-rf/tele/RESULT, then under SHOW MORE, added the incoming value transformation JSONPATH:$.RfReceived.RfKey and ticked the IS COMMAND at the bottom

So now, after linking this thing to a numeric item. Whenever I receive an RfKey message, there is a command dent to the item (so I can trigger a rule) and the value of that item is the respective ID on the Sonoff RF.

I knew the solution was simple and elegant.

Thanks once again, and happy for any feedback on my summary here if I am wrong or there’s a better way!

Cheers