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

Code review request - What could be done better, more appropriate?


JcBorgs

Question

Please login or register to see this spoiler.

 

Edited by JcBorgs
Minor updates
Link to comment
Share on other sites

Recommended Posts

  • 0
20 minutes ago, JcBorgs said:

@jgab I made an alternative solution to ensure to save the time in 2 digit format:

  startHour = string.format("%0.2i", tostring((14+cheapest.block) % 24))

  stopHour = string.format("%0.2i", tostring((14+cheapest.block+04) % 24))

 

Yes, that can be nicer for logging.

 

Test run of the logic (block starts at 0)

Please login or register to see this code.

Please login or register to see this code.

 

20 minutes ago, JcBorgs said:

@jgab I made an alternative solution to ensure to save the time in 2 digit format:

  startHour = string.format("%0.2i", tostring((14+cheapest.block) % 24))

  stopHour = string.format("%0.2i", tostring((14+cheapest.block+04) % 24))

 

 

The 14 is the start hour for the hour prices?

I used 20 as the starting hour.

Because we started at 20, the first stopHour was 24 (or 0), which is the same as the block number (0)

 

block 0 will give you startHour 14 and stopHour 18 with 14 as the base

Edited by jgab
Link to comment
Share on other sites

  • 0

Can't you start and stop charging multiple times and just choose the 4 hours with lowest price during the 10 hours?

or is it better to charge continuously?

Don't need that tostring (I'm actually surprised it works)

 

Please login or register to see this code.

still think it should be 20?

Link to comment
Share on other sites

  • 0
  • Inquirer
  • 29 minutes ago, jgab said:

    Can't you start and stop charging multiple times and just choose the 4 hours with lowest price during the 10 hours?

    or is it better to charge continuously?

    Don't need that tostring (I'm actually surprised it works)

     

    Please login or register to see this code.

    still think it should be 20?

    @jgabYes it should be 20 with the intention to find the cheapest 4 hour block during evening(20)->morning(06).

     

    I just had 14 put in during my tests..

     

    The format for stopHour you are using are working just perfect, as long as the calculations are done at 20.

     

    I am thinking of allowing the calc time to be a user selected time. So if choosen to instead start at 19 or earlier/later your format won't work anymore. That is why I changed the stopHour line to be:

     stopHour = string.format("%02i",(20+cheapest.block+4) % 24)

     

    That would give the possibility for flexibility in calcTime.

     

     

    Link to comment
    Share on other sites

    • 0

    Ok,

    Please login or register to see this code.

     

    cheapestTime(10,20,4)

    Edited by jgab
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 3 minutes ago, jgab said:

    Ok,

    Please login or register to see this code.

    cheapestTime(10,20,4)

    That look just perfect! Smart way of doing it!

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 45 minutes ago, jgab said:

    Please login or register to see this code.

     

    @jgab

    Had to make one change to make the logic work... Not sure if it was a mistake on your side or if I have misunderstood something.

     

    Changed this line:

    for i=0,blockStart-blockLen do

     

    to this:

    for i=0,timeSlots-blockLen do

     

    Otherwise I didn't see any usage of providing the Function with the timeSlots as it wasn't used in the code. And the change I made matched the logic you had in the old version.

     

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

    • 0
  • Inquirer
  • So decided to take a different path with the QA I starter to build and instead include the Smart Car Charger functions in the Charge Amps HALO QA that I created before (with a huge help of @jgab

     

    So combined the two codes together in one QA and have started the “Cleanup work”. Basic functionality are still there and everything seem to be working as it should both for Device control and the Smart Car Charger functions.

     

    Still a lot of things left on the ToDo list….. but slowly getting there

     

    The UI of the QA today:

    Please login or register to see this spoiler.

     

    Edited by JcBorgs
    Link to comment
    Share on other sites

    • 0

    Fine progress.

     

    Most of the quickapp function look exactly the same. Can’t you use only one quickapp function, maybe with a variable if you wan’t it to do something special? 

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @SmartHomeEddy

    Good input and one area that I will focus on, to reduce the amount of functions & code lines. Did an first stab on with the LED Ring part where each QA Button had it own function with a lot of code, now the button function only just call a common function with a variable included. 

     

    Best option would have been to call the same function directly from the button, but cant figure out how to include a variable in the onReleased part in the button configuration wanted to do a ‘function(variable)’ but it does not allow that, only allows for ‘function’.

    Link to comment
    Share on other sites

    • 0

    A useful way if you have buttons that do similar things, is to use the button ID (elementName of the onRealeased argument) as a map to what it should do. Ex.

    Assume that we have 3 buttons:

    ID="A", Label="Set A", onReleased="buttonSet"

    ID="B", Label="Set B", onReleased="buttonSet"

    ID="C", Label="Set C", onReleased="buttonSet"

    They all have the same onReleased so they will call the same function

    Please login or register to see this code.

    Of course you don't need to call a function (somethingComplicated...) but can instead do it directly in the QuickApp:button function.

    Edited by jgab
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 5 hours ago, jgab said:

    A useful way if you have buttons that do similar things, is to use the button ID (elementName of the onRealeased argument) as a map to what it should do. Ex.

    Assume that we have 3 buttons:

    ID="A", Label="Set A", onReleased="buttonSet"

    ID="B", Label="Set B", onReleased="buttonSet"

    ID="C", Label="Set C", onReleased="buttonSet"

    They all have the same onReleased so they will call the same function

    Please login or register to see this code.

    Of course you don't need to call a function (somethingComplicated...) but can instead do it directly in the QuickApp:button function.

    @jgab

    Have tested this a bit but couldnt  get it to work…. and got a lot of errors until I added some extra { }, my HC3 didn’t like the ” = args = ”

     

    Have two buttons:

    ID=”lbl_LightOn”, Label=”On”, onRelease=”Light”

    ID=”lbl_LightOFF”, Label=”OFF”, onRelease=”Light”

     

    Please login or register to see this code.

     

    But the log for this only shows:

    [05.07.2022] [11:16:03] [DEBUG] [QUICKAPP978]: A is : 

    [05.07.2022] [11:16:03] [DEBUG] [QUICKAPP978]: B is :

     

    Edited by JcBorgs
    Link to comment
    Share on other sites

    • 0

    Yes, your'e right. Too early in the morning...

    Add curly braces and add btnMap[ev.elementName].args

     

    Please login or register to see this code.

     

    You could also add a function to use, then you can have a generic QuickApp:button() function for all buttons. Make sure you define the btnMap table after you defined the functions.

     

    Please login or register to see this code.

     

    Alternatively
     

    Please login or register to see this code.

     

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

    • 0

    Another thing in your new code is the use of the lua global "QA".

    It was introduced in the previous example so it would be easy to use the QA functions from with local lua functions (non QuickApp:functions...)

    However, within QuickApp:functions I advice you to use self: to access functions. When reading the code it tells us that it's a method belongs to the current class and makes it easier to follow. Also, in the future if you want to move some stuff to QuickApp child objects you can't rely on a global QA variable.

    • Thanks 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 19 hours ago, jgab said:

    Another thing in your new code is the use of the lua global "QA".

    It was introduced in the previous example so it would be easy to use the QA functions from with local lua functions (non QuickApp:functions...)

    However, within QuickApp:functions I advice you to use self: to access functions. When reading the code it tells us that it's a method belongs to the current class and makes it easier to follow. Also, in the future if you want to move some stuff to QuickApp child objects you can't rely on a global QA variable.

    @jgab

    Yes I realized that this was not good, but seen some strange things when trying to correct this.. Changing back from QA:function to self:function have caused issues where to code simply dont execute the command self:function() and need to be changed back to QA:function() to work properly.

     

     

    In regards to the code for handling “buttons” I made the below out of your suggestions and it works just perfect! Done the same for more or less all buttons in the QA and reduced the amount of code rather good. But one question…. If I now in the code want to call a function to in the example below turn on a light, how do I do that in code? It works just fine through the press of the buttons, but how would I call the function Light(ev) and make it turn on the light (parameter ‘a’ should be =true).

     

     

    Please login or register to see this code.

    Edited by JcBorgs
    Link to comment
    Share on other sites

    • 0
    On 7/6/2022 at 10:56 AM, JcBorgs said:

    In regards to the code for handling “buttons” I made the below out of your suggestions and it works just perfect! Done the same for more or less all buttons in the QA and reduced the amount of code rather good. But one question…. If I now in the code want to call a function to in the example below turn on a light, how do I do that in code? It works just fine through the press of the buttons, but how would I call the function Light(ev) and make it turn on the light (parameter ‘a’ should be =true).

    Please login or register to see this code.

     

    Btw, if you have sliders you need to add the slider value to the args before calling the fun.

    Link to comment
    Share on other sites

    • 0

    Just commented in another discussion about the QA for the ChargeAmp. This discussion is though exactly what I am after, combining Tibber and ChargeAmp.  Would we very nice to get to see the QA/fqa-file.  Either published via MarketPlace, or via other channel, also possible via commercial channel of course.

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