If I was to try and filter data, for example I am experimenting with a oil level monitor however my neighbour has one as well so i’m getting his data as well.
I can’t see clear documentation on regex on the new 2.x mqtt.
I can get data through if I have JSONPATH:$.depth but would ideally like to filter it to my own based on id
There is one 1 record in the Json so you won’t be able to “filter” it.
What you need to do is use a String channel to get the RAW Json and link it to an item
Then use this item in a rule an rule the transform() action to check for the id and then update your value.
Does that make sense?
item:
String RawJson { channel="mqtt......." }
Rule
rule "Update oil level"
when
Item RawJson changed
then
val String id = transform("JSONPATH", "$.id", RawJson.state.toString)
if (id == "141932971") {
OilLevelDepthItem.postUpdate(transform("JSONPATH", "$.depth", RawJson.state.toString))
}
end
I have been experimenting but no joy, I have modified your rule to cover my existing rule so it does it all in one go but no errors are displayed, nothing in events or openhab.log when data is sent so not sure what i’m missing, there’s not much to go on
val float height = 89 // height of the tank
rule "Update oil level"
when
Item RawJson changed
then
val String id = transform("JSONPATH", "$.id", RawJson.state.toString)
if (id == "141932971") {
val float depth = Float::parseFloat(WatchmanDepth.state.toString) as Number
val float left = height - depth
val float per = (left / height) * 100
WatchmanPercentage.sendCommand(per) // update the item that holds the percentage
}
end
Can you show the thing and channel, please?
Also try to avoid the use of primitives in the DSL, use Number instead of Float
val Number height = 89 // height of the tank
rule "Update oil level"
when
Item RawJson changed
then
val String id = transform("JSONPATH", "$.id", RawJson.state.toString)
if (id == "141932971") {
val Number depth = Float::parseFloat(WatchmanDepth.state.toString) as Number
val Number left = height - depth
val Number per = (left / height) * 100
WatchmanPercentage.postUpdate(per) // update the item that holds the percentage
}
end
I have to wait 20mins between transmissions from the watchman, with your example i needed ‘Item’ at the start and a close brackets on the transform line