Recommended Posts

1) create a new channel to hold the daily totals. Make it device type Test, and select Timing of 0. Lets say you call it dailytotal

2) In the event for your firmware counter channel, which we will say you called Counts, put:

if (formatdatetime("%d",counts.time[0]) != formatdatetime("%d",counts.time[1]))
   private total = sum(counts[systime(),systime() - 86400])
   total.time = systime()
   dailytotal.addvalue(total)
endif

3) Graph dailytotal vs time

Link to comment
Share on other sites

That's just:

sum(counts[floor(systime()/86400)*86400, systime()]

floor(x/y)*y

is the standard algorithm in all programming languages to find the largest integer multiple of y that is less than x. 86400 is the number of seconds in the day, and since systime() returns time since midnight 1970, the above does what you want. Notice how I'm subsetting in time instead of by index (i.e. counts[0])

Link to comment
Share on other sites

What I've sent so far assumes that you are resetting the counter every time you read it. This is not necessarily the best way as it will miss occasional counts, but it is easier to work with to some extent. If you expect a large amount of counts (say 100 per second or more), then you really don't want to reset the counter ever and the logic changes. In this case, you should create a global variable with the counts at the end of the previous day and subtract it from the counts at the end of the day. First, create the global variable in a startup sequence that sets this to 0 and resets the counter (once). Then the event code becomes (assuming that global is lastdaycount:

if (formatdatetime("%d",counts.time[0]) != formatdatetime("%d",counts.time[1]))
   private total = counts[0] - lastdaycount
   total.time = systime()
   dailytotal.addvalue(total)
   lastdaycount = counts[0]
endif

Then to display the accumulated for the day, just do:

counts[0] - lastdaycount

Link to comment
Share on other sites

  • 10 months later...

Hi, I was interested in using this count routine to display my cumulative kWh for today, from midnight. Could you just tell me how to..

"First, create the global variable in a startup sequence that sets this to 0 and resets the counter (once). Then the event code becomes (assuming that global is lastdaycount:"

Thanks

Andy

Link to comment
Share on other sites

It depends. Are you measuring KWh or KW? I guess it comes down to: do you just want a sum of a channel for the day, or is the hardware doing the summing (as it is in the rest of this post)? If you just want a sum of a channel its just:

sum(mychannel[floor(systime()/86400)*86400, systime()])

floor(systime()/86400)*86400 is midnight today. Systime() is just now, so we are just getting all the data points in mychannel from midnight until now and summing it.

The rest of this post is about putting the sum for the day into a separate channel so you can graph daily totals over time.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.