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

[Help Needed] Storing a table in global variable corrupted by panel save


Lambik
 Share

Question

Hi2UAll,

 

I need some help. I'm able to store a table in global variable and read the content. But..

 

When saving is done, by a webbrowser in the HC2 global variable panel, the data is corrupted! The value in the global variable shows NaN (Not a Number).

 

Before saving a table in a global variable in lua I encoded it first. When reading the global variable I use decode.

( json.decode(MYTABLE) )

 

Can somebody help me with this to get it right?

 

Thank in advance!

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

As far as I can tell, you can store Lua strings (byte sequences) in a global variable, and the size is pretty decent. I think Krikroff tested it some time ago, it's thousands of bytes if I remember well. But the variables panel can't handle that. I always thought this is a minor issue, because editing json tables via the variables panel seems awkward to me. Is that the problem that you are facing?

Link to comment
Share on other sites

  • 0

Maybe you want some code to make your scene handle invalid data?

 

Please login or register to see this code.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • The solution you have suggested I had already implemented in my (the Beta version)

    Please login or register to see this link.

    Virtual Device:

    Please login or register to see this code.

    Maybe I didn't do it right, D-vine

    Please login or register to see this link.

    that the error still occurs. I couldn't reproduce the error (yet), but I was looking for another solution which should be bugfree.

     

    So, what I understand is that it is a Fibaro bug and not my coding bug?

     

    Is there a nice way to store a list of id's in a global variable, which could be read again and be stored in a table for further handling?

     

    Thanks in advance!

    Link to comment
    Share on other sites

    • 0

    As far as I know, that is a correct application of "pcall()". Regarding that code snippet: I'd guess if you first test if "devicesWatchTable" is a table (the type() function never fails!), and bail out if it isn't, that you don't need to do a pcall on the length of that table. I have to go through your code, maybe the error is elsewhere? Possibly... this weekend.

    The variables panel corrupts non-numerical data, and I'd call that a "bug". I don't think there is a solution. Only that workaround that you're using: detecting the issue and fixing the corruption.

    I don't know another storage mechanism on an HC2. Some have suggested to use a REST interface to store data externally (SQL database on a NAS, for instance). But I guess you'd like to keep your solution "self contained" in a vfib...

    BTW does this "corruption" also occur *without* people using the variables panel?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • As far as I know, that is a correct application of "pcall()". Regarding that code snippet: I'd guess if you first test if "devicesWatchTable" is a table (the type() function never fails!), and bail out if it isn't, that you don't need to do a pcall on the length of that table. I have to go through your code, maybe the error is elsewhere? Possibly... this weekend.

    The variables panel corrupts non-numerical data, and I'd call that a "bug". I don't think there is a solution. Only that workaround that you're using: detecting the issue and fixing the corruption.

    I don't know another storage mechanism on an HC2. Some have suggested to use a REST interface to store data externally (SQL database on a NAS, for instance). But I guess you'd like to keep your solution "self contained" in a vfib...

    BTW does this "corruption" also occur *without* people using the variables panel?

    In short, when the global variable is changed, it will be detected and when it return a value, not a table, it will 'press' a button from the same virtual device, which fills the table again and stores it to the global variable.

     

    The error doesn't occur when when people do not save the global variable panel.

     

    If you have the time, opportunity and feels like it, please advise. It's highly appreciated!

     

    Btw, you can find the last (Beta) version

    Please login or register to see this link.

    .

    Link to comment
    Share on other sites

    • 0

    Awesome suggestion! 

     

    When I get some time I will have to implement this. Instead I have been writing to a global variable every time a scene runs to force the table to remain a table.

    Thanks for sharing.

    Link to comment
    Share on other sites

    • 0

    Definitely a bug. I get the same NaN corrupted globals when using the save button on the variables web page.

     

    Another fix is to default the global when it is first called in a script using xpcall. Something along the lines of:

    Please login or register to see this code.

    Using pcall or xpcall at least allows the script to continue. Sometimes json.decode() will throw a fatal error so needs to be caught also.

    Link to comment
    Share on other sites

    • 0

    I'am using global variables to store some tables for devices and scenes I have, also table of numbers in words like nT = {one, two, ... , hundred} for TTS engine and never problems with that. Of course I never save variables using panel on web page. I don't see any need to do that since I have VDs to do that. I even make one VD with which I can put system in demo mode and demonstrate how it works depending on manually changing global variables. Demo mode is still in development stage since need to make different scenes to demonstrate capability of the system but it is so fun and still no need to touch global variables panel. Actually testing and demo I can do from phone walking aorund.

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

     

    So, I really want to know why someone need to save anything in global variables panel on web interface except adding or deleting variables themselves, which does not corrupt variables content?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • So, I really want to know why someone need to save anything in global variables panel on web interface except adding or deleting variables themselves, which does not corrupt variables content?

    If it is their it should be bugfree. Doesn't matter where or why users are using it. A lot of users just do.

     

    So when I write code and share it with the community, I don't want that code crashing because of a buggy webinterface. It's a bit strange to warn users not to use the global variables panels when using my shared code...

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    Link to comment
    Share on other sites

    • 0

    If it is their it should be bugfree. Doesn't matter where or why users are using it. A lot of users just do.

     

    So when I write code and share it with the community, I don't want that code crashing because of a buggy webinterface. It's a bit strange to warn users not to use the global variables panels when using my shared code...

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

     

    I didn't wrote in my post that is ok all this problems with global variable panel. But you still didn't answer my question, why would anyone need to click all the way to variable panel to change and save values there during normal use of the home center? I don't do that even during programming and configuring my system so I don't have any problems with corrupted variables. Yes, interface is buggy, but why then force something that can be easily avoided?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I didn't wrote in my post that is ok all this problems with global variable panel. But you still didn't answer my question, why would anyone need to click all the way to variable panel to change and save values there during normal use of the home center? I don't do that even during programming and configuring my system so I don't have any problems with corrupted variables. Yes, interface is buggy, but why then force something that can be easily avoided?

    Well, I used it when coding..... It took some time before I discovered it.

     

    My point is, you have to know to avoid it. I'm not reading all content of this forum, so I guess there are more people who stumbled on this problem and spent a lot of time figuring out why things are not working how they supposed to do. :->

    Link to comment
    Share on other sites

    • 0

    why would anyone need to click all the way to variable panel to change and save values there during normal use of the home center? 

     

    Agree this wouldn't happen during normal use, but I have been known to change predefined globals on the web interface when testing as it's quicker and easier. Changing a predefined variable and saving still causes all JSON globals to NaN.

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