Jump to content

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


  • 2

HC3 how to create a Global Variable from LUA


ChristianSogaard

Question

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

  • 0

Please login or register to see this link.

Please login or register to see this image.

/monthly_2020_05/image.png.cbc5e348b85c81654a655b7d2b4a220d.png" />

Link to comment
Share on other sites

  • 1
Guest MartienP

I would think this would be nice if it could be build in the interface where you program the lua scripts. I never used a HC2 but from what I read it was supported direct from the lua development. Which is a logical place.
Of course as a programmer I managed to do it from the swagger interface, but this is very low level and error prone working. Certainly way too complicated for a novice programmer. Apart from having support for enums, making a simple CRUD like userinterface for global variables should not be that hard to add! At least I vote for such a addon!

For the rest I'm pretty happy with my HC3, it does what it is supposed to do. 

Link to comment
Share on other sites

  • 0

try

 

Please login or register to see this code.

 

Edited by leegoo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...