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


  • 0

HC3 Store a table into Global Variable


Question

Posted

Hi All,

 

I want to store a table into a variable, this looks fine:
 

function QuickApp:onInit()
self:debug("onInit")
Table={}
 
local TimeHour = os.date('%H')
-- Array Session
Table[TimeHour]="1000"
Table[TimeHour+1]="1501"
Table[TimeHour+2]="2500"
 
hub.setGlobalVariable("TableVAR", json.encode(Table))
end

 

The TableVAR has the following content:

Please login or register to see this attachment.

 

 

But now I want the read this TableVAR again back into the Table array.

That is the part I don't get to work..

 

Table=json.encode(hub.getGlobalVariable("TableVAR"))

Gives the error:
/usr/share/lua/5.3/json/util.lua:55: bad argument #1 to 'for iterator' (table expected, got number)

 

And when I try to read the Global Variable like this:

Table=json.encode(tostring(hub.getGlobalVariable("TableVAR")))

 

The output is:

print ("Table" .. Table)

Table"{\"12\":\"1501\",\"13\":\"2500\",\"11\":\"1000\"}"
 

But the I can't use this as a table..

print ("Table field 12 " .. Table["12"])

Gives an error:
main.lua:20: attempt to concatenate a nil value (field '12')
 

Which part am I missing here? What is the correct way to handle a table in a Global Variable?

 

Thanks in advance.

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

@Nevicare

If you read out a globalvar you get 2 return arguments: the value and a timestamp

So you need to define a variable to just save the globalvar itself

Please login or register to see this code.

 

Then you can save the new variable as json table like this and there you can then use it as table

Please login or register to see this code.

 

Edited by Stycx
  • Like 1
  • 0
Posted

Please login or register to see this code.

The extra (...) discards the second result from getGlobalVariable that interferes with json.decode

 

  • Like 2
  • 0
  • Inquirer
  • Posted

    Thanks Stycx,

    That works :)

     

    Now I want to have a for-loop to walk through this table.

    But the for loop below doesn't give any output at all..

     

    print(hub.getGlobalVariable("TableVAR"))
    tableVar,_ = hub.getGlobalVariable("TableVAR")
    print(tableVar)
     
    Table2 = json.decode(tableVar)
     
    for k,r in ipairs(Table2)
    do
      print("Table " .. Table2[k] .. " output r " ..  r )
    end
    • 0
    Posted

    Use 'ipair' for an array
    Use 'pair' for a key/value table

    • 0
  • Inquirer
  • Posted

    Thanks all, that did the trick.

     

    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...