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

qa define variables


Question

Posted

can someone help me to understand when writing a qa, when i would define a variable as local and when not?

 

many thanks.

11 answers to this question

Recommended Posts

  • 0
Posted

@Jay Ess

If you use a local variable, it can only be used within internal QA.
You can then read the variable you enter in the QA settings from external systems / processes.
For example:
IPyourHC3 / api / devices / NUMBERofDeviceQA / properties / quickAppVariables

 

For example, I read data from these variables by the ESP8266 module with the display connected.

  • 0
Posted

I will add one more information. There are still global variables that you will use across the system and in scenes.
They are set to /6.General/Variables

You can also "touch" these variables from QA.

  • 0
  • Inquirer
  • Posted

    thank you, so any variable which is only for the use of the qa should be a local? and then it can be used in any files in that qa but not outside?

    • 0
    Posted

    jgab, who else ?, wrote somthing about it. There is a difference between local, global and quickapp variabel. 
     

    On 5/21/2020 at 3:24 PM, jgab said:

    What is global and local variables in Lua?

    • 0
  • Inquirer
  • Posted
    50 minutes ago, SmartHomeEddy said:

    jgab, who else ?, wrote somthing about it. There is a difference between local, global and quickapp variabel. 
     

    do you have a link to the post please?

    • 0
    Posted (edited)
    17 hours ago, SmartHomeEddy said:

    jgab, who else ?, wrote somthing about it. There is a difference between local, global and quickapp variabel. 
     

     

    There are 4 types of "variables".

     

    A) "Fibaro globals" that are accessed with fibaro.getGlobalVariable/fibaro.setGlobalVariable that is an invention of Fibaro, and these variables can be accessed from Scenes and QAs with the mentioned  functions. They are stored in a common place in the HC3... somewhere. They are also accessible in the Web GUI under the general/variables panel.

    Note that we can only store strings. If we want  to store something else we need to make a string of them , by ex.  using json.encode.

     

    B) "QuickApp variables". These are also Fibaro inventions. They are stored in a property of the QA and is normally only accessed by the QuickApp itself using self:getVariable/self:setVariable.

    These variables are useful for QAs to store configuration data or other "state" data that needs to  be remembered between restarts of the QA. These variables  are also included in the  QA if  you  download it to your  PC etc for backup. We can access the quickApp variables of another QA using API calls by looking through the property  of the QA as @Martin_N explains.

    We can store any value that can be json.encoded in a QuickAppVariable. The self:setVariable will json encode the value for us and self:getVariable will json.decode the value. The reason is that the values are kept in a property table that is json.encoded when stored together with the QA structure.

     

    C) Lua global variables. This is Lua's normal globala variables and they are (in short) variables declared without  a 'local' infront of them.

    Ex.

    Please login or register to see this code.

    A Lua global variable in a Scene will be destroyed when the scene terminates.

    A Lua global variable in a QA will be destroyed when the QA restarts (ex. when the QA is re-saved, or crashes)

    Lua globals in QAs are accessible between a QA's files. Ex. 'fibaro' is a Lua global variable that is a table of the Fibaro functions we have, and the global 'fibaro' is available in all files of a specific QA. Note that Lua globals  are not shared between different QAs as all QAs run within their own Lua environment.

     

    D) Lua local variables.

    They are declared with 'local' infront of them

    Please login or register to see this code.

    and being local they are only accessible within the scope where they are declared. (parameters to a function can be considered locals, and are  scoped to the function)

    If we declare a local  variable outside all functions inside a QA file (ex. the 'main' file) it is only accessible within that  file and can not be accessed from another file in the  QA. locals also "die" when the QA restarts/crashes. ...and local Lua variables can also be used in scenes.  

     

     

    Should you use local or global Lua variables? 

    In short, use local variables wherever you can as minimise the scope  and the risk that you accidentally overwrite the variable (because you  used another  variable with the same name...). Secondly, local  Lua variables are 25x  faster to access  than a global Lua variable (and 5x to set - it's difficult to benchmark these things as the compiler is clever and cache a lot of results). (locals are usually allocated to VM  registers, while globals are looked up via a global Lua table (_G))

     

    Correction. There is actually  "Scene variables" now - similar to the QA's QuickAppVariables.

    Edited by jgab
    • Like 2
    • Thanks 3
    • 0
    Posted (edited)

    scene variables (fibaro.setSceneVariable / fibaro.getSceneVariable) are stored in the database, with their name, scene id and value. So one can't access from other scenes to them, but they perfect to save things which need to be saved, like current status of something, so when scen runs again one can read it back and do somethign with this. I love to use them to save clicks / state of device, or to count loops ...

    Edited by tinman
    • Like 1
    • Thanks 1
    • 0
  • Inquirer
  • Posted

    thank you for the clarification.

    when fetching a global variable using getGlobalVariable in a qa when do we need to double bracket them and when is a single bracket enough?

    for example this brings me back nil when the global variable is an enum with a value of 19.168.1.120

    local SonosPlayer = fibaro.getGlobalVariable("sonosPlayer")

    print("Sonos Variable is",sonosPlayer)

    but if it is a text value then it brings back the correct value as in

    local songNameVariable = fibaro.getGlobalVariable("songName")

    print("Song Name Variable is",songNameVariable)

    where song name is "latesthits.mp3"

    • 0
    Posted
    14 hours ago, tinman said:

    scene variables (fibaro.setSceneVariable / fibaro.getSceneVariable) are stored in the database, with their name, scene id and value. So one can't access from other scenes to them, but they perfect to save things which need to be saved, like current status of something, so when scen runs again one can read it back and do somethign with this. I love to use them to save clicks / state of device, or to count loops ...

    These are also interesting. But only in scene? What is the difference with Fibaro Globals?

    • 0
    Posted
    1 hour ago, Jay Ess said:

    thank you for the clarification.

    when fetching a global variable using getGlobalVariable in a qa when do we need to double bracket them and when is a single bracket enough?

    for example this brings me back nil when the global variable is an enum with a value of 19.168.1.120

    local SonosPlayer = fibaro.getGlobalVariable("sonosPlayer")

    print("Sonos Variable is",sonosPlayer)

    but if it is a text value then it brings back the correct value as in

    local songNameVariable = fibaro.getGlobalVariable("songName")

    print("Song Name Variable is",songNameVariable)

    where song name is "latesthits.mp3"

    You must be doing something else - the example you give works well and would break most of the code out there if it behaved like your example.

    How do you set the variable to 192.168.1.120 ?

     

    55 minutes ago, SmartHomeEddy said:

    These are also interesting. But only in scene? What is the difference with Fibaro Globals?

    Think about them as QuickAppVariables but for Scenes. They are stored together with the Scene - but unfortunately not in the scene structure we can retrieve with  the api. I guess because there is no way to download scenes from the web UI and share them  like we can do with QAs, they  saw no need to store the variables in the scene structure. I think it's kind of pity that they choose that route...

    • Thanks 1
    • 0
  • Inquirer
  • Posted (edited)

    LOL my mistake need to start naming my variables better as i was looking sonos instead Sonos.

    Edited by Jay Ess

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