I have an MQTT Broker in use, where various things are pushing messages in the SAME topic (always JSON).
I have multiple different channels which are checking the messages and selecting the ones which are suitable via JSONPATH. Unfortunately string items are updated to NULL when the JSONPATH does not match e.g.
JSONPATH:$.[?(@.model==‘ABC’)].humidity
When the json does not contain ABC then string items will get NULL.
What is the best way to avoid it? ( It would me hard to change them to different topics for other reasons)
Thanks, this works.
However what I do not understand:
When I am looking for my “requiredterm” first in a regex and then in JSONPATH, this works fine.
REGEX:(.requiredterm.)∩JSONPATH:$.[?(@.model==‘requiredterm’)]
However, I would more prefer the other way round to first try to extract the value and filter in case of the JSONPATH returned nothing / NULL.
However, I do not get the REGEX to filter out failing JSONPATH.
JSONPATH:$.[?(@.model==‘requiredterm’)]∩REGEX:(\b(?!NULL\b).+)
JSONPATH will not return the text “NULL” in the case of a “miss”. (so of course it is no use looking for that with a REGEX)
EDIT - well yes it can return “NULL” sometimes, see below.
You can test these things out using a string channel/Item - I think you’ll be surprised to find a JSONPATH “miss” returns the whole input string.
Do you mean, my Item was set to NULL state before I started and then not having any valid update left it continuing as NULL?
Bear in mind we can’t see over your shoulder. Have you looked in your openhab.log for error reports? They sometimes clarify.
As long as my json has an attribute “model”: “Smoke-GS558”, the whole json is transferred to the string ( as designed). If a json is appearing without matching criterion, then the value is set to NULL. No errors or warnings in the openhab log, in the events logs it says
Item changed from {model=Smoke-GS558, id=2134} to NULL
I’ve never seen the MQTT actively set anything to NULL before so I was questioning that.
However, now it reminded me that the JSONPATH transform can return NULL under specific circumstances.
If you’ve not worked with it before, it is important to note that openHAB JSONPATH Transformation Service is not JSONPATH, it has some quirks.
In case of total failure, like all the transformations, it returns the complete input string.
It can only ever return a single string. No arrays, etc.
It is possible to select multiple elements, where JSONPATH would normally return a List of results. But this will take the form of a single string still, with concatenated results. If I remember, this list-to-string also removes quotemark characters.
Here I think is also where an ‘empty list’ returns string “NULL”, an unusual thing for a transform.
Demo -
Based on details so far, using JSONPATH to select messages by ID text is just the wrong approach, use the REGEX technique suggested earlier. But it all depends on whether you want something to happen or not, when the message part is not present.
Are there any updates on this topic? I have exactly the same issue with the following JSONPath $.packData[?(@.sn == ‘CO03408023457’ && @.socLevel)].socLevel. I simply want to retrieve the value only if it’s present in the JSON, and otherwise not. Getting NULL in the item is never desired. Couldn’t we add an option in the channel link to exclude NULL values?