Simple JSON Problem

I am unable to figure out where my code is wrong.
The json:
{“output”:100,“actual”:195,“enabled”:true,“target”:225}

The item:
String bbqTempStr “Temperature [JSONPATH($.actual):%s °F]” {channel=“mqtt:topic:generic:bbqJson”}

The resulting log entry:
2020-07-17 11:02:39.428 [vent.ItemStateChangedEvent] - bbqTempStr changed from {“output”:100,“actual”:197,“enabled”:true,“target”:225} to {“output”:100,“actual”:195,“enabled”:true,“target”:225}

bbqTempStr is returning the entire JSON, meaning it failed to parse properly. I am at a loss as to why.

You’d have to tell us a bit about how your channel is configured.
Nothing shown here suggests that you should expect anything other than the whole JSON payload in your Item.

Note that applying a transform in the [state presentation] part of a label is only about, well, presentation in a UI. It never affects the actual Item state.

I am following this documentation:

my MQTT thing channel
Type string : bbqJson “BBQ pit temperature” [ stateTopic=“BBQ/from”]

Okay, I see that the example transforms in the rule. Is there a way to do that in the item? Okay, profiles. I was blinded to the documentation. Looking at that now.

I’m sure you are. That doesn’t conflict with what I said.
You might also look at MQTT binding documentation.

There are many different places where you can apply a transformation in openHAB, with of course different effects.

If you want to transform an incoming MQTT payload before it gets passed to Item state, you do that in the MQTT channel.

Transform profile is a bit limited at the moment, only working with String type Items. If you extract your numeric in the channel instead, you could use a Number Item.

I suspect you might also need a divide-by-ten or something on your raw numeric data? MQTT binding supports multiple chained transforms for that.
Or you can write a javascript transform that does JSON and maths in one script.

No, everything you said was correct and matches the docs. I just didn’t read to the end. Here’s what is working for me.
String bbqTempStr2 { channel=“mqtt:topic:generic:bbqJson”[profile=“transform:JSONPATH”, function="$.actual"]}

Yes, there are a lot of ways to skin an OpenHab cat.

I had an example working in the rule, but it threw errors on null data. I was hoping this would be a more suitable location. I had not considered doing this in the MQTT channel. I’ll look at that as well.

The MQTT channel transformation is definitely the way to go:
Type number : bbqTempNum “BBQ pit temperature” [ stateTopic=“BBQ/from”, transformationPattern=“JSONPATH:$.actual” ]

1 Like