How to use in scenes
All values that can be managed in QuickApps are also available externally, for example from a scene. The values can be retrieved with the help of the json object published and periodically updated in the QuickApp values variable, while they can be written by calling the set_value() function of the QuickApp with the appropriate parameters. The QuickApp also publishes its own ID in the NilanQAID global variable after installation to make it easier to access its own variables.
Example
The example scene that implements and demonstrates the above functions can be downloaded here. Feel free to use it, I grant all rights to convert the code and use it for free or resell it.
Dumping objects
This is a helper function that dumps any lua variable in a human readable form
-- Helper function to dump any type of variable
function dump(o)
if type(o) == 'table' then
local s = ''
local sep = ''
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. sep .. k ..': ' .. dump(v)
sep = ", "
end
return '{ ' .. s .. ' }'
elseif type(o) ~= 'number' then
return '"'..tostring(o)..'"'
else
return tostring(o)
end
end
Get/Set QuickApp variables
Getting and setting the values of the quickapp is possible by calling the nilan_var(name, value) function.
function nilan_var(name, value)
local NilanQAID = tonumber(hub.getGlobalVariable("NilanQAID"))
if not NilanQAID then
print("Error: Nilan QuickApp not found.")
return
end
if (NilanVars == nil) or (name == nil) then
local NilanDev = api.get("/devices/"..NilanQAID)
for _,v in ipairs(NilanDev.properties.quickAppVariables) do
if v.name == "values" then NilanVars = json.decode(v.value) end
end
end
if name ~= nil then
local old_value = NilanVars[name]
if (value ~= nil) and (value ~= old_value) then
hub.call(NilanQAID, "set_value", name, value)
NilanVars[name] = value
end
return old_value
end
end
This function can be used in two ways:
-
If called without any parameters, after it is run, all the values of the variables will be available in the
NilanVars
variable as an associative array. For example, the target temperature value will be read from theNilanVars["target_temp"]
variable. -
If I want to change the value of a variable (this will only work for writable parameters), I need to specify the name of the variable and its new value as a parameter, e.g.
old_value = nilan_var("target_temp", 23.3)
. Then the function returns the previous value of the variable (old_value
).
The reference list of available variables can be found here