storage object provides persistent key-value data storage for user scripts. Only the following Lua data types are supported:
Sets new value for the given key. Old value is overwritten. Returns boolean as the result and an optional error string.
Gets value for the given key or returns default value (nil if not specified) if key is not found in the data storage.
Note: all user scripts share the same data storage. Make sure that same keys are not used to store different types of data.
The following examples shows the basic syntax of storage.set. Result will return boolean true since the passed parameters are correct
result = storage.set('my_stored_value_1', 12.21)
This example will return false as the result because we are trying to store a function which is not possible.
testfn = function(t)
return t * t
end
result = storage.set('my_stored_value_2', testfn) -- this will result in an error
The following examples shows the basic syntax of storage.get. Assuming that key value was not found, first call will return nil while second call will return number 0 which was specified as a default value.
result = storage.get('my_stored_value_3') -- returns nil if value is not found
result = storage.get('my_stored_value_3', 0) -- returns 0 if value is not found
When storing tables make sure to check the returned result type. Assume we have created a storage item with key test_object_data.
objectdata = {}
objectdata.temperature = 23.1
objectdata.scene = 'default'
result = storage.set('test_object_data', objectdata) -- store objectdata variable as 'test_object_data'
Now we are retrieving data from storage. Data type is checked for correctness.
objectdata = storage.get('test_object_data')
if type(objectdata) == 'table' then
if objectdata.temperature > 24 then
-- do something if temperature level is too high
end
end
Stores alert message and current system time in the main database. All alerts are accessible in the "Alerts" module. This function behaves exactly as Lua string.format.
temperature = 25.3
if temperature > 24 then
-- resulting message: 'Temperature levels are too high: 25.3'
alert('Temperature level is too high: %.1f', temperature)
end
Converts variables to human-readable form and stores them in the main database. All items are accessible in the "Logs" module.
-- log function accepts Lua nil, boolean, number and table (up to 5 nested levels) type variables
a = { key1 = 'value1', key2 = 2 }
b = 'test'
c = 123.45
-- logs all passed variables
log(a, b, c)