[SOLVED] REGEX im MQTT item

I have a couple of 433MHz devices publishing to an MQTT topic RTL_433/JSON. The power meter looks like this:
{“time” : “2017-12-28 17:53:27”, “model” : “CurrentCost TX”, “dev_id” : 77, “power0” : 2080, “power1” : 0, “power2” : 0} and I need power0 so the item file entry is:
Number PowerMeter “Power [%d W]” (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.power0):.“dev_id” \: 77,.]"}
The dev_id ensures I am only listening to my meter. This works fine, and I can see the readings in the logs.

The second device is a Watchman oil level meter and it’s output looks like this:
{“time” : “2017-12-28 17:53:38”, “model” : “Oil Watchman”, “id” : 137574874, “flags” : 128, “maybetemp” : 28, “temperature_C” : 1.667, “binding_countdown” : 0, “depth” : 86}

As one of my neighbours also has the same device I must select my device id in the item:
Number OilDepth “Depth [%d cm]” (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.depth):.“id” \: 137574874,.]"}

However, this does not work and there is nothing in the logs. The two items are functionally identical but I cannot see why the second one fails. Any ideas?

I forgot to mention that I put both REGEX expressions into an on-line checker and they both failed. However, if I change dev_id from 77 on the power meter to another number, no readings are logged so the expression is working.

Please use code fences to make your Items and JSON easier to read:

```
code goes here
```

I’m pretty sure your REGEX, while syntactically correct, is not going to match the messages you are after.

.*"id" . 137574874.*

You may have to escape the double quotes.

Also, the MQTT binding won’t let you use :, even if you escape it, in your REGEX so I use . to match any one single character.

Finally, your REGEX must match against the entire message, not just a part of the message. So you must have the .* at the beginning and the end.

Sorry, I’m still getting to grips with OpenHAB, it’s a steep learning curve! The double quotes are escaped, and I’ve replaced the : with . so it should be OK, but still nothing in the logs.

Number OilDepth "Depth [%d cm]" (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.depth):.*\"id\" . 137574874,.*]"}

Then simplify and build up.

Just use .* to match everything and verify you are getting the messages.

Then use .*137574874.*

Then keep adding in characters until you get the regex specific enough for your purposes.

1 Like

OK, I have got this to work now. I had two different streams of data on the same MQTT topic - power useage which was logged every 5 seconds and was constantly changing, and the oil tank level which logged every 15 minutes and only changed every one or two days. I didn’t realise that OpenHAB only logs an event if the MQTT data changes, but once I published the data manually using

mosquitto_pub -h localhost -i RTL_433 -t RTL_433/JSON -m '{"time" : "2017-12-29 09:52:32", "model" : "Oil Watchman", "id" : 137574874, "flags" : 128, "maybetemp" : 27, "temperature_C" : 3.433, "binding_countdown" : 0, "depth" : 88}'

the problem was obvious.

I put back my original items and tried again

Number OilDepth "Depth [%d cm]" (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.depth):.*\"id\" \\: 137574874,.*]"}
Number OilTemp "Oil Temperature [%.2f °C]" (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.temperature_C):.*\"id\" \\: 137574874,.*]"}
Number PowerMeter "Power [%d W]" (g1) { mqtt="<[broker:RTL_433/JSON:state:JSONPATH($.power0):.*\"dev_id\" \\: 77,.*]"}

Everything works and the oil level and temperature are logged as I change their values.Thanks for your help @rlkoshak, I now know a lot more about REGEX, MQTT and items files!