Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team
Question
ChristianSogaard 60
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
Please login or register to see this link.
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)
Link to comment
Share on other sites
3 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.