• # Separate data meter kWh into daytime and nighttime logging and then obtain accumulated value for a chosen date range picker

Hi,
I want to separate the data being logged from an energy meter into daytime and night-time. For example I want the meter to log data for time period 08:00 to 22:59 as daytime, and then from time 23:00 to 07:59 as night-time. I then want to obtain an accumulated energy usage value for a chosen date range picker for the daytime usage and the night-time usage. This way I can distinguish energy usage by daytime and by night-time and then report this.
What is the best way to achieve this? Maybe setup a meta data point, use a script and then a CRON pattern...
Have you any pointers of how to achieve this or maybe this has been done before and there are examples I can follow?
Thanks,
Paul

• Hi Paul,

So, you have a point that is measuring wattage, and you'd like to find the energy used?

If you've got 3 phase amps and volts, this thread may be of interest: https://forum.infiniteautomation.com/topic/4083/calculating-power-use

You can easily separate the data into two meta points with something like,

``````//Meta point 1, daytime
var dt = new Date();
if(date.getHours() >= 8 && date.getHours() < 23)
return p.value;
return UNCHANGED;
``````
``````//Meta point 2, nighttime
var dt = new Date();
if(date.getHours() < 8 && date.getHours() >= 23)
return p.value;
return UNCHANGED;
``````

And then use the `INTEGRAL` rollup to trend the energy consumption. So, if you just needed a daily value, you could have it only execute once on a cron pattern, and do something like `return .past(n, HOUR).integral`

If this is only going in an Excel Report, you could use two separate time periods to divide the original series without needing meta points.

Note also that units come into play here. The integral of watts would be watt-seconds (joules). You can use the data point's units to say you would like the integral as `kW*h` but that conversion would only be done after using the text renderer, as is shown in this post: https://forum.infiniteautomation.com/topic/3989/send-multiple-point-value-in-alarm-text/13

• Hi Phil,
I think I am on the right track as I have produced something similar during testing (if you forgive me for the print statements). See the picture below

I am curious- what is the purpose of the 'return UNCHANGED;' ?

If I need to return the daily value do I just use CRON '0 0 23 * * ?' and use 'return p48.past(15, HOURS).integral;' instead of 'return UNCHANGED;'?

Also the data being collected is already a kWh value, therefore I guess I just need to change the rollup to Integral as suggested.

Further question-
I may need to produce a daily value using something like CRON '0 30 23 * * ?' to run daily at 23:30. My reason to run at 23:30 is that the data been collected may get delayed through sigfox transmission and processing so in order to retrieve last reading I may need the delay (which is unpredictable). This would raise the question that need to change the >=23 to something like >23:15. This is another complication to resolve. Maybe a trigger event might work here. Any suggestions?

Paul

• Also the data being collected is already a kWh value, therefore I guess I just need to change the rollup to Integral as suggested.

Ah, that makes it much easier. Not the integral, you're probably looking for the last value - the start value, like,

``````// on cron 0 30 23 * * ?
var stats = p.prev(15, HOUR);
if( stats.lastValue !== null ) //would be null if no values in the period
return stats.lastValue - stats.startValue;
return UNCHANGED;
``````

Using prev() instead of past() will align to the period boundary and allow you to have the delay you seek. We can't use the integral because we're looking for the different in the value in time, not the integral of that value over time.

what is the purpose of the 'return UNCHANGED;' ?

That was for separating the raw data into two data points. For a meta point, it's context would trigger execution and give it the opportunity to produce a value. If it should not produce a value, though, it needs to `return UNCHANGED;` to tell Mango that. One can also `return UNCHANGED;` from an email handler script to cancel the email (`return CANCEL;` also works for email handler scripts).

• Hi Phil,

I seem to get error message 'Script error: TypeError: date.getHour is not a function in <eval>'.
Similarly I get this error message 'Script error: ReferenceError: "HOURS" is not defined in <eval> at line number 7 in <eval> at line number 7'

Is there an internal variable/function I need to declare in the script?

Thanks,
Paul

• My mistake, try `HOUR` and `.getHours()` . I will edit my original post

Posts 6Views 738