• # reseting a live count at a certain time

Hello all,
I'm trying to have a live updating total of a difference between 2 points. The tough part is resetting the total at a certain time, say midnight.
So, for example, the count will increase all day and at midnight, reset to 0 so it can count up again for the next day.
I know there's ways to sum past values and display it after the fact, but I need a live count.
This code doesn't work but the logic is what I want:

if (delta.HOUR == 6) {delta = 0;
}
else{ delta += p3118.value + p3117.value;
}
return delta;

Thank you,
Stephen

• Hi Stephen,

The pseudocode you supplied and your description do not seem to match, to me. Specifically, it sounds like you wanted "delta += (p3118.value - p3117.value);" from your description, but the code sums them.

Regardless, I would potentially write this as....

``````var now = new Date();
var minutesToday = now.getMinutes() + now.getHours()*60;
return p3118.past(MINUTE, minutesToday).sum - p3117.past(MINUTE, minutesToday).sum;
``````

It should work since addition is commutative, we do not need to necessarily sum it up as we go. I am assuming there's an equal number of data points in each, but I feel as though you were as well, so they probably come from the same data source. Also, because this is only one day, we can let it slide a little on the efficiency by querying the whole range (but if you have very dense data (subsecond or second) this could be a consideration). I would run it on a cron like....

0 * * * * ?         <-- Every minute
0 0/5 * * * ?    <--Every five minutes

Do you think this will work, or should we get a more efficient strategy and sum as we go?

• One of the data points is always negative, so the + is what I wanted.

I don't see where the restart of the day comes in, but I think this might work. I'll have to check the data for a while first.
Thank you,
Stephen

• Ah, gotcha. You'll probably want to switch the sign in my return statement, then.

The reset is that now.getMinutes() and now.getHours() will return 0 at midnight, and any past(PERIOD, 0) call will have an empty data set (I should confirm this, it could have a single data point if there's a value at that millisecond), and the sum of an empty data set is 0, so it'll be 0 - 0 = 0

Posts 4Views 1711