javascript for "this month"
-
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.