MQTT JSONPATH Transform

Hi all,

I’ve many MQTT things created using text files and majority using some sort of JSONPATH transformation. Now, I was looking to integrate OctoPrint with OH (running OH 4.1.2 release build) and find that I’m getting a NULL value for one transformation which is very strange indeed.

.thing setup:

    Thing topic octoPrint "OctoPrint" {
    Channels:
        Type string : printerConnected "Printer Connected" [ stateTopic="octoPrint/mqtt" ]
        Type string : printerState "Printer State" [ stateTopic="octoPrint/event/PrinterStateChanged", transformationPattern="JSONPATH:$.state_string" ]
        Type string : printerZChange "Printer Z Change" [ stateTopic="octoPrint/event/ZChange", transformationPattern="JSONPATH:$.new" ]
    }

Both printerConnected and printerZChange work as expected.

Please see JSON for printerZChange:

ZChange = {"new": 0.12, "old": 0.3, "_event": "ZChange", "_timestamp": 1717694706}

Now, printerState does not work, JSON (returning NULL):

PrinterStateChanged = {"state_id": "PRINTING", "state_string": "Printing", "_event": "PrinterStateChanged", "_timestamp": 1717694480}

Anything that I’m doing wrong? I’ve tried both state_id and state_string…

Look forward to hearing from you.

Many thanks in advance! :slight_smile:

First, let’s see if it’s getting the full json data.

Try

Thing topic octoPrint "OctoPrint" {
    Channels:
        Type string : printerConnected "Printer Connected" [ stateTopic="octoPrint/mqtt" ]
        Type string : printerState "Printer State" [ stateTopic="octoPrint/event/PrinterStateChanged", transformationPattern="JSONPATH:$.state_string" ]
        Type string : printerZChange "Printer Z Change" [ stateTopic="octoPrint/event/ZChange", transformationPattern="JSONPATH:$.new" ]
        Type string : printerStateFull [ stateTopic="octoPrint/event/PrinterStateChanged" ]
    }

What do you get for printerStateFull ?

@JimT thanks for the above.

Based on the above printerStateFull thing, I’m getting the following:

Many thanks,

Is the jsonpath transformation addon installed?

It is indeed, other jsonpath transformations are working as expected…

image

Theoretically, it should work fine. Here’s a sample jruby code I used to test jsonpath transform:

json = '{"state_id": "PRINTING", "state_string": "Printing", "_event": "PrinterStateChanged", "_timestamp": 1717694480}'
logger.warn transform(:jsonpath, "$.state_string", json)

And it logged Printing as expected.

There must be something funny going on, perhaps a rule / profile is messing up with the state?

Try adding another dummy item, and link it to the channel, and see if it is also null. You’ll have to wait for a new mqtt message to arrive of course.