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

Lua scripting 101 ... advice


AutoFrank
 Share

Question

Hi,

I'm new to Lua and I have read what I could find within the forum discussions. I'm trying to master the basics before I move on and wanted to check from one of the experts that my simple scene is structured as it should be.

I have a HC2 and am running the latest FW 4.070

 

My scene below sets a global variable 'TOD_LightMode' to night, dusk or day depending on the light level from an outside sensor. It runs the scene and then sleeps for 15 mins and repeats. It seems to work okay and I have checked the global variable is available to other scenes and I have used it to trigger other activity.

 

The few questions I have and hopefully somebody can provide advice...

1) do I have all the variables declared correctly ? (I have seen some included in the header in some examples but am not sure what to include there)

2) Is my autostart code correct ?

3) I have a lot of scenes in mind that would be "waiting" scenes. Many people use the while true do or while (someglobalvariable == "somevalue") method. Is there a limitation as to how many of these types of scenes can be running at the same time before it starts to use all the CPU/mem resources ?

4) Is there a simple error checking or logging method that I could/should be using with my scripts.

5) Any other suggestions that would be better practice or make my code more efficient

 

Any help or suggestions very welcome ?

Thanks

-F

 

Code as follows...

Please login or register to see this code.

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Made a mistake where there was a missing 'end' at the bottom. Here is a revision:

Please login or register to see this code.

Link to comment
Share on other sites

  • 0

Hi,

 

I am pretty new to LUA scripting too and learning from others in this forum. I have started to try and standardize scripts so that there is sort of a baseline template. Maybe one day, all the scripts collection in this forum will have some sort of consistency in baseline structure so new ones who joins can have an easier learning curve to pick up the knowledge.

Check this out ->

Please login or register to see this link.

 

I will have some thought later when I get home and share with you on those questions you asked. One thing that come across now is how to share codes in forum. Using the "<>" button on the top of reply editor dialogbox allow to paste code in proper manner and even adding the starting line number, and this makes it easier for others to look at your script/code.

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks chaicka for the help

     

    I amended the original post to use the <> as suggested 

    Link to comment
    Share on other sites

    • 0

    Line 34's "else" seems redundant to me. Otherwise seems decent. Line 5's "TOD_LightMode" is not necessary since you are not using the global variable as a trigger.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks

    What about the variable declaration in the header

    I'm using OutsideLuxSensor = 217 and have seen some people put the ID and and whats been referenced in the code so it would appear like 

    Please login or register to see this code.

    I'm not sure why its added and if its necessary

    Link to comment
    Share on other sites

    • 0

    I have not tested this out. Think it looks more structured and easier to troubleshoot or comprehend.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks for that 

    I'll pick my way through it this evening but at first glance it looks like a reusable format that's well structured

     

    I assume that the "Validate Number of Scene Instance" is to make sure that no more than one instance is running at any point in time ?

     

    Any thought on my query about running a good many of these type of 'while do' scripts/scenes

    Does the HC2 have the limit to the number it can run (if we were to take these basic scenes as a sample)

    Link to comment
    Share on other sites

    • 0

    Thanks for that 

    I'll pick my way through it this evening but at first glance it looks like a reusable format that's well structured

     

    I assume that the "Validate Number of Scene Instance" is to make sure that no more than one instance is running at any point in time ?

     

    Any thought on my query about running a good many of these type of 'while do' scripts/scenes

    Does the HC2 have the limit to the number it can run (if we were to take these basic scenes as a sample)

     

    I am still new to LUA scripting. I know there are others with 200+ scenes and HC2 has no issue. Don't worry about how many scenes HC2 can support cause it is an Intel board after all. Worry about proper scripting so that there is no looping or loose-end conditions cause usually that is what causes HC2 instability issues.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Good idea and thanks for all the advice

    I only have about 10 elscenes and will re do them in your format before adding more

    -F

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