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
  • 15 hours ago, ppeterr said:

    Could it  be, that after removing the quickapp. There remain some parts

    of code in other quickapp on my system ?

     

    [26.02.2022] [17:47:00] [ERROR] [QUICKAPP225]: QuickApp crashed[26.02.2022] [17:47:00] [ERROR] [QUICKAPP225]: ./quickApp.lua:239: module 'include.MEMORYWATCH' not found:

     

     

    Maybe i forgot to remove the probes. Not sure. 

    MW adds an extra file to QAs named "MEMORYWATCH". The file only adds an extra QuickApp function used get info from the QAs. The QuickApp function is "passive" when not called - no setTimeout or loops are started, 

    So, even if the file would be left in some QAs it should not have any impact (and can be removed by hand).

     

    Your error message I can't figure out what it mean. Does QA 225 still contain the MEMORYWATCH file? 

     

    You could reinstall the memorywatch QA you could run the "remove" function.

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab Not sure where to look, i checked the QA225 LUA, and there is no

    reference to any file. I will run the remove function again :)

     

    [edit] That worked 

    Edited by ppeterr
    Link to comment
    Share on other sites

    On 2/27/2022 at 8:26 AM, jgab said:

    MW adds an extra file to QAs named "MEMORYWATCH". The file only adds an extra QuickApp function used get info from the QAs. The QuickApp function is "passive" when not called - no setTimeout or loops are started, 

    So, even if the file would be left in some QAs it should not have any impact (and can be removed by hand).

     

    Your error message I can't figure out what it mean. Does QA 225 still contain the MEMORYWATCH file? 

     

    You could reinstall the memorywatch QA you could run the "remove" function.

    I experience the same issues

    I have 

    Please login or register to see this code.

     

    QA 1077 is SONOS Zone controller by kirkoff

    I dont see any file in QAs named "MEMORYWATCH"

    I tried "remove QA Probes" - and delete the memorywatch QA

     

    Please login or register to see this attachment.

     

    Link to comment
    Share on other sites

  • Topic Author
  • @ppeterr Was it the SONOS Zone controller for you too?

    Link to comment
    Share on other sites

    @jgab

    you should use some trick to minimize potentional error during deleting

    1. save empty source code of deleted module (it should restart QA)

    2. than to delete empty module

    • Thanks 1
    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, petrkl12 said:

    @jgab

    you should use some trick to minimize potentional error during deleting

    1. save empty source code of deleted module (it should restart QA)

    2. than to delete empty module

    Yes, I suspect that there must be some bug with deleting files. A delete causes a restart, but it may be safer to clear the content in a step before...

    Link to comment
    Share on other sites

    • 5 weeks later...
    On 2/12/2020 at 7:34 AM, jgab said:

    Ok, back to code.

     

    The QD has support for UI buttons, labels, and sliders like the old VD (no improvements here).

    We declared handler in the QD, so if we have a button with id 'button1' it will call 

    function QuickApp:button1Clicked() .... end

    when we click on the button. 

    Disclaimer: Still some bugs on the HC3 that makes these UI's not working on mobile apps, and sometimes flaky even in the web gui... but that will hopefully be fixed.

     

    So, on to an example:

    Assume, we make a simple QD to work as a code lock - here we make the QD to be of type ''door sensor' (we chose the type when we create the QuickApp device).

    It could look like this:

    Please login or register to see this code.

    We lay out the controls like this

    Please login or register to see this link.

    and name the handlers 'button0' etc.

    This device will then act as a code lock, that will get 'unsecured' when the right code is typed and auto-secure again after 4s.

    Other scenes and devices (or block scenes) could trigger on this device being unsecured and do something - like disable an alarm.

    FQA:

    Please login or register to see this attachment.

    Next post, a bit more advanced...

    Hi @jgab

     

    I'm new to quickapps.

    I tried your doorlock QA but it doens't work.

    I get 

    UI callback for element: buttonx not found 

    when i press a button.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 12 minutes ago, Lode said:

    Hi @jgab

     

    I'm new to quickapps.

    I tried your doorlock QA but it doens't work.

    I get 

    UI callback for element: buttonx not found 

    when i press a button.

     

    Hi, this was really early code and the UI defs changed a bit. I have uploaded a new version that works in the original post

     

    Link to comment
    Share on other sites

    3 hours ago, jgab said:

    Hi, this was really early code and the UI defs changed a bit. I have uploaded a new version that works in the original post

     

    Hi @jgab

     

    Thank you so much. Got it working now but i really have to study that code before i understand it.

    I learn the most from good examples but can't find them that much.

    I have my HC3 (had HC2 for 7 years) since yesterday so a lot to discover but until now i like it a lot.

    Anyway, thanks again !!?

    Link to comment
    Share on other sites

    • 3 weeks later...
  • Topic Author
  • I just looked at an old QA I wrote and it had the lines

    Please login or register to see this code.

    ...and it took me a couple of seconds to understand what I was doing and why - and the thinking behind the code was actually not that bad.

    It creates a local variable 'self' that we can use in non-QuickApp functions. Note the use of a function to set 'self' as 'self' is already a local variable in :onInit.

    Please login or register to see this code.

    For QAs (without children or other classes) this makes it easy write most of the functions as local lua functions and avoid to unnecessarily expose them as external QickApp functions.

     

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

    17 hours ago, jgab said:

    and it took me a couple of seconds to understand what I was doing and why

    Unless the casual reader has had a course on lua, or has read the lua book, it will probably seem like voodoo. To be honest, I am only going to add to the confusion, but I think I am here to motivate people to learn the difference between a local variable, a global variable (which is the default) and an upvalue which is "a local variable in an outer scope".

     

    The whole quick app file acts an "outer scope" so it seems, as if a global variable "self" exists everywhere - unless it is hidden in a (sub) scope. 

     

    17 hours ago, jgab said:

    Note the use of a function to set 'self' as 'self' is already a local variable in :onInit.

    Errrm is that a trick question on lua :)

     

    I would say... The lua philosophy is that variables with the same name are hidden indeed and are inaccessibly by name, there is no prefix or suffix to get to a global or an upvalue if the local scopa already has a variable with that name. Your function is a "trick" to get different names.

     

    But I think you could go via the global or local environment instead of using a function. EDIT: it would work using a "normal" lua interpreter, but not sure about HCx, Fibaro has denied access to some parts of lua to increase security.

     

    Just saying, I appreciate your post - it made my brain work (and ache a bit at the same time).

     

    Lua is such a neat, concise, orthogonal language... I love it.

     

    If you think this is all WEIRD... For those who love PYTHON.... I only recently found out...

     

    thistuple = ("apple")

     

    That is a tuple, a list of items, right? No it is not! it is a string! What makes a tuple is the comma separator... So to make a tuple with one item in it the correct syntax is...

     

    thistuple = ("apple",)

     

    Doh

     

    Edited by petergebruers
    Link to comment
    Share on other sites

  • Topic Author
  • On 4/25/2022 at 3:52 PM, petergebruers said:

    Unless the casual reader has had a course on lua, or has read the lua book, it will probably seem like voodoo. To be honest, I am only going to add to the confusion, but I think I am here to motivate people to learn the difference between a local variable, a global variable (which is the default) and an upvalue which is "a local variable in an outer scope".

     

    The whole quick app file acts an "outer scope" so it seems, as if a global variable "self" exists everywhere - unless it is hidden in a (sub) scope. 

     

    Errrm is that a trick question on lua :)

    Well, some people don't understand where the 'self' variable inside a QuickApp defined function comes from. That it's just an extra parameter prepended by the Lua "preprocessor" to the methods parameter list is not obvious.

    I.e.

    Please login or register to see this code.

    is equivalent to writing 

    Please login or register to see this code.

    On 4/25/2022 at 3:52 PM, petergebruers said:

     

    I would say... The lua philosophy is that variables with the same name are hidden indeed and are inaccessibly by name, there is no prefix or suffix to get to a global or an upvalue if the local scopa already has a variable with that name. Your function is a "trick" to get different names.

    Well, it's not a trick if you know how it's done ;-) 

    No, but more seriously, it's easy to think about the scope as a single hierarchy - but it becomes much more interesting with closures that we get with all Lua functions that have "free" local variables.

    In a way our setSelf function has access to another scope then the scope from where it's called - which in itself is a powerful concept if used properly.

     

    On 4/25/2022 at 3:52 PM, petergebruers said:

     

    But I think you could go via the global or local environment instead of using a function. EDIT: it would work using a "normal" lua interpreter, but not sure about HCx, Fibaro has denied access to some parts of lua to increase security.

    It's really ugly to access and set local variables - one need to use the debug library (

    Please login or register to see this link.

    ) that is blocked on the HC3 and one needs to search through the environment for the name. Not for the faint hearted...

     

     

    On 4/25/2022 at 3:52 PM, petergebruers said:

    If you think this is all WEIRD... For those who love PYTHON.... I only recently found out...

    ...this thread could easily diverge into tales of strange programming languages... but I'm not taking that bait :-) For the Python thing, I think the designer actually was happy when coming up with that syntax for a single item tuple... and a 0-tuple is ()....

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

    10 hours ago, jgab said:

    Well, some people don't understand where the 'self' variable inside a QuickApp defined function comes from.

    Excellent point.

     

    I think it sometimes is the "syntactic sugar" that throws off new users, but makes the code more readable in the end.

     

    This is one of those idiosyncrasies I use but it confuses new users:

     

    Please login or register to see this code.

     

    There is a lot going on there. You have to know how "or" returns valua, understand "short circuiting" in some cases, and understand that nil has its own type that cannot be added to a string...

     

    11 hours ago, jgab said:

    but it becomes much more interesting with closures

    Yup. I like the programming style and the way it encapsulates data. However. As I understand it, academics point out the downside to using closures and upvalues is that you can willingly or accidentally create long lived objects that keep a portion of the memory "locked". Which is evident, on the one hand, because if the function lives, then all the associated variables must live and cannot be garbage collected. On the other hand... It is not trivial to see the impact of that, it is not very clear from the code which variables have to be kept in memory. IMHO the academics may have a point.

     

    11 hours ago, jgab said:

    one need to use the debug library (

    Please login or register to see this link.

    ) that is blocked on the HC3

    Oh right, yes, the debug library for locals.

     

    11 hours ago, jgab said:

    but I'm not taking that bait :-) For the Python thing, I think the designer actually was happy when coming up with that syntax for a single item tuple... and a 0-tuple is ()....

    Hehehe I would not dare to derail this thread into a discussion of strangeness of other languages.

     

    What I wanted to say is... I don't see many thing in the Lua language that bite you, and require a "linter" to catch because the compiler itself won't catch it (iir the runtime barks at you, but that can be ... rather late).

     

    BTW I have been doing Python since version 1.2 and I think the reason behind this is quite mundane. Python did not use a "parser framework", they handcrafted the parser (up to 3.X that is still true). I imagine, in this case they sted "if it has a comma, then it is a list".  Does not explain why indeed () is an empty list, it should be (,) to be consistent, but that is a syntax error :D Give me Lua, please.

    Link to comment
    Share on other sites

  • Topic Author
  • 12 hours ago, petergebruers said:

    Excellent point.

     

    I think it sometimes is the "syntactic sugar" that throws off new users, but makes the code more readable in the end.

     

    This is one of those idiosyncrasies I use but it confuses new users:

     

    Please login or register to see this code.

     

    There is a lot going on there. You have to know how "or" returns valua, understand "short circuiting" in some cases, and understand that nil has its own type that cannot be added to a string...

     

    Yup. I like the programming style and the way it encapsulates data. However. As I understand it, academics point out the downside to using closures and upvalues is that you can willingly or accidentally create long lived objects that keep a portion of the memory "locked". Which is evident, on the one hand, because if the function lives, then all the associated variables must live and cannot be garbage collected. On the other hand... It is not trivial to see the impact of that, it is not very clear from the code which variables have to be kept in memory. IMHO the academics may have a point.

     

    Oh right, yes, the debug library for locals.

     

    Hehehe I would not dare to derail this thread into a discussion of strangeness of other languages.

     

    What I wanted to say is... I don't see many thing in the Lua language that bite you, and require a "linter" to catch because the compiler itself won't catch it (iir the runtime barks at you, but that can be ... rather late).

     

    BTW I have been doing Python since version 1.2 and I think the reason behind this is quite mundane. Python did not use a "parser framework", they handcrafted the parser (up to 3.X that is still true). I imagine, in this case they sted "if it has a comma, then it is a list".  Does not explain why indeed () is an empty list, it should be (,) to be consistent, but that is a syntax error :D Give me Lua, please.

    The problem is without the comma that they wouldn't know if (2-1)+(3-2) was the arithmetic calculation resulting in 2 or the tuple concatenation (yes, they use the + operator for that) resulting in (1,1)

     

    I know that we agree - but to add to it I think the mindset should be that functions are just values (or data types) like all other data types - or objects in object oriented languages. People have a tendency to accidentally create long lived data in forgotten global variables, arrays and hash tables too - or on the call stack in endless recursions....

     

    On the other hand, closures gives a very fined grained control over scope. Ex

    Please login or register to see this code.

     

    The point is that x is shared between two functions being created - and no one else can change/touch x - that's pretty powerful. In a way it's a similar mechanism we use in other languages with objects and private instance variables - but this is much simpler.

     

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

    • 1 month later...

    Is there a way to put a single button in a QA that adds a value each time it's pressed, and change the name of that button in that value?

    For example, when the button pressed 1 time, the name of the button is 1. After press another time it's 2. This until 10 and then start over again.

     

    I tried with function Quickapp: Clicked() and self:updateview, but doesn't work for me.

     

     

     

    Link to comment
    Share on other sites

    1 hour ago, Neo Andersson said:

    Please login or register to see this code.

     

    Use AddValue in your button's onReleased function 

    That works, thanks?

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