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

Anyone want to help me sort my Lua out?


shaunfrost
 Share

Question

I have written an enhancement scripts for boosting the heating in certain rooms based on presence. I already have a zwave boiler controller, and LC-13 radiators with a scripted running base heating scene.. The purpose of this is for certain rooms that meet the following criteria:-

1. Don't need to be heated to comfort level all the time in the heating panel

2. Presence is detected, and the user tends to be in the room for a good amount of time.

 

2 initial rooms for me are Study and Living Room

 

I wrote it based on tabulated data, so I could just add additional information into the tables for inclusion, but my lua was "stretched" and there are a lot of loops in loops... it runs fine, but I wondered if anyone wanted to demonstrate how it SHOULD be written :)

 

or if you want to use it, and you have radiator set points you would like to boost, feel free...

 

 

 

Please login or register to see this attachment.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Thanks for sharing. The main part of the code is a big, nested, if-then-else structure. This is something I find hard to read. You can improve it by introducing functions. As a rule of thumb, for readability: "the 'if' and 'end' should fit on your screen" or even more stringent "no more than 10 lines between 'if' and 'end'".

 

I am reluctant to try this kind of script for fun, on my production HC (which is also my test HC... )because it takes me a lot of time to set up and it modifies my "HeatingTable", which doesn't have a simple undo. And to run that on a simulator seems difficult (lots of things to simulate).

Link to comment
Share on other sites

  • 0
  • Inquirer
  • 1 hour ago, petergebruers said:

    Thanks for sharing. The main part of the code is a big, nested, if-then-else structure. This is something I find hard to read. You can improve it by introducing functions. As a rule of thumb, for readability: "the 'if' and 'end' should fit on your screen" or even more stringent "no more than 10 lines between 'if' and 'end'".

     

    I am reluctant to try this kind of script for fun, on my production HC (which is also my test HC... )because it takes me a lot of time to set up and it modifies my "HeatingTable", which doesn't have a simple undo. And to run that on a simulator seems difficult (lots of things to simulate).

     

    Thanks for that, I am really trying to understand how to enter and exit into and out of functions whilst retaining my "thread". there are quite a lot of arrays that are nested, and I agree it looks wrong, of course it works great, but a script like this shouldn't take 40 seconds to execute!

     

    I might start small and look at it myself, its a learning curve.

     

    Link to comment
    Share on other sites

    • 0
    14 hours ago, shaunfrost said:

     

    Thanks for that, I am really trying to understand how to enter and exit into and out of functions whilst retaining my "thread". there are quite a lot of arrays that are nested, and I agree it looks wrong, of course it works great, but a script like this shouldn't take 40 seconds to execute!

     

    I might start small and look at it myself, its a learning curve.

     

     

    Oh, but I don't want to suggest your code is wrong, it's simply difficult for me to "read".

     

    I'll try to modify the code a bit, to reduce the nesting, by introducing some functions. Maybe today ;-)

     

    It could help me a bit, but it's not necessary, if you send me your hometable and heatingtable via PM (output of fibaro:debug(fibaro:getGlobalValue("HomeTable"))).

     

    BTW did you just say that the code takes 40 seconds to execute on your HC?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 31 minutes ago, petergebruers said:

    It could help me a bit, but it's not necessary, if you send me your hometable and heatingtable via PM (output of fibaro:debug(fibaro:getGlobalValue("HomeTable"))).

     

    BTW did you just say that the code takes 40 seconds to execute on your HC?

     

    This has been an interesting experiment for me.. I think I have altered it into as best functions as I can without rewriting completely from scratch, you may be able to read it a little more now...

     

    the 40 seconds was the reason I was concerned that my nested for loops were a concern, then I realised I put a 20second sleep on each iteration!! I have reduced that to 10 seconds (this is required to allow the TTS to speak on the Sonos before a second command is sent to it, voiceRss lag)

     

    The HeatingTable is poorly named, it is essentially a Json Table containing data on when the Home Automation last "announced " it was enhancing the room, as I run the script from a main sheduling scene every 5 mins, this table is used to make sure it only announces every 3 hours minimum, you can see the HeatingTable variables being built in the last function (changetimestamp)

     

    thanks for your guidance.

     

    Please login or register to see this attachment.

    Edited by shaunfrost
    wrong attachment
    Link to comment
    Share on other sites

    • 0

    Ah, you have been busy :-)

    Yes, I saw the sleep() so that's solved ;-)

    You attached your new script as RTF, could you post it as plain text with lua extension instead, please? I recommend "ZeroBrane Studio" as an offline editor, there are a few topics dedicated to this.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 7 minutes ago, petergebruers said:

    Ah, you have been busy :-)

    Yes, I saw the sleep() so that's solved ;-)

    You attached your new script as RTF, could you post it as plain text with lua extension instead, please? I recommend "ZeroBrane Studio" as an offline editor, there are a few topics dedicated to this.

     

    added to post above... lua.

    thanks

    Link to comment
    Share on other sites

    • 0

    Thanks. That massively improved readability! Really! I had a quick play with it, I added your devices to my simulator and I was now able to run your code. Tomorrow I'll have a closer look at it.

     

    One silly trick to improve readability: in Zerobrane use CTRL-I to automatically indent the code. The HC web editor can format your code too: do CTRL-A to select all then SHIFT-TAB to auto indent.

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