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

Table Insert LUA


Question

Posted (edited)

Hi ,

 

I have a problem with a piece of LUA code and i'd like to ask for help from the LUA gurus  

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" />

 

Basically what i'm trying to achieve is store some values in an array like array = {January = 1, February = 2} . The number after the equal sign is taken from a global variable that changes as a water meter runs every 100 liters. I've got the code that reads the meter and increments the global variable by 0.1 sorted out.

 

The scene has as trigger the UBS value that reads the meter - every time the UBS is 1 it increments the global variable by 0.1

 

So far so good.  

 

Storing the data is the problem: I'd like to store the data in an array like above.  The problem is that every time the scene restarts the array will be empty again. I want it to be populated with each month and the value.

Also, the scene is started multiple times per day so the code should check if that month is already in the array and if it is just rewrite the value, otherwise insert the month and the value in the array.

 

I hope i was clear enough

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" />

 

My code which at this point sucks

Please login or register to see this image.

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

Please login or register to see this code.

Edited by Momos

16 answers to this question

Recommended Posts

  • 0
Posted (edited)

As I understand you want to store water meter value per month, right?

 

May I suggest to use json table as global variable or virtual device with labels.

Edited by cag014
  • 0
Posted (edited)

As @cag014 said: Use a global variable to store the months table.

Please login or register to see this code.

 
Not tested.... Hope I understood what you were after 

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" />

 

EDIT: fixed the month error - cheers @cag014

Edited by seiq
  • 0
  • Inquirer
  • Posted (edited)

    Doesnt seem to work.  Produces a debug line:

     

     [DEBUG] 22:[DEBUG] 22:17:31: month 27.6

     

    I don't think i understood the suggestion thou: 

    Create a global variable lets say WaterMeter and in first value field put a table like {"January","February","March"."April","May","June","July","August","September","October","November","December"} ?

     

     2

    Edited by Momos
    • 0
    Posted (edited)

    Please try it now (create MonthsData as global predefined variable)

    Please login or register to see this code.

    In general this code creates the table on the fly for every month.

    Have tested and it works... (for example I have set the WaterFlow value to 1)

     

    [DEBUG] 23:47:59: September 1

    Edited by cag014
    • 0
  • Inquirer
  • Posted

    seems to be doing something : [DEBUG] 23:58:53: September 28

     
    How should the variable MonthsData look like ?
     
    And what happens next month when monthsData = {} will reset all the table? will it still keep the previous month ? 
     
    Sorry if the questions are dumb but i'm tired and missing the obvious most likely

    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" /> 
    • 0
    Posted (edited)

    It looks like json table stored as string.

    Add debug after setting global

     

    fibaro:setGlobal('MonthsData', json.encode(monthsData))

    fibaro:debug(fibaro:getGlobalValue('MonthsData')) 

     

    next month the code will add October to the table, so you will be able to see September and October (and so on...)

     

    Just for an example set month to let say January and see how it works. Don't forget to remove the line after test.

     

    local month = os.date("%B") -- month name
    local monthsData

     

    month = "January"

     

    Note:

    The monthsData = {} resets the table only in case the table not exists. (For example you did full recovery to HC2)

    Edited by cag014
    • 0
  • Inquirer
  • Posted

    [DEBUG] 00:57:58: nil
    [DEBUG] 00:57:58: September 29

     
     
    with the debug line added.
    • 0
    Posted (edited)

    Did you create MonthsData global predefined variable?

    In Variables panel

    Edited by cag014
    • 0
    Posted

    Thanks for fixing the error in the code @cag014. Had it that way first... no idea why I changed it 

    Please login or register to see this image.

    /emoticons/default_icon_rolleyes.gif" alt=":roll:" />

     

    Note:

    The monthsData = {} resets the table only in case the table not exists. (For example you did full recovery to HC2)

     

    Has become a habit to put json.decode() in a catch block (pcall or xpcall) when retrieving a global variable. Globals have been known to corrupt or clear themselves which can cause your script to crash. Not so bad in a triggered scene but sucks for the main loop of a virtual device.

    • 0
    Posted

    Did you create MonthsData global predefined variable?

    In Variables panel

     

    Not a global 'predefined' variable. Just a normal variable in the 'Variables Panel' on your HC2 (ie: the top one) Can't write custom values or a json string to a global predefined variable. I'm pretty sure you didn't mean it this way @cag014 

    Please login or register to see this image.

    /emoticons/default_wink.png" alt=";)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> I've seen the two different types get mixed up a bit on these forums. 

    Please login or register to see this attachment.

    • 0
    Posted (edited)

    Not a global 'predefined' variable. Just a normal variable in the 'Variables Panel' on your HC2 (ie: the top one) Can't write custom values or a json string to a global predefined variable. I'm pretty sure you didn't mean it this way @cag014 

    Please login or register to see this image.

    /emoticons/default_wink.png" alt=";)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> I've seen the two different types get mixed up a bit on these forums. 

    attachicon.gif

    Please login or register to see this link.

    seiq, I tested and even use predefined global variable for storing tables and it is working. Just need to make global with one value with any name and I usually name it Table. Value 2 I delete after saving since it can't be removed during definition. Also it is better to use predefined global variable to store tables because it doesn't get corrupted as normal variable when saving in global variable panel.

    Edited by Sankotronic
    • 0
    Posted (edited)

    Also it is better to use predefined global variable to store tables because it doesn't get corrupted as normal variable when saving in global variable panel.

     

    Wow. I'd never actually tried setting a predefined global with a custom string. Can't believe that actually works! haha I was always under the impression the predefined variables were to provide a level of data validation...guess not! Surely that's not intended functionality and just another Fibaro quirk?

     

    One thing I did notice is the predefined variable doesn't show the custom value in the Variables Panel. When I click the save button, the predefined variable with the custom value is overwritten with what is displayed in the drop down list. So it still gets 'corrupted'... 

    Please login or register to see this image.

    /emoticons/default_icon_curve.gif" alt=":-/" />

    Edited by seiq
    • 0
    Posted

    Wow. I'd never actually tried setting a predefined global with a custom string. Can't believe that actually works! haha I was always under the impression the predefined variables were to provide a level of data validation...guess not! Surely that's not intended functionality and just another Fibaro quirk?

     

    One thing I did notice is the predefined variable doesn't show the custom value in the Variables Panel. When I click the save button, the predefined variable with the custom value is overwritten with what is displayed in the drop down list. So it still gets 'corrupted'... 

    Please login or register to see this image.

    /emoticons/default_icon_curve.gif" alt=":-/" />

    What I have found that, the global variables are good for numbers and predefined variable work excellent with strings. 

    • 0
    Posted

    What I have found that, the global variables are good for numbers and predefined variable work excellent with strings. 

     

    Yeah I see the

    Please login or register to see this link.

     site does suggest standard (global) variables are for storing numbers. It also suggests that predefined variables are for storing words (strings) but are supposed to "limit the content of the variables" to a value in the drop down box. So it's a bug that's being exploited to store custom strings. Same goes for storing strings in a standard global which is designed for storing numbers. Personally I've never had a problem (apart from the hitting the save button in the Variables Panel) storing json strings in standard global variables... yet.

     

    IMHO, functionality with this type of design by Fibaro would be:

    - standard globals for storing custom values (strings, integers, booleans [, tables])

    - predefined globals for exactly that; storing values from a predefined list. Most likely a string as they can easily be converted to a number.

     

    Additionally:

    - A value could be returned from fibaro:setGlobal(). TRUE if the value was successfully updated, NIL on failure. Bit of validation never hurt anyone right?

    - The little text box for standard globals could be widened and set read only with an 'edit' button column added (next to delete like the predefined variables have now) to modify and save the value manually if required.

     

    But what Fibaro really need to do first is fix the problem causing the standard globals getting corrupted and set to NaN and update their !@#$%^* documentation so we know how they intend things to work instead of guessing all the time. (Or vice versa; ensure the system operates as it's currently documented

    Please login or register to see this image.

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

    Please login or register to see this image.

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

    • Like 1
    • 0
    Posted

    Absolutely right and well described.

    Let's see what Fibaro has to say about....

    • 0
    Posted

    Yeah I see the

    Please login or register to see this link.

     site does suggest standard (global) variables are for storing numbers. It also suggests that predefined variables are for storing words (strings) but are supposed to "limit the content of the variables" to a value in the drop down box. So it's a bug that's being exploited to store custom strings. Same goes for storing strings in a standard global which is designed for storing numbers. Personally I've never had a problem (apart from the hitting the save button in the Variables Panel) storing json strings in standard global variables... yet.

     

    IMHO, functionality with this type of design by Fibaro would be:

    - standard globals for storing custom values (strings, integers, booleans [, tables])

    - predefined globals for exactly that; storing values from a predefined list. Most likely a string as they can easily be converted to a number.

     

    Additionally:

    - A value could be returned from fibaro:setGlobal(). TRUE if the value was successfully updated, NIL on failure. Bit of validation never hurt anyone right?

    - The little text box for standard globals could be widened and set read only with an 'edit' button column added (next to delete like the predefined variables have now) to modify and save the value manually if required.

     

    But what Fibaro really need to do first is fix the problem causing the standard globals getting corrupted and set to NaN and update their !@#$%^* documentation so we know how they intend things to work instead of guessing all the time. (Or vice versa; ensure the system operates as it's currently documented

    Please login or register to see this image.

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

    Please login or register to see this image.

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

    I totally agree with you!

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