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


HC3 QuickApps coding - tips and tricks


jgab

Recommended Posts

  • Topic Author
  • 42 minutes ago, SmartHomeEddy said:

    After

    Please login or register to see this code.

     

     

    Code that works ...works, but...

    Just a general coding reflection - feel free to ignore.

    You have a function that is named setGlobalVariable that actually deletes the variable if a global variable 'setglobalvar' is false... ?

    It may look like a clever Swiss-army-knife function today but in a year you will wonder "why do I call 'setGlobalVariable to delete variables?" and chasing bugs because the global 'setglobalvar' happens to have the wrong value...

    I would split it up in two functions - the first similar to my example but with the extra json encode (and it's a bit more efficient as it doesn't need to call api.get for every setGlobalVariable call as it cache that it has seen the variable) and then a separate deleteGlobal function that does just that.

    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    3 hours ago, jgab said:

    you will wonder "why do I call 'setGlobalVariable to delete variables?"

     

    ?

     

    The code grew upon testing ? If someone decides not to use the variables anymore, then I have to delete them. 
     

    3 hours ago, jgab said:

    I would split it up in two functions

    done

     

    3 hours ago, jgab said:

    the first similar to my example but with the extra json encode

    On my ToDo list, first I try to understand that code

     

     

     

    Edited by SmartHomeEddy
    Link to comment
    Share on other sites

  • Topic Author
  • Just a reflection on speed of different ways to store values - local Lua variables, global Lua variables and fibaro globals (QA quickVars seems to be similar to GVs)

    Please login or register to see this spoiler.

     

    The timings I get are

    Please login or register to see this code.

    The comparison between Lua variables and GVs are not exactly comparable but it gives some rough proportions anyway.(It would be even worse (>2x) if we incremented a fibaro global in the loop)

    First note that Lua globals are much slower than Lua locals. Note that 'fibaro' in ex. fibaro.call is a global. Locals are good - and because they are scoped there are less risk that you have unintentional effect due to globals overwriting each other...

    Fibaro globals are ~4000x slower than a Lua global (getting a bit faster than setting). Implication of that is to keep values in Lua variables and treat fibaro globals as a storage/persistence and don't ex. access/set them in loops if you can instead read before the loop and update afterwards - or at least update after the loop.

     

    • Like 1
    Link to comment
    Share on other sites

  • Topic Author
  • 13 minutes ago, SmartHomeEddy said:

     

    ?

     

    The code grew upon testing ? If someone decides not to use the variables anymore, then I have to delete them. 

    No worries - I been overpowered by code many times - just need to find the strength to fight back...:-) 

     

    Link to comment
    Share on other sites

    Jan, persistent storage if we talking about Fibaro is SQLite. Idk why global params is too slowly as you said 40000x

    Link to comment
    Share on other sites

  • Topic Author
  • 1 minute ago, 10der said:

    Jan, persistent storage if we talking about Fibaro is SQLite. Idk why global params is too slowly as you said 40000x

    Well, it's rather that Lua is very fast.

    fibaro API is going through a rest call (It may be internally optimised, I don't know) but in any case there are a lot of overhead. compared to pure Lua.

    Edited by jgab
    Link to comment
    Share on other sites

    - hey boy, what are you doing here?
    - ....

    - boy, what is you name?

    -....

    - are you slowpoke?
    - my name is John 

    - where are you living?
    - no! I am not a slowpoke!!
     

    sometime development with Fibaro UI look like this meme.

     

    Link to comment
    Share on other sites

    2 hours ago, 10der said:

    Jan, persistent storage if we talking about Fibaro is SQLite. Idk why global params is too slowly as you said 40000x

     

    rough 4000 not 40000.

     

    2 hours ago, jgab said:

    fibaro API is going through a rest call (It may be internally optimised, I don't know) but in any case there are a lot of overhead. compared to pure Lua.

     

    indeed, all thr pcall, asserts, invokeScenes does not take lot of time, the rest call does however. 

     

    Now imagine all these smart scenes looping while rest called global variable changed :P 

    Link to comment
    Share on other sites

  • Topic Author
  • 59 minutes ago, tinman said:

    rough 4000 not 40000.

    indeed, all thr pcall, asserts, invokeScenes does not take lot of time, the rest call does however. 

    Now imagine all these smart scenes looping while rest called global variable changed :P 

    Yes, 
    Just to put things in perspective, in absolute time a fibaro.getGlobalValue takes ~1.7ms - so one can read the value 600 times in a second.

    Accessing Lua globals on the other hand takes microseconds.

     

    Link to comment
    Share on other sites

    2 minutes ago, jgab said:

    Yes, 
    Just to put things in perspective, in absolute time a fibaro.getGlobalValue takes ~1.7ms - so one can read the value 600 times in a second.

    Accessing Lua globals on the other hand takes microseconds.

     

    Jan remember that getGlobalValue also return for you the tracking value lastChanged. a native  Lua globals not.

     

    Edited by 10der
    Link to comment
    Share on other sites

    Does anybody know how to setup in QA that device is not connected?

    Please login or register to see this image.

    /monthly_2020_08/image.png.0f5aae7b22de273f4d66e77782419158.png" />

    Link to comment
    Share on other sites

    31 minutes ago, petrkl12 said:

    Does anybody know how to setup in QA that device is not connected?

    Please login or register to see this link.

    yep

     

    example (SonOff device)

     

    Please login or register to see this code.

    pay attention to 

     error = function(err) self:updateProperty("dead", true) end,

    Please login or register to see this image.

    /monthly_2020_08/image.png.0fe0fed60fed5b4b05141845807e8e57.png" />

    image.png.2fa93d4bcf6fedef7bc44600b3d689f8.png

    Edited by 10der
    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    On 2/22/2020 at 7:11 PM, jgab said:

    Sharing devices between the HC2 and the HC3

    Yet another hack.

     

    Here is a HC2 scene - You need to call it "HC3Proxy" and set the variables for IP, username, and pwd for you HC3 in the code.

    Also set "Max running instances" to 10.

     

    Please login or register to see this spoiler.

     

    Link to comment
    Share on other sites

    7 minutes ago, Rover said:

    Hi @jgab,

    I did install the scene on HC2 and the QA on HC3.

    Blockscene on HC3 knowns the HC2 devices, but test Toggle HC2 device (switch) from HC3 test blockscene delivers the next on HC3Proxy on HC2 (and no toggle of the HC@ device):

     

    Please help me to get it done.

    Please login or register to see this spoiler.

     

     

    Edited by Rover
    Link to comment
    Share on other sites

    Hi @jgab,

    I set up a topic with a QD question, but I realize that I should have asked that question here.

    Would you please look at my question?

    I would like to make a QA Child on an existing fibaro double switch.

     

    thank you in advance..

    • Like 1
    Link to comment
    Share on other sites

  • Topic Author
  • On 8/30/2020 at 4:59 PM, Rover said:

    Hi @jgab,

    I did install the scene on HC2 and the QA on HC3.

    Blockscene on HC3 knowns the HC2 devices, but test Toggle HC2 device (switch) from HC3 test blockscene delivers the next on HC3Proxy on HC2 (and no toggle of the HC@ device):

     

    Sorry, I missed this post.

    Have you done all the configurations?

    Have you set the HC3 IP/user/password variables in the HC2 scene?

    Have you set the HC2 IP/user/password variables in the HC3 QA? (and setup the table with the devices)

     

    I don't fully understand this sentence

    "...but test Toggle HC2 device (switch) from HC3 test blockscene delivers the next on HC3Proxy on HC2 (and no toggle of the HC@ device)"

    Can you elaborate?

     

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, NLWaard said:

    Hi @jgab,

    I set up a topic with a QD question, but I realize that I should have asked that question here.

    Would you please look at my question?

    I would like to make a QA Child on an existing fibaro double switch.

    thank you in advance..

     

    Just to recap:

    You have a double switch today that is visible as two binary switches in the UI.

    One of those switches control a light bulb that can dim depending on the number of  clicks.

    Correct?

    -------------

    You can't "attach" a childDevice to an existing z-wave device. A child device needs a QA that runs the code and creates the child devices. We can't add Lua code to z-wave device.

    Why would you like it to be a child device? You want them to group together in the Web UI? (child devices are listed directly below the "mother QA"  that creates them)

    What you maybe could do is to create a regular multilevelSwitch QA (on, off, dim level) that takes the input from the binary switch (on, off) and add a slider for the "dim" value. Then hide the z-wave binary switch. Can you control the dim-value by sending rapid turnOn/turnOff commands to the bulb?

     

     

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

    2 minutes ago, jgab said:

    create a regular multilevelSwitch QA

     

    3 minutes ago, jgab said:

    Then hide the z-wave binary switch.

    .... and the next question to @jgab will be 

    Jan I can see nothing ;) NO QA and NO physical device in mobile application ;) 

     

    • Like 1
    Link to comment
    Share on other sites

  • Topic Author
  • 5 minutes ago, 10der said:

     

    .... and the next question to @jgab will be 

    Jan I can see nothing ;) NO QA and NO physical device in mobile application ;) 

    ...but Fibaro will fix that soon - just hold your breath ? ...and when you wake up it will work :-) 

    Edited by jgab
    • Like 3
    • 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
    Reply to this topic...

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