[SOLVED] Problem while converting kBit/s to MBit/s with JSTransform

  • Platform information:
    • Hardware: Raspberry Pi 3b+
    • openHAB version: 2.3

Hello community,

since yesterday I am fiddling around with a problem which I thought would be easy to solve, but I got stuck.

I use Fritzbox-Binding to read out current Up- and Downstream. FB-Binding delivers values in kBit/s, but I wanted to show it in MBit/s.

So here is what I did:

I installed Transform service and made a simple JS:

//getmega.js
//convert Kilobit to Megabit
(function(i){ 

	var result = (parseInt(i) / 1000).toFixed(3);


	return result;
})(input)

In the item, I call the script to transform it (second item without transformation to compare):

Number  fboxDslDownstreamCurrRate   "DSL Downstream Current [JS(getmega.js):%s MBit]" {fritzboxtr064="dslDownstreamCurrRate"}

Number  fboxDslUpstreamCurrRate     "DSL Upstream Current [%s MBit]" {fritzboxtr064="dslUpstreamCurrRate"}

In the sitemap:

Text item=fboxDslDownstreamCurrRate label="DSL Downstream" icon="fritzbox2"
Text item=fboxDslUpstreamCurrRate label="DSL Upstream" icon="fritzbox2"

Now what happes is:

As you can see, the value of MBit/s for the Downstream is calculated correctly, but the “Mbit” is not shown.

In Upstream-Icon, I use the “raw” value (as delivered by Fritzbox in kBit), and it shows the “MBit” unit behind the value (which is the wrong unit, but it is shown). I also tried to change format to .3f

Number  		fboxDslDownstreamCurrRate   "DSL Downstream Current [JS(getmega.js):%.3f MBit]" {fritzboxtr064="dslDownstreamCurrRate"}

but then it also does not show “MBit” behind the value. What is my mistake?

I was able to figure it out by myself. Somehow it seems that the transform “removes” the unit after conversion. So what I did was following:

getmega.js

//getmega.js
//convert Kilobit to Megabit
(function(i){ 

	var result = ((parseInt(i) / 1000).toFixed(3) + " MBit");


	return result;
})(input)

As you can see, I already add the unit “MBit” in the transform.

Item:

Number  fboxDslDownstreamCurrRate   "DSL Downstream Current [JS(getmega.js):%.1f]" {fritzboxtr064="dslDownstreamCurrRate"}

Number fboxDslUpstreamCurrRate     "DSL Upstream Current [JS(getmega.js):%.1f]" {fritzboxtr064="dslUpstreamCurrRate"}

Now Bandwith is shown in MBit and unit as desired:

3 Likes

PS you need to divide by 1024 not 1000

Yeah you are right, I first wanted to make conversion “simple” to check result :wink:

Very useful - thank you !

Question: I would like to format the result to local number-format (e.g. “1.234,56 MBit”)
I tried different versions of the corresponding JS-functions and checked it on


but in openHAB nothing worked.
Any idea?

Solutions I tried (division by 1024 omited):

(function(i){ 
    var r = parseInt(i).toLocaleString('de-DE');
    return r;
})(input)
(function(i){ 
	const toMBit = new Intl.NumberFormat('de-DE');
    var r = toMBit.format(parseInt(i));
    return r;
})(input)

Test:

Hi, i’m little confused. with OH3.1 - is it still required to use an external script (JS) to transform kbit/s to mbit/s ? Having TR-064 binding and would love to show max DSL rate in mbit/s but if possible not need another script transformation.

Thanks, Norbert

Depends on your channel type and Item type, the OP used a plain Number type.

@rossko57
thanks…not sure what OP means but the question is related to these channels:

OP just means original poster, commonly used where people tack their queries onto others posts.

If you link your number:datatransferrate channel to an Item of Number:DataTransferRate type, as the UI should suggest, then you won’t need any external conversions.

OK thanks. but how to i tell the item that it should present in mbit/s or kbit/s? does it sort it out by looking at the label content? if i have used mbit/s or kbit/s in the labeling? Thanks

Okay, presentation is controlled by Item metadata, “pattern”.
Old style [state presentation] in file defined Item labels is old hat in OH3.