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

UDP protocol read / transmit / loop / LUA optimisation


Deivydas

Question

Hello,

I'm new in LUA, but with some Experience in other programming languages.

I'm almost completing my first Quick App for Fibaro which allows to control GREE AC Units. To make completed this Quick App I need advices from other developers how to make changes in the code, how to organise the LUA code that it will use less RAM, CPU and other gateway resources.

 

The main Quick App Goals should be:

1. To Send commands to Gree AC Unit (turn on, off and etc). It's working right now

2. Get the statuses from Gree AC Unit after each 5 minutes and update it to dashboard, history logs. And at this point I have some issues:

2.1 How to make loop which will be trigered in each X minutes? The problem is with correct loop function how to make it work. How to get statuses is not a problem

I have made a loop, but need that someone review it. Maybe @lazer you? :)

 

3. How to make default ON, OFF relay switch work in QuickApp? At the moment I have 2 functions Quickapp:on / Quickapp:off, but these functions should be trigered via default relay buttons. Answer: To make working default TurnOn, TurnOff buttons, functions should be named: Quickapp:TurnOn, Quickapp:TurnOff

4. How should I organise the code? At the moment it's just 2 files: main file and aes library. It's doesn't look ok for me, because all the stuff is in the same file, this code is hard to read and understand

5. How can I have just 1 function with paramater, and this param should be passed when button is Click. For example I have function mode(which_mode) and there are 5 modes: auto, cool, dry, fan, heat. I would like to do that when button is clicked it trigger function mode ant pass the param for example - cool. Have no Idea How to Do

And at this moment that's all. After receiving advices, I'll keep this quickapp developing and when result will be fine, I'll share it with all of you

6. When I click On image, device doesnt turn on or off

7. How to save all actions to Fibaro History?

 

 

Please login or register to see this attachment.

Edited by Deivydas
Updated
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

(It would help if you share the lua code, not only the .fqa file)

 

 

 

Edited by SmartHomeEddy
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Ok, the Quick App for Gree AC Unit almost done.

     

    It will have this functions:

    • Turn On / off
    • Set temperature
    • Set mode
    • Set Swing type
    • Set Fan Speed
    • We read all mentioned actions statuses from device

     

    What is left to do:

    • Optimise the code
    • Find the best way to make  a loop, which repeat this function: readAllStatuses() after x minutes.

     

    I'm sharing with the LUA code, need the review from better than me, and I need the help to find a solution how can I make the best loop for data reading after x minutes.

     

     

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

     

    Please login or register to see this attachment.

    Edited by Deivydas
    Updated
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • On 10/26/2022 at 1:23 PM, SmartHomeEddy said:

    (It would help if you share the lua code, not only the .fqa file)

     

     

     

    Ok, I have upload the code :)

    Link to comment
    Share on other sites

    • 0
    On 10/26/2022 at 12:07 PM, Deivydas said:

    I have made a loop, but need that someone review it. Maybe @lazer you? :)

    Hi, sorry but I am fully busy right now, I can't spend some time reviewing your LUA code, I don't even have time to write my own code for several month.

     

    I hope you will manage to the get the desired result with your QuickApp.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I have made it working fine, there no methods which are write several times, for me it looks not bad, but I would like to make it the best :)

     

    Ok, for the first version, I can release it :)

    Link to comment
    Share on other sites

    • 0
    15 hours ago, Deivydas said:

    I have made it working fine, there no methods which are write several times, for me it looks not bad, but I would like to make it the best :)

     

    Ok, for the first version, I can release it :)

     

    So a couple of remarks.

     

    The loop with setTimeout is the way to do it. The only comment is that it's > 5min incl. the computations done, but that may not be a problem (so it's drifting).

     

    Don't put functions in QuickApp that you don't need to be accessible outside the QA.

    And don't stuff self.* with a lot of variables that could as easily be local variables declared in the beginning of the QA.

    Local variable access is much faster - but mostly it's because it makes no sense to have them in self. (If you have QuickAppChild objects it can make sense for scoping purpose)

    You will need access to 'self' e.g. the QuickApp instance in your local functions but there is a global variable 'quickApp' that is set to self after you exit :onInit so that could be used - or pass 'self' as an explicit parameter.

    Ex.

    Please login or register to see this code.

     

    or, using self as a parameter

     

    Please login or register to see this code.

     

    alternatively, create a local variable 'self' with scope of the whole QA.

    Please login or register to see this code.

     

    You just need to be careful to forward declare the local functions if they are referenced before they are declared in the code.

    However, if you declare them before use you don't need to . ex.

    Please login or register to see this code.

     

    All the local variables you declare in the beginning of the QA.

    Between QA files you need to use global Lua variables - alternatively put them in QuickApp.* or fibaro.*

     

    Lua lacks case switch, and the solution is to use table lookups - as the tables in Lua are very "flexible". 

    The "updateDashboard" functions is an example of endless if-then-else tests that mostly can be replaced by a table.

    Ex.

    Please login or register to see this code.

     

     

    Your sPack 

    • Thanks 1
    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...