javascript for "this month"
-
I have the need to follow the MAX of a point for each month. I need it to keep logging this peak data every 15 minutes, but as the peak changes throughout the month it will keep using the highest as its logging point. The problem is, I don't know how to write the date script to look at the current month.
Any ideas out there?
-
You could probably do it most efficiently by some kind of checking if it were the first value in a month. Something like this, on a 15 minute cron:
var dt = new Date(); var bt = new Date(my.time); if(bt.getMonth() != dt.getMonth()) return peak.value; //new month, so whatever is current peak is current max peak else if( peak.value > my.value ) return peak.value; else return my.value
It may have issues if your system is offline (it won't consider older peak data in the month if it didn't run for the first few days, for some reason). But you can do it the brute force way like this:
var dt = new Date(); dt.setDate(1); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0); peakValues = peak.getValuesSince( dt.getTime() ); var max = -100; for(var k in peakValues) { var pvt = peakValues[k]; if(pvt.value > max) max = pvt.value; } return max;
Of course, if it's that straightforward, max is included in the statistics function, so you could do:
var dt = new Date(); dt.setDate(1); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0); var stats = peak.past(SECOND, (new Date().getTime() - dt.getTime())/1000 ); return stats.maximumValue;
-
Thanks Phil. Can I use this in the Mango Metapoints?
-
Yeah, those are provided with Meta points in mind, where "peak" is the name of the context point providing the peak information, and "my" is the self-reference for the meta point.
-
Awesome that worked great!!!
-
Is there a variation on this equation to figure out a delta for this same "month" period? Like this?
var dt = new Date();
dt.setDate(0); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0);
var delta = delta.past(SECOND, (new Date().getTime() - dt.getTime())/1000 );
return stats.deltaValue;*SUBSTITUTE delta WITH THE POINT NAME VAR
-
Hi Chris,
Yes! Analog statistics objects do have a 'delta' member variable. You can access it at stats.delta
I encourage you to use the print(stats) function to get the object printed below the script entry box.
Also, in answering this question I became suspicious there may be some continuous time type shenanigans or an error in the stats.delta value, so, if you think of delta values as being the difference between the last and first values, I would use
if( stats.firstValue !== null ) //Check that there is data in the range return stats.lastValue - stats.firstValue; return 0;
-
@phildunlap Hey Phil,
will this reset each beginning of the month. Im trying to establish the peak for each month.
-
Yes, it will be using the server's timezone as the month delineation.
-
ok.
Do you have a good one for kwh delta for each month. I have a totalizer that I need to see how much is using each month.
-
Update event: start of month
Execution delay: 10s (might not matter)var now = new Date(); var lastMonth = new Date( now.getTime() - 24*60*60*1000 ); //Get yesterday, definitely in last month lastMonth.setDate(1); lastMonth.setHours(0); lastMonth.setMinutes(0); lastMonth.setSeconds(0); lastMonth.setMilliseconds(0); var oldValue = acc.pointValueBefore( lastMonth.getTime() ); //Get the value just before last month return acc.value - oldValue.value;
-
So I need a function to keep a running delta during the month. I've tried this, but for some reason it seems the firstDay is actually like 1 day before the last day of the previous month.
What have I missed?var date = new Date(); var firstDay = new Date(date.getFullYear(), date.getMonth(), 1); firstDay.setDate(0); firstDay.setHours(0); firstDay.setMinutes(0); firstDay.setSeconds(0); firstDay.setMilliseconds(0); var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); var oldValue = p892.pointValueAfter( firstDay.getTime() ); return (p892.value - oldValue.value)/1000;
-
Whoops! My mistake. I shouldn't have been using setDate(0); The first day of the month is 1, and it accepts negative numbers, so 0 is a day before 1.