Getting total kWh from W readings

I know how to calculate the kWh delta (the energy spent during the period) using Meta Data point. What I am not sure about is how to calculate the total kWh usage, I mean accumulating reading through time periods. What I am not sure about is how to get the last current kWh state and add the kWdelta to it and save it as a new value.
Let's assume I have values in the W.
// getting the current and the previous readings (W + time) var lastW = p.lastValue(1); var nowW = p.lastValue(0); // getting epoche period in milliseconds var period = nowW.time  lastW.time; //getting period in seconds periodS = period/1000 // getting the W readings var Wnow = nowW.value; var Wlast = lastW.value; // calculating the W delta Wdelta = Wnow  Wlast; // getting periods W seconds Wsdelta = Wdelta*periodS // getting Whdelta Whdelta = (1/3600)*Wsdelta //getting kWhdelta kWhdelta=(1/1000)*Whdelta // so how I can get the last point kWh value //assuming that the metadata point variable name is "my", should I return something like my.value + kWhdelta for the new value? return my.value+kWhdelta;

@ninokurtalj try this and see how you get on
https://forum.mangoos.com/post/14208Also search the forums for point value roll up in scripting data sources you can use the query to calculate the delta for you, then just add it to the datapoint's current total
Fox

I calculate this using rollups You can use the integral rollup type on watts over the time you want. Mango will give you this in watt seconds. Then you can divide that by 3,600 to get watt hours, then scale it as needed.
Better is to read the kwh raw value out of your metering device, though, if that works for you.
Here's an example:
// This runs every hour on the hour to calculate last hours' energy var energy = 0; // This sets up and returns 1 hour integral on power var values = []; var endDate = CONTEXT.getTimestamp(); var startDate = endDate  CONTEXT.millisInPast(HOURS, 1); // Last 1 HOUR var pointToQuery = [ POWER.getDataPointWrapper().getId() ]; PointValueQuery.rollupQuery( pointToQuery, startDate, endDate, function( value, index ) { values.push(value); }, com.serotonin.m2m2.Common.Rollups.INTEGRAL, 1, com.serotonin.m2m2.Common.TimePeriods.HOURS); energy = values[0] / 3600000; my = energy; return my;
I'm not sure if this is the right way to do this, but it works for me.

@Turbo exactly what I meant, although I'd use delta, thanks man!

return my.value+kWhdelta;
my.value is the current value of the metapoint when the script runs. This looks correct to me.

@MattFox It's not delta. It's integral. Delta between two powers doesn't given you kwh, unless mango use some different understanding of "delta".
If you do delta on power over 1 hour (for example), and you have 1000W at time = 0 and 1000 W at time = 1 hour, your delta is 0 (since 10001000 = 0), but your integral should be 1000w * 1hr = 1kwh.
If you do delta on power that's what you get: A delta on power (power at start of time minus power and end of time). You can use that to estimate energy, if the time period is short enough, but energy is the integral of power over time, so you need to use integral (if you're going from watts > kwh).

@Turbo no I meant delta on a raw kwh ouput from the unit as opposed to the integral with respect to time
Fox

@MattFox Fair enough @ninokurtalj said "Let's assume I have values in the W.".. Which I understood to mean he was using "watts" and it looks like he was trying to integrate watts over seconds in the metapoint he posted, using the nowlast values as a sort of way to integrate watts into kwh.
Also; @ninokurtalj if you are integrating watts over time to get kwh, realize that you're accumulating errors as you go. It's much much better to use the raw kwh (or wh, or energy point) out of your metering device. This is because even integrated watts to kwh in mango, you're still limited by polling rate of your device. I run 1000's of points of energy and power in my system, and we can see the errors building up over time: Particularly when the power signal changes rapidly.
The internal power meter kwh points (the "kwh" register you read out of your electrical meter) integrates at "electrical speed", so they catch those wiggles in real time: They basically sum every update of some highspeed V*A calculation.
If you're reading watts and integrating that into wh youself, you can only get those values every time you ask the meter for it, so you'll miss the little wiggles that happen in between polls. If you're polling every second, this will probably be a fairly small error, but it grows over time, so if you go true up your integrated watt > kwh point by comparing it to like the power company's billing meter, you'll always be off by a few percent. The slower the watt polling interval, the larger that energy error is at the end of the month.
I run into this (and deal with this) all the time in the business I'm in, since some people insist on taking (for example) 15minute average kilowatt, summing them up, calling it kwh, and then complaining to me that my energy points are wrong They aren't.
They're just integrating a lowfrequency discrete time signal and comparing it with an integral at real time as the signal changes: They will not be the same. That error can cause lots of confusion, if you're (for example) trying to calculate your AC line losses over time The error in going from 15minute average power into kwh is generally larger than the actual losses in the cable you're trying to measure.
We do use "integrated watts" from Mango for certain applications, but it's in places where that error is easy to deal with (such as by hourly comparisons, where you don't have time for that error to accumulate over months before comparing).