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

error reading home table in qa


Jay Ess

Question

HC3

I have a small table which i store in a global varibale, this is the table.

--Construct Music Table

jsonMusic = {

    Song = {

        S1 = "SholomAleichem", 

        S2 = "KohEchsoif",

        S3 = "JoyShabbos",

        S4 = "ItsTimeToSayGoodShabbos",

        S5 = "Hamavdil",

    }, 

    Length = {

        S1 = 401, --6.41,

        S2 = 483, --8.03,

        S3 = 256, --4.16,

        S4 = 267, --4.27,

        S5 = 332, --5.32

    }

}

--END OF MUSIC TABLE

fibaro.setGlobalVariable("jMusicTable", json.encode(jsonMusic))

 

from a scene i can reference the table and get the values i need.

eg. local jMT = json.decode(fibaro.getGlobalVariable("jMusicTable"))

and then songName = MusicPathPi.. jMT.Song.S1 ..MusicPathEnd

which get me the song i want to play.

 

however if i put this same code in a qa i get the following error:

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

 

this is the lines in my qa.

 

local jMT = json.decode(fibaro.getGlobalVariable("jMusicTable"))

    local songName = jMT.Song.S1

    print(songName)

 

 

What am I doing wrong?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
9 minutes ago, Jay Ess said:

HC3

I have a small table which i store in a global varibale, this is the table.

--Construct Music Table

jsonMusic = {

    Song = {

        S1 = "SholomAleichem", 

        S2 = "KohEchsoif",

        S3 = "JoyShabbos",

        S4 = "ItsTimeToSayGoodShabbos",

        S5 = "Hamavdil",

    }, 

    Length = {

        S1 = 401, --6.41,

        S2 = 483, --8.03,

        S3 = 256, --4.16,

        S4 = 267, --4.27,

        S5 = 332, --5.32

    }

}

--END OF MUSIC TABLE

fibaro.setGlobalVariable("jMusicTable", json.encode(jsonMusic))

 

from a scene i can reference the table and get the values i need.

eg. local jMT = json.decode(fibaro.getGlobalVariable("jMusicTable"))

and then songName = MusicPathPi.. jMT.Song.S1 ..MusicPathEnd

which get me the song i want to play.

 

however if i put this same code in a qa i get the following error:

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

 

this is the lines in my qa.

 

local jMT = json.decode(fibaro.getGlobalVariable("jMusicTable"))

    local songName = jMT.Song.S1

    print(songName)

 

 

What am I doing wrong?

fibaro.getGlobalVariable returns 2 values - the value and the modification date (epoch)

That means that you send 2 value to json.decode.

Unfortunately, json.decode in QAs can take an second modifier parameter - but not a number - so it crashes.

Do like this

Please login or register to see this code.

An extra parenthesis around a multiple return value will only return the first value.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • thank you as always spot on and fixed

     

    if i have a sub table how can i do a reverse lookup in the sub table to get the key from the value?

    Link to comment
    Share on other sites

    • 0
    2 hours ago, Jay Ess said:

    thank you as always spot on and fixed

     

    if i have a sub table how can i do a reverse lookup in the sub table to get the key from the value?

    You can do a "reverse lookup table"

    Please login or register to see this code.

     

    However, the limitation is that you can not have several keys mapping to the same value as the reverse will only preserve one of them.

     

    Edited by jgab
    Link to comment
    Share on other sites

    • 0

    Thank you once again, as usual spot on and resolved. 

    What are the pros and cons of adding a sub-tables? What if I have a subs table? How may I perform a reverse lookup in the subs table in order to retrieve the key from the value?

    And I found the best place to download free videos at 

    Please login or register to see this link.

    why not try it because it's free

    Edited by kieran00music
    Link to comment
    Share on other sites

    • 0

    Well, it depends. It's a slight overhead but it shouldn't really matter as we speak about milliseconds. 

    If you program becomes clearer it is of value.

    It could look something like this

    Please login or register to see this code.

    Note that you need unique leaf-values in the table as the table will be the key in the reverseMap

    Output:

    Please login or register to see this code.

     

    Edited by jgab
    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...