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

How to store a table in global variable ?


gucio1234
 Share

Question

How to store a table in global variable ?

 

I tried  :

--The table has predefined values:
--lights2checkstatus[1] = 0
--lights2checkstatus[2] = 86

 

-- I copy local variable (1 dimensional table)  to global variable :


fibaro:setGlobal("LightsStatus", lights2checkstatus)

 

--where  lights2checkstatus is 1-dimension table (0,86).

 

-- I copy global variable to local variable  "lights2checkstatus1" :

 

local lights2checkstatus1 = {}
lights2checkstatus1 = fibaro:getGlobal("LightsStatus")

 

-- I debug values of local variables "lights2checkstatus" and  "lights2checkstatus1" :

 fibaro:debug(lights2checkstatus)
 fibaro:debug(lights2checkstatus1)
 
 -- The result of debug is eg. :
 table: 0x8731ca0
 table: 0x8731ca0
 
Which means  lights2checkstatus and lights2checkstatus1 are same.
There was read from the global variable the same what was earlier written to it.
 
 But each time I rune the scene (with the same values of the table (0,86) )
 I have different values :
 
table: 0x8828500
table: 0x8828500

 

table: 0x93f6938
table: 0x93f6938

and so on ...

 

 

What I do wrong ?

How to store in a global variable a table , eg. my test table (0,86) ?

 

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

You cannot use fibaro.debog or print for tables.

If you want to dump a table you must dump it by iterating through table.

 

Also for saving/restoring a table in global variables you might use json.encode and json.decode.

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Alex,

     

    Thank you very much for the hint. I managed to store and read back the table to/from Global Variable.

    Link to comment
    Share on other sites

    • 0

    Hi Alex / Gucio1234,

     

    Would you mind explaining how you did this loading and saving of a table from and to the Global Variables please? I am a senior programmer (VB.net/C#) but am only at the beginning of my journey with Lua and Home Center 2. My final aim is to share tables of information between scripts but the Global variables page is very limiting and I cannot seem to find any solutions on the forum.

     

    This is an example of an array/table that I want to be able to access by different a shared script or file. The data within the array is not static and requires changing by multiple scenes.

     

    local lightArray = { }
    lightArray['192'] = { 'Kitchen', 195, 1, 40, 360 }
    lightArray['125'] = { 'Hallway', 64, 0, 0, 60 }
    lightArray['11'] = { 'Living Room', 84, 0, 0, 360 }
    lightArray['72'] = { 'Office', 21, 1, 80, 360 }
    lightArray['167'] = { 'Landing', 58, 0, 0, 60 }

     

    Any ideas? Any help would be most appreciated.

     

    Cheers

    Trev

     

    Edited by TrevorNewsome
    Thanks but I think I have found a solution having searched JSON variables. Thanks alot though.
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • TrevorNewsome,

     

    Here it is :

     

     --WRITE INTO GLOBAL VARIABLE "LightsStatusIn" the content of local array "lights2checkinsidestatus":
      fibaro:setGlobal("LightsStatusIn", tostring(json.encode(lights2checkinsidestatus)))

     

     

     --WRITE INTO LOCAL ARRAY "lights2checkinsidestatus" the content of the global variable  "LightsStatusIn":
      lights2checkinsidestatus = json.decode(tostring(fibaro:getGlobal("LightsStatusIn")))  
     

     

    Edited by gucio1234
    update
    Link to comment
    Share on other sites

    • 0

    Hi 

    Please login or register to see this link.

     

    Please login or register to see this link.

    Can you please write a simple LUA  to write the content of for example variable "weatherstate" table to a global variable "RainAmount"

    Edited by JEAU
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @JEAU

     

    as wrote earlier :

     

     

     

     --WRITE INTO GLOBAL VARIABLE "RainAmount" the content of local array "weatherstate":


      fibaro:setGlobal("RainAmount", tostring(json.encode(weatherstate)))

     

    Link to comment
    Share on other sites

    • 0

    Thanks 

    Please login or register to see this link.

     

    Please login or register to see this link.

    Thanks a lot for your reply that worked, can I ask another question, How to see the content of a Variable Table 

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @JEAU,

     

    again as I wrote earlier :

     

    --WRITE INTO LOCAL ARRAY "weatherstate" the content of the global variable  "RainAmount":


      weatherstate= json.decode(tostring(fibaro:getGlobal("RainAmount")))  
     

    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.

     Share

    ×
    ×
    • Create New...