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

simple qa which allows me to add 1 to a stored variable


Question

Posted

Hi,

I am trying to make a simple qa with two buttons and one label, which shows me the number stored in a variable and if i press one botun it will add one to the variable and the other button is take off 1.

I have

function QuickApp:UpdateNightLevelVarable(NewLightLevel)

    fibaro.setGlobalVariable("upHallwayNightLevel", NewLightLevel)

    --NightLevelVariable = fibaro.getGlobalVariable("upHallwayNightLevel")

    --self:updateView("lblNewNightLevel","text",NightLevelVariable)

end

 

function QuickApp:PlusOne()

    NewLightLevel = NewLightLevel + 1

    print (NewLightLevel)

    self:UpdateNightLevelVarable(NewLightLevel)

end

 

function QuickApp:MinusOne()

    NewLightLevel = NewLightLevel - 1

    print (NewLightLevel)

    self:UpdateNightLevelVarable(NewLightLevel)

end

When I press the plus1 or minus1 i get this error:

[16.03.2021] [17:06:45] [ERROR] [QUICKAPP275]: QuickApp crashed

[16.03.2021] [17:06:45] [ERROR] [QUICKAPP275]: Unknown error occurred: handleJsonRpc

 

i assume it is due to the param i have in the function UpdateNightLevelVarable, can you tell me what is the proper way of sending the number when i call the function please?

4 answers to this question

Recommended Posts

  • 0
Posted

Hi @Jay Ess,

 

I have write and tested this code for you:

 

Please login or register to see this code.

 

Above code will add global variable to system automatically if is missing. Your code didn't work for several reasons:

  1. function fibaro.getGlobalVariable() is always returning two data, global value (v in code) and time when was last changed (t in code).
  2. function fibaro.getGlobalVariable() is always returning global variable value as string, so it needs to be converted to number for doing calculations. Normally LUA supports comparison of numerical values that are stored as string type eg. comparing time "10:00" > "09:00" will give true answer, but for calculations the best is to convert numerical values to numeric format with tonumber("11") == 11
  3. For some reason function updateView does not like arguments in numeric format except if they are concatenated to the string:
    • self.updateView("label","Night level: "..v) - this format works
    • self.updateView("label",v) - this format doesn't work if v is formatted as number
    • self.updateView("label",tostring(v)) - this format work since v is formatted to string

Hope this shed some light on why sometimes code fails.

I have also added checking of the MIN and MAX values since usually dimmer values goes from 0 - turned off to 99 - max brightness. (yes 99 is correct and some devices doesn't work if 100 is sent as brightness value.

 

Enjoy coding :-)

 

  • 0
  • Inquirer
  • Posted

    @Sankotronic  thank you ever so much for your help, amazing help and helps me to get a better understanding of the lua code at the same time.

     

    One more question please, if the value is at 99 how can i hide the plusone button?

    • 0
    Posted
    On 3/21/2021 at 11:06 AM, Jay Ess said:

    One more question please, if the value is at 99 how can i hide the plusone button?

     

    Here is solution, not with hiding buttons, but with changing label:

    Please login or register to see this code.

     

    You can of course replace MIN and MAX labels with anything you like more. I have also added that QA shows proper value of the global variable when QA is restarted

     

     

    • 0
  • Inquirer
  • Posted
    10 hours ago, Sankotronic said:

     

    Here is solution, not with hiding buttons, but with changing label:

    Please login or register to see this code.

     

    You can of course replace MIN and MAX labels with anything you like more. I have also added that QA shows proper value of the global variable when QA is restarted

     

     

    thank you for your insight and help

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