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.
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,.*]"}
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
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!