A Continual last 14 day value accumulation - Circular Buffer

Hi all,

I’m wondering if any knows how to send data to a widget (labeled) that is the sum of a value that has been obtain at a particular time of day each day for the last 14 days continually.

This is one approach…

You could also save the values to SPIFFS of LittleFS of you’re using and ESP MCU so that your data is protected against power failure.


Thanks @PeteKnight…I’ll check it out

Ok @PeteKnight… So what you have said in theory in the link is what I need to code…any more hints…pls?

Not really, I think the description in that link covers the theory.
I could share my code, but it wouldn’t make any sense and would probably make the task more complicated for you.


Ok no worries…I’ll slowly get there no doubt…I’m researching circular buffers etc. now.

I wasn’t suggesting g that you use circular buffers, simply a 7 element array (normally referenced as 0-6) to store the data and a variable to act as a pointer and another to act as a running total.
If you’re using the RTC widget then you could use the day of week value as your pointer.

With this type of process it’s sometimes better to test the process out on paper to get the logic correct before you start coding.


Ok no probs at all @PeteKnight, so after a bit of research I settled for the following…Just in case anyone else is curious…

float dailyFigures[7]; //last 7 days total profit/loss
int pos = 0;
void ofSomeKind()
//   ***** Last 7 days accumulative profit/loss value from V36 *****
      float value = (((((ExportEnd - ExportStart) * FeedInTariffPerKw) / 100) - ((((ImportEnd - ImportStart) * PricePerKW) / 100) * UsageDiscount) - (DailySolarFee / 100)) - (DailyConnectionFee / 100));
      dailyFigures[pos++] = value; // place the value in the next slot.
      if (pos == 6){ 
        pos = 0;
        float sumLast7DaysProfitLoss = dailyFigures[0];
          for (int i=0; i<7; i++){
            sumLast7DaysProfitLoss = sumLast7DaysProfitLoss + dailyFigures[i];   


timer.setTimeout(1000, [](){
 timer.setInterval(30000L, getDailyReadings);

I think I’ve finally worked this out so I’m posting my results in case others are interested. Full credit to Roberto Lo Giacco for his CircularBuffer Library which helped me to achieve what I wanted. I’m also storing values on Blynk Server side (instead of EEPROM) in the case of a ESP8266 reboot. I’m using this to keep a running total of daily profit/loss of my solar system over the last 14 days (actually i’m keeping track of the last 7, 14 & 28 days)

Basically the setup is as follows,

(Only the relevant Circular Buffer code and Blynk Server side value storage has been posted)

#include <CircularBuffer.h>

CircularBuffer<float, 14> buffer14;

float ForteenDayTotal;


BLYNK_WRITE(V90) {ForteenDayTotal = param.asFloat();}

BLYNK_WRITE(V91) {                                       //Store Last 14 days values
  for (float i = 0; i < 15; i++) {

void sendsValues(){ //I'm using RTC time widget and some time code to send values at 23:59hrs daily

////****** Last 14 days accumulative profit/loss value *****
    float readingForLast14days = (((((LiveExportEnd - ExportStart) * FeedInTariffPerKw) / 100) - ((((LiveImportEnd - ImportStart) * PricePerKW) / 100) * UsageDiscount) - (DailySolarFee / 100)) - (DailyConnectionFee / 100));
    float sumForLast14days = 0;
       for (float i = 0; i < 15; i++) {
            sumForLast14days = sumForLast14days + buffer14[i] ;
    for (float i = 0; i < 15; i++) {
         Blynk.virtualWrite(V91, buffer14[i]);

void setup() {

 Blynk.begin(auth, ssid, pass);
timer.setInterval(30000L, getDailyReadings); //Checks every 30 sec for the time 23:59hrs

void loop() {;;