How do I create a Global Variable from LUA in HC3?
I'm trying to create a light dimmer function for Philips HUE using a Logic Group Matrix ZDB5100 switch so when I hold the Button its starts dimming up or down. But to keep track if the button is released and pressed again I need to keep track of the direction from last time it was pressed.
So I'm thinking of using an Enumerated global variable "dimDirection" that can be set to Up or Down.
I want the code to create the variable if not present.
My working code whiteout remembering dimDirection is here
https://pastebin.pl/view/d74c93bb
In HC2 i used this snippet of code.
But how is it in HC3 ?
local ProjectorStatus = {On="Tændt", Off="Slukket"};
-- in code you then declare predefined global variable using users mapped values:
local varName = {
["Projector"] = {
value = ProjectorStatus.On,
isEnum = true,
isTable = false,
enumValue = {ProjectorStatus.On,
ProjectorStatus.Off}
},
}
function checkGlobalVariable(varName)
-- Check if variable exist
for i , v in pairs(varName) do
if fibaro:getGlobalValue(i) == nil then
api.post("/globalVariables/", {
name = i
});
fibaro:sleep(400)
api.put("/globalVariables/"..i, {
name = i,
value = v.value,
isEnum = v.isEnum,
enumValues = v.enumValue
})
if v.isTable then
ok, msg = pcall(function() wT = json.decode(fibaro:getGlobalValue(i))
if type(wT) ~= "table" then error() end end )
if not ok then
fibaro:debug("ERROR - Creating table was unsuccessful, error code: "..msg)
else
fibaro:debug("Variable '"..i.."' created successfully.");
end
else
if fibaro:getGlobalValue(i) ~= nil then
fibaro:debug("Variable '"..i.."' created successfully.");
else
fibaro:debug("ERROR - Variable '"..i.."' was NOT created!")
end
end
end
-- update all predefined global variables in case user changed default values
if v.isEnum then
fibaro:debug("Variable '"..i.."' allready exists.");
local value = fibaro:getGlobalValue(i)
for i,m in pairs(v.enumValue) do
if m == value then
v.value = value
end
end
if v.value == value then
fibaro:debug("For variable '"..i.."' found current value ("..value..") and will keep it")
else
fibaro:debug("For variable '"..i.."' NOT found current value, will set default value ("..value..")")
end
api.put("/globalVariables/"..i, {
name = i,
value = v.value,
isEnum = v.isEnum,
enumValues = v.enumValue
})
fibaro:sleep(400)
end
end
updateVar = true
end
checkGlobalVariable(varName)