[solved] OH3 Jsonpath transforms on MQTT Json payloads

Hi All,

After upgrading from OH2 to OH3 I noticed some of my MQTT items were not able to transform:

2021-11-02 16:10:01.356 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '0.14' on item 'mqtt_topic_radiation_dose' with pattern 'JSONPATH($.dose):%f.5 mSv/h': Cannot format state '0.14' to format '%f.5 mSv/h'
2021-11-02 16:10:01.386 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '21.06' on item 'mqtt_topic_radiation_cpmTotal' with pattern 'JSONPATH($.cpmTotal):%d CPM': Cannot format state '21.06' to format '%d CPM'
2021-11-02 16:10:01.403 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '18' on item 'mqtt_topic_radiation_cpmMinuteAverage' with pattern 'JSONPATH($.cpmMinuteAverage):%d CPM': Cannot format state '18' to format '%d CPM'

Even after Installed the MQTT binding and the JSONPath transform, this error remained.
I have read the documentation, but I cannot discern where I went wrong. Hope yall can help me see the error through the chars :stuck_out_tongue:

rads.things

Bridge mqtt:broker:3525be49 [ host="127.0.0.1", secure=false ]
{
   Thing mqtt:topic:radiation
   {
   Channels:
     Type number : cpmTotal "cpmTotal: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.CPMTotal" ]
     Type number : cpmMinuteAverage "cpmMinuteAverage: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.cpmMinuteAverage" ]
     Type number : dose "RadDosage: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.dose" ]
   }
}

rads.items

Number mqtt_topic_radiation_cpmTotal			"cpmTotal: [JSONPATH($.cpmTotal):%d CPM]"		   	 	<radiation_round>		(gRadiationCPM) 		 {channel="mqtt:topic:radiation:cpmTotal"}
Number mqtt_topic_radiation_cpmMinuteAverage	"cpmMinuteAverage: [JSONPATH($.cpmMinuteAverage):%d CPM]"		 	<radiation_round>		(gRadiationCPM) 		 {channel="mqtt:topic:radiation:cpmMinuteAverage"}
Number mqtt_topic_radiation_dose				"dose: [JSONPATH($.dose):%f.5 mSv/h]"		   	 	<radiation_triangular>		 (gRadiationDose)		 	{channel="mqtt:topic:radiation:dose"}

this is how the payload is structured (copied from the ESP’s serial console)

MQTT Connected
Connecting to  as ESP-5c:cf:7f:5a:3e:b3-48
Connected to MQTT broker
topic is: /radiation
Publish ok
{"CPMTotal":13.71,"cpmMinuteAverage":10.00,"dose":0.09}
{"CPMTotal":13.71,"cpmMinuteAverage":10.00,"dose":0.09}
Publish OK!

it’s probably something obvious but dyslexia/dyscalculia is making those minor differences hard to discern for me.

The complaint is that the string output of the JSONPATH, “21.06”, cannot be formatted using %d formatter. That’s because it’s a string, not numeric. The raw output of transformations is always a string.

So

Number mqtt_topic_radiation_cpmTotal			"cpmTotal: [JSONPATH($.cpmTotal):%s CPM]"

would make the complaint go away by using %s string formatter.

But look again, this a Number type Item. What are you expecting to find in this Item’s state that needs a JSONPATH transformation before display? It can only ever be a number.

Number mqtt_topic_radiation_cpmTotal			"cpmTotal: [%d CPM]"

will do.

This is very helpful! it solves my error. But then the values didn’t update…

So I checked the .thing file and found I had used the OH2 syntax, this is the correct file

Bridge mqtt:broker:3525be49 [ host="127.0.0.1", secure=false ]
{
	Thing topic radiation "Dosimeter" @ "Living Room"
	{
		Channels:
			Type string : cpmTotal "cpmTotal: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.CPMTotal" ]
			Type string : cpmMinuteAverage "cpmMinuteAverage: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.cpmMinuteAverage" ]
			Type string : dose "RadDosage: " [ stateTopic="/radiation", transformationPattern="JSONPATH:$.dose" ]
	}
}

But still no updates. Openhab didn’t complain about validation errors so I checked the logs and saw I had the wrong structure for the topic, so I copy-pasted the structure from the log and came up with this.
this is the .item file with the correct topic

Number mqtt_topic_radiation_cpmTotal		"cpmTotal: [%d CPM]"		   	 	      <radiation_round>          (gRadiationCPM) 		 {channel="mqtt:topic:3525be49:radiation:cpmTotal"}
Number mqtt_topic_radiation_cpmMinuteAverage	"cpmMinuteAverage: [%d CPM]" 	<radiation_round>          (gRadiationCPM) 		 {channel="mqtt:topic:3525be49:radiation:cpmMinuteAverage"}
Number mqtt_topic_radiation_dose		"dose: [%f.5 mSv/h]"          		   	 	<radiation_triangular>		 (gRadiationDose)		 {channel="mqtt:topic:3525be49:radiation:radiation:dose"}

Now it’s working again.

Thanks for helping me out!

Ideally you link channels of one type to Items of the same type. I think you get away with it here.