How to avoid NULL for string after jsonpath filtering

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)

Thank you
M

Try using REGEX filtering before the JSONPATH extraction:

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).+)

Do you have an idea why?

Thanks
M

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.

thanks rossko57,

but I am on a string channel so why is my channel set to “NULL” when the jsonpath does not match?

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.

sorry for not including all details, so the Thing is defined as

UID: mqtt:topic:433MHzreceiver
label: 433MHzreceiver
thingTypeUID: mqtt:topic
configuration:
  availabilityTopic: rtl_433
bridgeUID: mqtt:broker:localMQTTBroker
channels:
  - id: smokedetector
    channelTypeUID: mqtt:string
    label: smokedetector
    description: ""
    configuration:
      stateTopic: rtl_433
      transformationPattern: "JSONPATH:$.[?(@.model=='Smoke-GS558')] "

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.

Thanks Rossko. Yes, I will use Regex. This works fine as said. I was just curious