State / read MQTT messages

Hello I try to read State in MQTT messages,

I try to read a LaCrosse TX Sensor via RTL_433.

When I try on shell “mosquitto_sub -h my.host -t RTL_433/JSON” the following appears:
{“time” : “2018-06-22 03:46:48”, “model” : “LaCrosse TX Sensor”, “id” : 111, “temperature_C” : 12.800}
{“time” : “2018-06-22 03:46:49”, “model” : “LaCrosse TX Sensor”, “id” : 111, “humidity” : 16.500}

so my test.item is:

//Item  myItem                                                              { mqtt="<[<broker>       :<topic>     :<type>:<transformer>]"} 
Number  RTL_433_temperature                 "RTL_433_temp      [%.1f]"      { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.temperature_C):.*\"id\" \\: 111,.*]"}
Number  RTL_433_humidity                    "RTL_433_humidity  [%.1f%%]"    { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.humidity):.*\"id\" \\: 111,.*]"}
String  RTL_433_temperatureX                "RTL_433_tempX     [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.temperature_C):.*]"}
String  RTL_433_humidityX                   "RTL_433_humidityX [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.humidity):.*]"}
String	 RTL_433_test                        "RTL_44_info       [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:*]"}

My goal is to get the temperature and humidity displayed in openhab, but I di not even get the whole String from RTL_433_test.
No Values appear in my Test.sitemap:

 Frame label="Aussentemperatur" {
        Text item=RTL_433_temperature
        Text item=RTL_433_humidity
        Text item=RTL_433_temperatureX
        Text item=RTL_433_humidityX
        Text item=RTL_433_test
    }

If you’re not getting even the full/raw string into openHAB but are successfully reading it by connecting to the mosquitto broker via mosquitto_sub, my guess is your MQTT configuration is incorrect in openHAB. Check the contents of your mqtt.cfg file in your openhab-conf/services folder to make sure you have the correct broker.url configured (most likely tcp://localhost:1883 if you’re running the broker on the same host as openHAB).

Also, I’ve seen issues in my setup if I have both the MQTT binding, as well as the MQTT action installed. Uninstall both, then install only the binding, and see if you start getting MQTT strings then.

1 Like

(is this an example entry or are you using this broker alias in your OH2?)
from what I remember, the broker alias does not accept dots due to the parameters structure of the mqtt.cfg file
try to use another alias (and post the contents of your mqtt.cfg to check them)

1 Like

I agree with both @bartus and @Dim
Please post you mqtt.cfg

Also, I am not sure what you are trying to achieve with your REGEX but they are totally unnecessary

//Item  myItem                                                              { mqtt="<[<broker>       :<topic>     :<type>:<transformer>]"} 
Number  RTL_433_temperature                 "RTL_433_temp      [%.1f]"      { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.temperature_C)]" }
Number  RTL_433_humidity                    "RTL_433_humidity  [%.1f%%]"    { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.humidity)]" }
String  RTL_433_temperatureX                "RTL_433_tempX     [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.temperature_C)]" }
String  RTL_433_humidityX                   "RTL_433_humidityX [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:JSONPATH($.humidity)]" }
String	 RTL_433_test                        "RTL_44_info       [%s]"        { mqtt="<[my.host:RTL_433/JSON:state:default]" }
1 Like

The my.host is a example entry, but in reality it is also with dots (more like hostename.fritz.box) as I thought it is the hostname of the broker. And the regex is adopted from https://tech.sid3windr.be/2017/03/getting-your-currentcost-433mhz-data-into-openhab-using-an-rtl-sdr-dongle-and-mqtt/.

I changed the name in the mqtt.cfg (the only set Item)

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
#<broker>.url=tcp://<host>:1883
LocalMQTTbroker.url=tcp://localhost:1883

And the broker name in in my Test.sitemap to “LocalMQTTbroker” and now they work" :slight_smile:

Could you or someone how the “transformer” has to be?
In Item “RTL_433_temperatureX” it seem to work with “JSONPATH($.temperature_C):.*]” but I don’t know why. I took it out of the example I found, The Docs: https://www.openhab.org/addons/bindings/mqtt1/#item-configuration-for-inbound-messages brings not much clarity. Execpt, that i could use default, wich now works on the Item RTL_433_test (gives the whole string). so what can I use besides the JSONPATH?

But you already helped me very much!

As I said, you don’t need the regex bit. The JSONPATH will do the trick on its own.

1 Like

Just to elaborate a bit.

The regex part of the MQTT incoming config is a message matching mechanism. When a message is published on that topic the binding will first check it to see if it matches the regular expression and if it does it passes it to the transformation and assigns the result to the state of the Item. If it doesn’t match then it ignores that message for that Item.

By using .* as the regular expression at the end you are essentially saying “match all messages”. Since that is the default behavior of the Binding, supplying the .* is redundant and unnecessary.

So while it isn’t wrong to include them, it is not necessary. It is not causing any problems.

Most things cleared up, ony the regex expressions are a bit confusing, as I have to double escape some characters unlike the normal regexes I knew. But in my case I need the regexes, because if i did not use them, the JSONPATH throws warnings in the openhab log.