Hi,
This is my very first post in this forum and I’m kindly asking for help.
I’m running OpenHAB 2.5.1-2 on a Raspi 3.
I have a Shelly Dimmer, that I hacked to switch 3 times more current and therefore 3 times more power.
The problem is now, that I have to multiply the measured power value by 3 and display it on Basic UI / sitemap but I don’t get it to work.
I’m using a Javascript transformation file ShellyDimmer1_transform.js for the calculation:
Text item=ShellyDimmer1MeterCurrentWatts label="current power"
Sometimes it displays the correctly formatted number, sometimes it has many more decimals.
It never displays a unit.
Now I think that the
Number:Power ShellyDimmer1MeterCurrentWatts
is some sort of complex data type consisting of a number and a unit
and I’m just multiplying the whole thing by 3 and for some reason it even spits out the
correct number sometimes but never a unit.
Can anyone show me how the multiplication is done correctly so that the unit is kept and displayable?
If you want to format decimal places, do it in your javascript transform (remember that the transformation service will return a string here)
I think the unit is dropped/ignored by the transformation service when it initially converts the Number:Power state to string for the javascript.
Again you cann add it in your javascript.
Hey Guys,
I got the problem solved.
It is correct, that JavaScript Transform takes the input, makes it into a string and drops the unit along the way.
What’s left is the value with 3 decimal places and no unit, formatted as a string.
Knowing that, I used the following code to transform it correctly:
// This converts the hacked Shelly Dimmer's measured power to 3 times its original value.
(function(i) {
var mem = parseFloat(i)*3.0; // Takes the input string i, makes it a floating point number and multiplies it by 3.
return mem.toFixed(2) + " W"; // Returns a string composed of the floating point number with 2 decimal places plus the unit W.
}) (input)
I then display it in my items definition as a string:
Number:Power ShellyDimmer1MeterCurrentWatts "Current Power [JS(ShellyDimmer1_transform.js):%s]" {channel="shelly:shellydimmer:f34484:meter#currentWatts"}