Data persistance problems

I defined some variables :
Setting <- {}; Setting.TsKey <- "Unknown"; Setting.TsRefreshDelay <- 120; Setting.SiteName <- "Unknown"; Setting.SiteDescription <- "Unknown"; Setting.SiteAdress <- "Unknown"; Setting.SiteContactInfo <- "Unknown"; Setting.EmailRecipient <- "Unknown"; Setting.OtherDetails <- "";

and in the code, I have :
local table = server.load(); if (table.len() != 0) { Setting = table; } imp.wakeup(1,Main);

But whenever i’m trying to use the variable Setting.SiteName I got an error that say " ERROR: the index ‘SiteName’ does not exist"

I really dont get it

Did you add ‘SiteName’ to the table after doing a server.save()? if so the table loaded in the second block of code above will not include it. Where do you do your initial server.save() (to persist the data?) I would do something like:

`local table = server.load();
if (table.len() != 0) {
    // Table loaded; assign to Setting
    Setting = table;
} else {
    // No table loaded, ie. first run, so save the defaults
    server.save(Setting);
}

imp.wakeup(1,Main); `

FWIW, in my code I have a flag which I set to true every time I want the code to overwrite an existing persisted table, allowing me to add in new keys without error.

Ok, but why does this dont even work? :
`// Establish the device settings table with default values
clockPrefs <- {};
clockPrefs.mode <- true;
clockPrefs.bst <- false;
clockPrefs.utc <- false;
clockPrefs.offset <- 12;
clockPrefs.brightness <- 15;

// Load the settings table in from permanent storage
local settings = server.load();

// If no preferences have been saved, settings will be empty
if (settings.len() != 0) {
// Settings table is NOT empty so set the clockPrefs to the loaded table
clockPrefs = settings;
}

function Main(){

server.log("Brightness" + clockPrefs.brightness);
imp.wakeup(1,Main);  

}

Main();`

I mean, its the exact code from here : https://electricimp.com/docs/api/server/load/

What do you mean by ‘it doesn’t work’? You should see ‘Brightness15’ in the log. The code above doesn’t do anything else. If you’re expecting to see a different value, you won’t because you don’t ever call server.save(clockPrefs) to write your settings to permanent storage (at least not in the code above).

Got it,
server.save(clockPrefs) need to run once…always

More specifically server.save needs to be called any time you change data in your table.

You may want to look at this code snippet:

It’s not well documented, but here is some completely untested code from memory which might do what you want:
`
//Set Defaults (Note: Second parameter of read is default value if they’ve never been set)
Persist.read(“mode”, true);
Persist.read(“bst”, false);
Persist.read(“utc”, false);
Persist.read(“offset”, 12);
Persist.read("brightness, 15);

//Called from code not shown here
function changeBrightness(value){
Persist.write(“brightness”, value);
}

function Main(){
server.log(“Brightness” + Persist.read(“brightness”);
imp.wakeup(1,Main);
}
Main();
`