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


Recommended Posts

Posted

 

 

I’m struggling with the syntax for “table.insert” on nested tables and can’t find the answer on tutorials I've read. Any help much appreciated.

 

This is the table format I’m using

 

bright = { 

rgbw_1 = {r=38, g=13, b=1, w= 1},

rgbw_2 = {r=43, g=23, b=88, w= 20},

}

 

I can call a value without a problem using…

 

r=bright.rgbw_1.g

 

I can update a value without a problem using…

 

bright.rgbw_2.r=60

 

What I cannot figure out the syntax for is inserting a new line into the table. I’ve tried every combination I can think of

 

table.insert(bright,{rgbw_3={ r = '*1', g = "*2", b = '*3', w = '*4'}}) - no line error but pairs throws error "bad argument #1 to 'for iterator' (table expected, got nil)""

 

table.insert(bright{rgbw_3,{ r = '*1', g = "*2", b = '*3', w = '*4'}}) – error “attempt to call a table value (global 'bright')”

 

table.insert(bright,{rgbw_3{ r = '*1', g = "*2", b = '*3', w = '*4'}}) – error” attempt to call a nil value (global 'rgbw_2')”

 

table.insert(bright,[rgbw_2,]{r = '*1', g = "*2", b = '*3', w = '*4'})- error “unexpected symbol near '['”

 

table.insert(bright,[rgbw_2]={r = '*1', g = "*2", b = '*3', w = '*4'})- error “unexpected symbol near '['”

 

table.insert(bright,[rgbw_2,]={r = '*1', g = "*2", b = '*3', w = '*4'})- error “unexpected symbol near '['”

 

table.insert(bright,{"rgbw_3"={ r = '*1', g = "*2", b = '*3', w = '*4'}})– error” '}' expected near '='”

 

table.insert(bright{"rgbw_3",{ r = '*1', g = "*2", b = '*3', w = '*4'}})– error “attempt to call a table value (global 'bright')”

 

-table.insert(bright,{"rgbw_3"{ r = '*1', g = "*2", b = '*3', w = '*4'}})– error “--'}' expected near '{'”

 

As usual probably something super simple. Thanks for helping.

Posted (edited)

Please login or register to see this code.

You're trying to append an object to a dictionary.

You should use association like

Please login or register to see this code.


To make it happen your dictionary should become an array (there should be no keys). Something like this:
 

Please login or register to see this code.


Rest of your examples are not a valid LUA syntax.

Same issue when you iterate over array and dictionary:
 

Please login or register to see this code.


VS
 

Please login or register to see this code.


When you access dictionary you use keys, like this:

 

Please login or register to see this code.


But when you access array you need to go like this:
 

Please login or register to see this code.

 

Edited by irekk
  • Topic Author
  • Posted

    Hi there @irekkThanks for your help, I really appreciate it.

     

    To use the array as you describe I will need to change how I use the data from tables, but I think its possible. In the mean time I made some new learning today which are confusing me……

     

    Part 1 – from yesterday

    As mentioned yesterday, I can create a file in the format I like to use and save as global variable. I can access the dictionary and update it, but for the reasons you described, I am not able to insert new lines.

     

    bright = { 

    rgbw_1 = {r=38, g=13, b=1, w= 1},

    rgbw_2 = {r=43, g=23, b=88, w= 20},

    }

     

    Appears like this as a global variable

     

    {"rgbw_2":{"w":20,"g":23,"r":43,"b":88},"rgbw_1":{"w":1,"g":13,"r":38,"b":1}}

     

    I can access it like this

     

    bright=json.decode(fibaro.getGlobalVariable("gTestBright2"))

    local r

    r=bright.rgbw_1.g

    print(r)

     

    returns 13

     

    But as you explained I can’t add new lines using table.insert

     

    Part 2 – from today

     

    I started with an empty array {}, saved as a global variable. Then I was able to use table.insert to insert data in the format I prefer. See example below. 

     

    I created an empty {} and saved as a global variable

     

    bright = {}

    jt=json.encode(bright)

    fibaro.setGlobalVariable("gTestBright2",jt)

     

    When I look at the variable it is stored as

     

    []

     

    Next I can insert data in the format I want to use

     

    bright=json.decode(fibaro.getGlobalVariable("gTestBright2"))

     table.insert(bright,{rgbw_3={ r = '*1', g = "*2", b = '*3', w = '*4'}}) 

    jt=json.encode(bright)

    fibaro.setGlobalVariable("gTestBright2",jt)

     

    When I look at the variable it is stored as

     

    [{"rgbw_3":{"w":"*4","b":"*3","g":"*2","r":"*1"}}]

     

    Interesting, it’s the same as Part1 but with [   ] at the ends

     

    I can add insert another line in the format I prefer

     

    bright=json.decode(fibaro.getGlobalVariable("gTestBright2"))

     table.insert(bright,{rgbw_4={ r = '*10', g = "*20", b = '*30', w = '*40'}}) 

    jt=json.encode(bright)

    fibaro.setGlobalVariable("gTestBright2",jt)

     

    When I look at the variable it is stored as

     

    [{"rgbw_3":{"w":"*4","r":"*1","g":"*2","b":"*3"}},{"rgbw_4":{"g":"*20","w":"*40","b":"*30","r":"*10"}}]

     

    But I can’t access the file

     

    bright=json.decode(fibaro.getGlobalVariable("gTestBright2"))

    local r

    r=bright.rgbw_1.g

    print(r)

     

    error “attempt to index a nil value (field 'rgbw_1')”

     

    r=bright[rgbw_1][g]

     

    error – “attempt to index a nil value (field '?')”

     

    Summary

     

    The first approach I can make a dictionary, I can save as a global variable, I can access & update data but I can’t insert new data rows

     

    The second approach I can make a dictionary, I can save as a global variable, I can insert new lines in the format I prefer but I can’t figure out how to access the data !!

     

    If I can work out how to access the data in the second approach, then bingo !! Otherwise I will reconfigure the way I work with data files into simple arrays.  Any thoughts on this? Thanks in advance

    • 1 month later...
    Posted

    Please login or register to see this code.

     

    I wrote the above to see if I understand how to add a "row" (rgbw_3 or rgbw_4) to my "file" (bright).  Another thing to consider, when you want to remove a "row", just set the key value to nil. Thus, bright["rgbw_2"] will remove the information stored at that key. The thing is, if you use a dictionary layout (as in the above), you merely reference the new key ("rgbw_3") with your new data ({r=46, g=53, b=108, w=25}). It has now been added. BUT, when you execute the above with the for...pairs() syntax, you will see the information is returned in an unordered list. However, the true value of using the key, data table idea is that when you want to get to a particular key, you merely have to reference it. If you are using an array table, then the only way to get to the "key" you want, you must pass through the table until you discover the key you want. This is the traditional means of storing information in traditional programming languages and is, perhaps, the easiest way to understand data storage.

     

    I hope this will help you understand the storage methods in Lua. I am new to all this myself. Honestly, I find that the syntax of these tables is almost too easy to believe. That was what prompted me to write the above snippet.

     

    Good luck.

     

    PeterV959

    Join the conversation

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

    Guest
    Reply to this topic...

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