MQTT 2.4 convert string to number in JSONPATH?

I’ve interfaced my boiler to my OH 2.4 system via MQTT. A typical MQTT payload looks like this:

{"wWSelTemp":"60","selFlowTemp":"5.0","outdoorTemp":"?","wWActivated":"on","wWComfort":"Comfort","wWCurTmp":"46.0","wWCurFlow":"0.0","wWHeat":"on","curFlowTemp":"54.2","retTemp":"51.5","burnGas":"off","heatPmp":"off","fanWork":"off","ignWork":"off","wWCirc":"off","selBurnPow":"0","curBurnPow":"0","sysPress":"1.2","boilTemp":"56.7","pumpMod":"0","ServiceCode":"0H"}

I’ve created a generic MQTT thing and added some channels. The channel for the current flow temperature looks like this:

This channel is currently linked to a string item. If I click on the string item and change the type from String to Number then the item in my sitemap displays a dash “-”. I presume this is because the string from the channel can’t be assigned to a item with type number. Is there a way to cast/convert the string from the MQTT channel to a number in the JSONPATH transformation before it’s passed to the linked item?

I could do a conversion from string item to number item via a rule but it would be nice if I could do it in the channel definition.

You have to change the channel type to number, too.
As you can’t change the channel type, you will have to add the channel as a number channel.

Hi Steve,
I can be wrong, but You can convert JSONPATH to a number only by the rule. I did not find any other solution.
Regards
Rafał

It should work, unless the quotes are included in the return from JSONPATH. But even if it is, in MQTT 2.5 M1 or later, you can chain transformations and add a REGEX:"(.*)" after the JSONPATH transform with the intersection operator symnbol separating them (you can copy it from the text under the incoming transformation field).

Thanks, Udo. That solved the issue for me. :slight_smile:

It should work, unless the quotes are included in the return from JSONPATH. But even if it is, in MQTT 2.5 M1 or later, you can chain transformations and add a REGEX:"(.*)" after the JSONPATH transform with the intersection operator symnbol separating them (you can copy it from the text under the incoming transformation field).

This is very interesting. To solve my current issue I followed Udo’s advice above and deleted the string channel, replaced it with a number channel and then linked a new number item to it. This method resulted in inbound MQTT numbers that were passed in quotes (see the example MQTT in the original post) being cast to numbers. This in turn allowed the values to be persisted and graphed in InfluxDB and Grafana respectively. Nice.

Having read the MQTT documentation I saw that messages that use the Homie 3 spec will cause MQTT things and channels to be autocreated. If I were to amend the code on the boiler interface to use the Homie 3.0 specification it may possibly be useful to convert/cast value types using the JSONPATH transformation. Are there any examples anywhere of a regex doing this sort of thing in an MQTT channel? I’ve searched but couldn’t find any.

REGEX pretty much only extracts parts of a String. It doesn’t transform or cast the values. Apparently Number Items are able to handle parsing Strings with quotes around them. But it is the fact that you have a Number Channel assigned to a Number Item that results in a Number that was parsed from the result of the JSONPATH.

If the Number Item couldn’t have handled the quotes, you could have used the REGEX to remove the quotes from the result of the JSONPATH, so that it could have been parsed into a number. But that isn’t transforming the data, it is just preparing the String that gets passed to the Number Item so it can be parsed.