Nv table - read and write more than one value

Hi there.
Is there way where i can store more that one value in the NV table ? - and read it again ?
I want to save a value - read from a pulse sensor and store it in the NV table - at the same time a want to save the timestamp - so i can find out how many pulses in a minute and in a hour.
Any help would be appreciated
Best Regards
Christian

Did you see the example code in imp development guide for nv table.

In example it initialises a “count” variable in the nv table nv <- { count = 0 }

Then increments using nv.count++

All you need to do is add in a timestamp variable. You have 2 squirrel functions available to choose from date() and time(). If you use time() you would then could use this code:

`
// initialise
nv <- {
count = 0,
p_time = 0
}

// then if we assume p_count is your pulse count
// you store values using
nv.count = p_count;
nv.p_time = time();
`

As you wanted to store more than one value, you would then use an array to store values as this is more efficient. See the following link for example.

Here’s one way of doing it using append method (I’m sure there are other ways):

`
// initialise
nv <- {
count = [],
p_time = []
}

// then simply append data… just remember to check size of nv table as limited to 4k
nv.count.append(p_count);
nv.p_time.append(time());
`

Thanz . Gerriko. I think it was the
nv <- {
count = 0,
p_time = 0
}
i was missing - i will try this evening :slight_smile:

Any idea why the function TimeOfDay newer reaches inside the IF.
When the minute passes the 28 it does not do the Mylog check part.

or another idea to check when a minute or an hour has passed ?

question two
What is the way to do the graybox around the code ? (in this forum)


function TimeOfDay() { local d = date(time() + TIMEZONE_OFFSET, 'l'); mylog(format("%02d:%02d:%02d", d.hour, d.min, d.sec)); mylog("minutter er: " + d.min); mylog("dag er: " + d.day); if (d.min == "28") { mylog("Vi har rundet en time ...... " + nv.p_time); mylog("Pulses accumulated ...... " + nv.count); nv.count = 0 } }

function mylog(note){
if (server.isconnected()){
server.log(note);
}
}

imp.onidle(function() {
/*server.log(imp.getmacaddress());
server.log(hardware.getdeviceid());
server.log(imp.getsoftwareversion());
*/
TimeOfDay()
server.sleepfor(60 - (time() % 60));
});

Your problem is that you are trying to compare d.min which is an integer with a string. Remove the quotes arround the 28.

To get the gray box use the code tag. That is the C at the top of the comment or composition box.

Q1: d.min is a value not a string. hence something like if (d.min == 28) or if (d.min >= 28) should do the trick rather than if (d.min == "28")

Q2: you enclose your code between html “<“code”>” and “<”/code">" tags (ignore the quotation marks) or click on the “C” icon which is found just above the edit box.

Gents !!! thank you. I had more working this evening compared to the last tree weeks

I am getting this error

2015-04-08 20:45:56 UTC+2 [Device] ERROR: the index ‘nv’ does not exist
2015-04-08 20:45:56 UTC+2 [Device] ERROR: at TimeOfDay:36
2015-04-08 20:45:56 UTC+2 [Device] ERROR: from :84

is it because the variable is set in another function ?

`
// Læser Pulser på Emernet E420

// 10.000 pulser/kwh = power eq Pulses x 6
// 500 pulser / kWh = power eq Pulses x 120

// Dong El Pris gælder 1. kvt 2014 228,04 øre

// INspiration to read
// https://discourse.electricimp.com/discussion/3348/arrays-in-nv-table

// queue <- [];
counter <- hardware.pin1;
counter.configure(PULSE_COUNTER, 30); // Count in x Seconds
Pulses <- 0;
Power <- 0;
count <- 0;
TIMEZONE_OFFSET <- 7200; // Plus 2 timer

function hourly() {
  /*  agent.send("hourlydata", nv.data);
      nv.data = "";
  */  
 mylog("LÆKKERT nu er der gået en time");
}
 
function TimeOfDay() {

 mylog("TimeOfDay() ...... ");
    local d = date(time() + TIMEZONE_OFFSET, 'l');
    mylog(format("%02d:%02d:%02d", d.hour, d.min, d.sec));
    mylog("minutter er: " + d.min);
    mylog("dag er: " + d.day);
    if (d.min == 45)  
          {    
  //        mylog("Vi har rundet en time ...... " + nv.p_time);
<blockquote>-> line 36   </blockquote>       mylog("Pulses accumulated ...... " + nv.count);
  //        nv.count =0
    mylog("LÆKKERT nu er der gået en time");
          }
  } 
 
function mylog(note){
    if (server.isconnected()){
        server.log(note);
    }
  //      server.log(note);
}
 
function GetApproximateFrequency() {
  local numPulses = counter.read();
  local approxFreq = numPulses * 2.0; // ganger med x for at få et minut
  return approxFreq;
}
 
function SendDataToCloud() {
 mylog("SendDataToCloud() ...... ");
 
  if (!("nv" in getroottable() && "p_min" in nv)) 
    {
      nv <- { count = 0,  p_min = 0, p_hour = 0  }
    }
          
    Pulses = GetApproximateFrequency() ;       
//    server.log("Now i read " + Pulses + "Pulses");
    
    nv.count += (Pulses);
    mylog("Pulses accumulated ...... " + nv.count);
    
    Power = (Pulses * 6);
    local PowerA = (nv.count * 6);
    
    server.log("Power er " + Power + " Power");
//    mylog("Pulses accumulated ...... " + nv.count);
    //agent.send("EmonCms", Pulses, power, PowerAcc);
    local jsonString="{Pulses:"+Pulses+",Power:"+Power+",count:"+PowerA+"}"; 
    agent.send("EmonCms",jsonString);     // Sending data to "agent"
}

imp.onidle(function() {
/*server.log(imp.getmacaddress());
server.log(hardware.getdeviceid());
server.log(imp.getsoftwareversion());
*/
  TimeOfDay()
  SendDataToCloud();
  server.sleepfor(60 - (time() % 60));
});`

Thank you - the code is working now

`
/* 
Læser Pulser på Emernet E420
10.000 pulser/kwh = power eq Pulses x 6
500 pulser / kWh = power eq Pulses x 120
Dong El Pris gælder 1. kvt 2014 228,04 øre
*/

counter <- hardware.pin1;
counter.configure(PULSE_COUNTER, 30); // Count in x Seconds
Pulses <- 0;
Power <- 0;
count <- 0;
TIMEZONE_OFFSET <- 7200; // Plus 2 timer

function mylog(note){
  server.log(note);
    if (server.isconnected()){
        
    }
}
 
function GetApproximateFrequency() {
  local numPulses = counter.read();
  local approxFreq = numPulses * 2.0; // ganger med x for at få et minut
  return approxFreq;
}
 
function SendDataToCloud() {

  if (!("nv" in getroottable() && "count" in nv)) 
    {
      nv <- { count = 0,  p_min = 0, p_hour = 0  }
    }
          
    Pulses = GetApproximateFrequency() ;       

//  ADDERER og gemmer pulserne i NV.COUNT    
    nv.count += (Pulses);
    mylog("Pulses accumulated ...... " + nv.count);
    
    Power = (Pulses * 6);
    local PowerA = (nv.count * 6);
    
    local d = date(time() + TIMEZONE_OFFSET, 'l');
    mylog(format("%02d:%02d:%02d", d.hour, d.min, d.sec));

    if (d.min == 00)  
          { 
// The HOUR changed, so we are resetting the Accumulated pulses       
           mylog("Vi har rundet en time ...... " + nv.p_min);
           nv.count = 0;
          }
     mylog("Pulses siden sidste timeskift ...... " + nv.count);

    //agent.send("EmonCms", Pulses, power, PowerAcc);
    local jsonString="{Pulses:"+Pulses+",Power:"+Power+",count:"+PowerA+"}"; 
    agent.send("EmonCms",jsonString);     // Sending data to "agent"
}

imp.onidle(function() {
/*server.log(imp.getmacaddress());
server.log(hardware.getdeviceid());
server.log(imp.getsoftwareversion());
*/
  
  SendDataToCloud();
  server.sleepfor(60 - (time() % 60));
});
`