State Formatter for JSON String

Hi,

im quite new to open Hab and now i have a simple problem… I use V3.0.1 on a raspberry Pi4.

I created a channel which receives a JSON String. Then i added a couple of Items to this channel with a JSONPath Expression to filter the values i want. All Items are of type number. Everything works fine and i get the values i want to display, but its not possible to use the state formatter in the adcanced setting to add a unit. If i use anything else than the standard %s expression, for example %.3f or %.1f it doesnt work anymore… How do i use this State formatter ?

I used the GUI to add the channel and the Iems.

Do i have to create a own channel for each value i want to display and add the JSON Patch expression there ? Because if i use the Unit of measurement there i can display the units…
best regards

Is this something to do with some particular binding? Are you applying a transformation to the channel maybe, or in a profile?

Hi,

its a standard MQTT Binding and i receive Data from a microcontroller. The JSON String looks like this:

{"Energy":{"VoltageID1":226.5,"CurrentID1":0.19,"PowerID1":41.11,"TotalEnergyID1":0.063,
      "VoltageID2":0.0,"CurrentID2":0.00,"PowerID2":0.00,"TotalEnergyID2":0.000}}

the channel is configured to the state topic, and for the linked Items i use a JSON expression

I want to have unit displayed to the extracted value…

I think the state formatter here formats the value before it’s passed to the profile. You need to specify this as the state description in the item configuration.

1 Like

Thank you thats it! But even in the state description its not possible to use %0.3f
only %s is allowed which i dont understand a the Items type is defined as number…

but thats not a big problem as i can change the number of digits in the jsonstring

Edit ok forget it its .3f of course its working :slight_smile:

What happens when you specify the state description? Do you get any errors in the log or the UI?

its working as expected thank you :slight_smile: I just made a expression error and typed %0.3f instead of %.3f

Is there some reason you are not using the units parameter provided by MQTT binding on number type channels? You would then link to a Number:ElectricPotential (or whatever appropriate) Item type.

yes, because then i have to create an own channel for every value in the JSON string. I thought it would be a better workflow to add one channel, get the whole JSON String and then do the transformation for every Item indidual. My Json string has parameters for voltage, current, power and energy.

That is the conventional way to do it. This becomes more meaningful when you have outgoing commands, so you’ll probably end up working this way for at least some devices.
I couldn’t say if transforming in channel is more efficient than a profile, but I suspect so.

Ok good to know. For this special purpose all data is read only, but you’re right, i guess it would be unhandy to change a parameter in the jason string with only one channel containing the whole string…