Node-red-contrib-watt2kwh

I've just published v0.1.0 of node-red-contrib-watt2kwh which is specifically written to convert power (watts) to energy (kWh).
It carries out the conversion by measuring the interval between consecutive inputs, and multiplying it by the power value - interval x power = energy.
The output is then scaled to deliver units of Joules, Watt hour, Kilowatt hour or Megawatt hour which is selectable in the node's configuration.

Hope it's of use!

3 Likes

I test it immediately for my photovoltaic production :stuck_out_tongue:

1 Like

Installed and worked. I use it for my PV production and house consumption.
The node replaced a rather cumbersome flow with global variables.
Thanks.

1 Like

Hi Paul...

Just a small coding trick to compact those big switch-statements.

formats = {"j":1e-3, "kj": 1, "wh": 3.6}
output = formats["jk"]

So.. instead of the switch statement, use this instead?

const formats = {"j":1e-3, "kj": 1, "wh": 3.6, "kwh": 3600, "mwh": (1e3*3600)};
this.output = formats[config.format] || 0;

It seems to work ok, is there a trade off?

There are many ways of doing things - no one is more correct than others.

The advantage with the associative array is that if you do lookups with many key-value pairs then your code will be much more compact this way.

I don't think there are any trade offs.

In that case I'll update the node shortly
Thanks @x375993 Alex.

Btw... you can also do it with your timeunits

const unitfact={"secs": 1, "mins": 60, "hours": 3600}
this.maximum *= unitfact[config.maximumunit]

1 Like

Yes, already updated it, thanks