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

Basic LUA questions


knuth

Question

Below is a simple LUA script to turn off most lights at night. The script (my first LUA attempt) works, but raises some basic questions:

  • I would have liked to find the length of the array Lights_exBedrooms using the LUA function n=table.getn(ArrayName). I could not make it work. Is this function not implemented in HC2 LUA?
  • General LUA documentation allows the construction for i,v in ipairs(ArrayName) do .. to cycle through an entire array. This does not seem to be implemented in HC2 LUA either?
  • The script runs without delay, including the do loop turning off all lights in the list. Line 18, however, turning device 251 back on at 20 %, is delayed by about 5 seconds. Why? 
  • The idea is to define the device list as a global variable to be used in other scenes. Will adding Lights_exBedrooms{} below line 5 "%% globals" achieve that?
  • More generally: How can something inserted between comment brackets --[[ and --]] in the header be significant to the LUA interpreter? 

 

  1. -- "Good night" script - turns off most lights
  2. --[[
  3. %% properties
  4. %% events
  5. %% globals
  6. --]]
  7. -- set toilet light to low before turning off (night light position, if manually switched on during night)
  8. fibaro:call(267,'setValue','20')
  9. -- Define vector of all non-bedroom light devices
  10. Lights_exBedrooms={306,304,311,305,291,286,271,366,276,
  11.   261,267,251,246,373,404,338,328,333}
  12. -- Turn off all lights except bedrooms
  13. n=18
  14. for i=1,n do
  15.   fibaro:call(Lights_exBedrooms,'turnOff')
  16.   end
  17. -- Special cases
  18. fibaro:call(251,'setValue', '20') -- Hallway at 20%

 

Edited by knuth
Formatting of script
Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
34 minutes ago, knuth said:

Below is a simple LUA script to turn off most lights at night. The script (my first LUA attempt) works, but raises some basic questions:

  • I would have liked to find the length of the array Lights_exBedrooms using the LUA function n=table.getn(ArrayName). I could not make it work. Is this function not implemented in HC2 LUA?

 

getn is not avialable in Lua 5.2 that scenes use. Try #ArrayName instead. Will not work for key-value tables or tables with "holes" in them.

 

Quote
  • General LUA documentation allows the construction for i,v in ipairs(ArrayName) do .. to cycle through an entire array. This does not seem to be implemented in HC2 LUA either?

 

Yes, ipairs works for arrays (not key-value tables)- can you give example where it doesn't work?

 

Quote
  • The script runs without delay, including the do loop turning off all lights in the list. Line 18, however, turning device 251 back on at 20 %, is delayed by about 5 seconds. Why? 
  • The idea is to define the device list as a global variable to be used in other scenes. Will adding Lights_exBedrooms{} below line 5 "%% globals" achieve that?
  • More generally: How can something inserted between comment brackets --[[ and --]] in the header be significant to the LUA interpreter? 

--[[ 

--]]

are multi-line comments in Lua. The HC2 uses that to allow people to do trigger declarations in the beginning of the scene inside a multi-line comment, so it won't affect the Lua code.

Please login or register to see this code.

 

Edited by jgab
  • Like 1
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks, impressively quick and relevant answers!

    To your comments and questions:

    Yes, device 267, a dimmer, is first set to 20 % (line 7) then switched off (line 10-11). The idea is that if someone switches it on during the night, it will go on at its last set value. Line 7 makes sure that the last set value is dimmed to 20 %, not to some disturbingly bright level. Next morning, a double-click restores it to full brightness.

    #ArrayName was unknown to me (my updated LUA reference book is on its way from Amazon :-) ). Syntax? n=#ArrayName ?

    Your modified do-loop using ipairs has a slightly different syntax (for _,id ...) than I tried. That's probably why mine did not work. I will try this the next time I am at the house (a holiday house).

     

    Any idea why my last line executes after a 5 second delay?

    And is adding Lights_exBedrooms{} below line 5 "%% globals"  the correct way to make that a global variable?

     

     

    Link to comment
    Share on other sites

    • 0
    1 hour ago, knuth said:

    Any idea why my last line executes after a 5 second delay?

    No, not really - but some people have issues with their Z-wave network getting congested. You could try to add a sleep in-between 

    Please login or register to see this code.

     

    1 hour ago, knuth said:

    And is adding Lights_exBedrooms{} below line 5 "%% globals"  the correct way to make that a global variable?

     

    No, a global fibaro variable needs to be created in the Panels interface in the HC2 Web GUI (can be done with Lua too, but a bit more involved)

    Fibaro globals only store strings, so if you want to store an array in it you need to create a JSON string out  of the array and store it in the variable. And when you read the value from the fibaro global you conversely need to decode it back into an Lua array.

    Please login or register to see this code.

    So, you typically have another scene that stores your values in fibaro globals.

    If you list the fibaro global under '%% globals', the scene will be triggered every time the global changes value. Probably not what you want in this case.

     

    • Like 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I would have expected congestion to occur during the do-loop, not after it has finished. I can try to play with small sleep intervals both within and after the loop, to see what happens.

     

    Global Fibaro variables are only strings? LUA standard, as far as I know, is that all variables are global, by default, unless declared as local? This is a rather severe limitation, don't you think, particularly since string operations are demanding on the CPU. But thank you for giving me a possible way to do this.

     

    So all information entered in the header section, whether property, event or global, will be used as triggers for the scene?  

     

    I desperately need a good overall reference to all the quirks of the HC2 implementation of LUA. All I have found up to now, are examples and some simple tutorials. Is there a total overview of all available and unavailable functions, syntax and limitations in the Fibaro HC2 version of LUA? 

    Link to comment
    Share on other sites

    • 0
    34 minutes ago, knuth said:

    I would have expected congestion to occur during the do-loop, not after it has finished. I can try to play with small sleep intervals both within and after the loop, to see what happens.

     

    Global Fibaro variables are only strings? LUA standard, as far as I know, is that all variables are global, by default, unless declared as local? This is a rather severe limitation, don't you think, particularly since string operations are demanding on the CPU. But thank you for giving me a possible way to do this.

     

    So all information entered in the header section, whether property, event or global, will be used as triggers for the scene?  

     

    I desperately need a good overall reference to all the quirks of the HC2 implementation of LUA. All I have found up to now, are examples and some simple tutorials. Is there a total overview of all available and unavailable functions, syntax and limitations in the Fibaro HC2 version of LUA? 

     

    Fibaro globals are not lua globals. Fibaro’s are for persistent storage of values between scene invocations. Like a key-value database where values are strings.

    lua globals can be used in scenes but don’t survive between scene invocations. 

     

    The lua in Hc2 scenes are lua 5.2 with some functions removed for security reasons. Then they have the fibaro:* library of functions to do HC2 specific stuff. ... and they use the header lua commment field to declare what triggers should start the scene. the HC2 parses the header of the scene file when you save the scene.

    Edited by jgab
    Link to comment
    Share on other sites

    • 0

    Hi @knuth ,

     

    Documentation on LUA API and REST API you can find and download here: 

    Please login or register to see this link.

     

     

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