Format Percent

I’ve got an item that displays a percentage value as “0.91000000 %” which I’d like to be shown as “91 %”. So I went to item’s metadata/state description/pattern and entered “%.2f” (don’t know what that means but found it in the forum). But that doesn’t work. Where’s my mistake?



It means "floating point number (f) rounded to two decimal places (.2).

This is a plain old Number Item. I’m guessing that the Channel linked to this Item is reporting a percentage as a “real” percentage (i.e. a number between 0 and 1). So you will have to transform that 0.90 to 90 by multiplying it by 100 in a JavaScript Transformation.

or maybe use a Number:Dimensionless type Item, which despite the name does have units and supports %, dB, etc.

I might need some more help …

  1. Rich’s idea:
    I installed Add-on Javascript-Transformation

I would imagine some coding like this: newvalue = (oldvalue*100)
It’d be nice if I could attach this directly to the item containing the oldvalue. If so, where would I do that? The other (less comfortable and less clear) possibility would be that I have to create a new item derived from the old one (how would this be done)?
In the description (JavaScript - Transformation Services | openHAB) (which I don’t really understand) a transformation folder is mentioned – where is it / is it necessary for what I want to do?

  1. My ideas (no experince with openhab-coding, just a bit of YAML and node red --)
    I thought maybe I could change it in the label-list-item by multiplying with 100. That didn’t work out, see last line:
component: oh-label-cell
config:
  trendItem: openWBVerbindung_OpenWBLP1SoC
  action: analyzer
  actionAnalyzerItems:
    - openWBVerbindung_openWB_SoC
  item: ((openWBVerbindung_OpenWBLP1SoC)*100)

Another (rather bad) idea: Export the value to nodered, transform it there (which I know how to do) and export it back to openhab. But that would be some kind of overkill to just get a simple transformation …

  1. rossko’s idea:
    Sounds like a nice and simple solution. More or less like one would to it e.g. in Excel. Same content, different format. The item is already a dimensionless number. How/where would I change the format you propose?

Alright, sounds good. So who or what is updating it?

I’m getting the value via MQTT (it’s a State of Charge information from the wallbox/electric car)

Ah, and you specified unit % in the channel?

MQTT binding is broke so far as updates with units go, i.e. it forgets to add the unit.

Best circumvention for now is to revert to a plain Number type Item.

There are two places it can be used in this case. If all you want it to change how it appears on your display, you’ll use the transformation in the State Description Pattern. If you want to change the Item’s state to be 90 instead of 0.9 you’d do the transformation on the Channel config or if that isn’t possible using a Transform Profile to apply the transformation on the value between the Channel and the Item on the Link.

Since you are using the MQTT Binding, add a JS transform to the “Incoming Value Transformations” field of the Channel config.

Usually by defining the second Item, the Proxy Item. Then create a Rule that triggers when the “raw” Item changes that multiplies the new value and updates the Proxy Item.

It’s in the openHAB conf folder. Transformations cannot be created in the UI. To use the JavaScript transformation you must create a new file and put it in the transform folder. Where exactly that transform folder is depends on how you installed openHAB and on what operating system.

On a Linux installation it’s /etc/openhab/transform. The file needs to end in “.js” and the contents will be

(function(i) {
    return parseFloat(i);
})(input)

Then on the Channel in that transformation filed you would have JS:nameoffile.js where “nameoffile” is what ever you named the file.

I guess this is what you’re referring to? Looks interesting, never seen this page before:


I’d imagine that “Incoming value Transformations” is to be addressed. If so, do you know what to enter there?

here’s the code

  - id: OpenWB_LP1_SoC
    channelTypeUID: mqtt:number
    label: OpenWB_LP1_SoC
    description: ""
    configuration:
      stateTopic: openWB/lp/1/%Soc
      max: 100
      min: 0
      transformationPattern: "%"

I just told you in the previous post.

1 Like

Funny, we overlapped by 1 minute …
Thanks for the information on the Transformation folder; I’ll probably need that soon, too.
For now, the appearance is the only thing I need. So if I understood correctly, one line of code entered in “incoming value transformation” should be sufficient. Could you tell me (or hint at) the javascript-code for the incoming value transformation, please?

No, you need that now. You must create the JavaScript function I posted above. You must put it into a file ending in “.js” in the transform folder. Only then can you put the JS:nameoffile.js into the Channel config.

I literally posted it above.

Though I notice I forgot to multiple by 100.

(function(i) {
    return parseFloat(i) * 100;
})(input)
1 Like

Thank you, Rich, it works fine!
Do I understand correctly that I need a separate file for every calculation?

Yes, though you won’t likely need too many calculations like this.

Can you go back a step? What actual value comes in your MQTT message payload?

In retrospect that’s an interesting question …
I’d have expected the answer to be something like 0,900000 as was originally shown in openhab. When I looked in MQTT-fx it’s 90 which surprises me because the OH-values are not written back and I made sure MQTT-fx is connected to openWB, not openhab. So it looks like openWB returns full numbers and OH formats them.
Just to make sure, I deleted the js-code in OH (incoming transformation) and still it’s 90 (even after a refresh) in OH; as opposed to yesterday.
Right now I have no explanation for that. I’ll wait until afternoon, by then the SoC of the car should’ve changed and see what happens. Maybe there needs to be a change of values first

The next step would be to look and see what the Item state really is, don’t just trust the UI presentation. A look at the JSON via API would tell you about state, and confirm type, formatting options etc.

The result is ok, everything is working fine. However, I’m always interested to learn something new (if not too time-consuming): So how would I “look at the JSON via API”?

There’s called API explorer, you’ll be looking for “get Item details” or suchlike.