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"
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:
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)
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.
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