NV table - best way to store timestamp/value


I’m looking for suggestions for the best way to store timestamps and associated numeric reading into the NV table. I’m thinking there are two approaches. 1/ create a 2D array in the NV table (no success but probably just my coding skills) or 2/ create a 1D array then store the timestamps and values sequentially in the same array.

Suggestions welcome.

I think I’d use the timestamps as the table’s keys, something like

`local value = getReading();
local latestTimestamp= time();
nv[latestTimestamp] <- value;`

You can then iterate back through possible timestamps, see if there’s a match in the table’s keys and if there is, extract the ‘found’ key’s value.

Bear in mind though, that nv is power-maintained RAM, so it will lose its contents if the imp’s power is lost. And its capacity is limited: just under 4KB, which may not be enough if you have a lot of data to store.

One alternative is to add a low-cost I2C-connected 32KB Ferroelectric RAM chip to your project and make use of our Serializer and MB85RC libraries. FRAM is non-volatile and fast.

If you prefer to access the values timestamps in order than 2 arrays works really well.
if ( !("nv" in getroottable() && "stamps" in nv && "values" on nv)) { nv <- { stamps = [], values = []}; }else{ nv.stamps.push(time()); nv.values.push(getValue()); }
This is very efficient (more so than a 2D array) but is still quite easy to access (more so than a single array with both values in it). It also allows you to guarantee order when iterating over the elements which is a bit harder with a table.

Thanks Brandon, your suggestion works great and I’m one happy camper. Thanks for your suggestions too Smittytone.