JS Script for Watts to Kilowatts

Hi All

Does anyone have a snippet of code to transform my item value of watts, to kilowatts?

I’ve tried:


Number:Energy Solar_GarageAC_Power     "AC Power"                     { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachannelpac" [profile="transform:JS", function="power.js"] }

with script

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

Without success

Thank you!

Hi Kris,

I do my ones in a rule that uses a JS and does various things periodically, might help you.

var watthr7Days      = itemRegistry.getItem('EnphaseDetailed_EnphasewhLastSevenDays').getState();
var watthrLifetime   = itemRegistry.getItem('EnphaseDetailed_EnphasewhLifetime').getState();
var watthrToday      = itemRegistry.getItem('EnphaseDetailed_EnphasewhToday').getState();

var KwhrLifetime = ((watthrLifetime / 1000).toFixed(1));
var KwhrToday = ((watthrToday / 1000).toFixed(3));
var Kwhr7Days = ((watthr7Days / 1000).toFixed(1));

Thanks George, i can’t see in the rule how it populates an item though?

Probably using postupdate.

Power_Watts.postUpdate(RawMqttData_SDRPower.state as Number * 240)
or
LoungeCurtainStatus_LoungeCurtainStatus.postUpdate(OPEN)

I just use something like this,

events.sendCommand("TodaysProductionKwhr", KwhrToday);

As far as I understand the value is in base SI unit watt.
Then you just need to reformat the value like:

Number:Energy Solar_GarageAC_Power     "AC Power [%.1f kWh]"                     { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachannelpac" }

Of course it may depend on your use case which means more details about it would help to understand what you would like to achive.

2 Likes

The trouble with your little transform script profile will likely be that the state update comes from the channel as a Quantity, with units;
1500 W
Data in and out of transforms is always presented as strings
“1500 W”
which is why the little script includes a parseFloat(), to convert to numeric before doing maths.
The non-numeric W will trip that up - javascript doesn’t nativly do units.
Extra processing required.
And when you’re finished, you need to put your (new) unit string back onto the string you are going to return for the Item update.

It’s fiddly and generally unnecessary.
If the binding gives units as measured, and you tell the Item what units you’d prefer, just as @Wolfgang_S describes, it all autoconverts.

1 Like

Thanks, in doing that it says 5000 kW, rather than 5.0kW

Says where?
I think part of the trouble here is using legacy “label[statedescription]” format which gets ignored by MainUI. Use Item state description metadata to set pattern.

or

What actual type of channel is involved here, have you linked a plain number channel (no units) to a Number:Energy Item?

Certainly have. Will try ‘item state description’ meta data

Number:Energy Solar_GarageAC_Power     "AC Power"    { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachannelpac" }

Not clear - you’ve linked a plain number type channel to a Number:Energy type Item?

So in that case the channel pumps out plain 5000 , no units.
openHAB says nope - you’ve specified Number:Energy, you must have units.
I’ll have to assume default units.
I’ll look at your state description as the best guess.
So you can put kW, W, mW, MW in there - the framework will assume it’s 5000 of whatever you told it.

You can’t get auto conversions unless both sides have units.

So back to your JS transform - you can use that to do maths on your plain number channel output, and then you should tell openHAB what units your are working in by appending them to the transform result.

In fact you could just append W to the channel, if that’s what it comes in, and do no maths.
Then choose kW or whatever for display, and auto conversion will work now.

EDIT - my mistake, Energy not Power - kWh etc. units please

The binding is displaying units, as being Watts. With the meta data, it just changes it to kWh, but I need the conversion from watts to kilowatts in maths

The JS doesn’t do it, at least not the snippet I put above

I guess ill resort to a rule!

Please, just examine the channel type to find out what you’re dealing with.

No, your JS will not work as it is; as we’ve already discussed if you are trying to feed output into a Number:Energy you need to add units if you want a predictable result.


Longer story.
So far as I can make out from actually reading the Fronius binding docs
inverterdatachannelpac
is a plain ordinary number type channel.
This will gives us numeric updates with no units.

So far as I can make out from actually reading the Fronius binding docs, it represents the power not the energy. I think you know that too from the Item naming and labelling.
So Number:Energy would never be the appropriate Item type.

Assuming you want instead to redefine your Item to use the more appropriate Number:Power Item type, let’s press on.

So you link the number channel to Number:Power Item, what happens? The channel updates it with a plain number, say 5000, that’s all. But that Item type demands a unit. So it will stick on a default unit, if it can find one in your Item’s state description. It doesn’t matter if you gave it W or kW or horsepower, you will get 5000 of them.

You know the plain number actually means Watts I assume? But you’d prefer a display in kW. That’s fine.
But you can’t do it automatically in this set up because you have chosen mis-matched channel type and Item type. It can’t happen because the system does not know that plain 5000 means 5000W, it’s just a number.
You have to take action to tell it that 5000 means 5000W.

That’s what transforms are for, great.

(function(i) {
return i + " W";
})(input)

Now the 5000 update gets massaged into 5000 W, just what a Number:Power Item likes to see.

But you wanted to see kW? It’s okay, that’s just what.state presentation is for. Put kW in pattern. Auto conversion, sorted.

3 Likes