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

41 minutes ago, jgab said:

Nice discovery.

 

Have a look in my toolbox and the child device. There I make a UIEvent handler that forward the events to the children. For some reason it's lacking in the default implementation...

You define a UIEvent handler where you can implement your logic to find the child. The example below is pretty involved and can be simplified,

Please login or register to see this code.

 

It works :)

but in child device I have to use the function without clicked

Please login or register to see this code.

 

  • Like 1
Link to comment
Share on other sites

  • Topic Author
  • 3 minutes ago, petrkl12 said:

    It works :)

    but in child device I have to use the function without clicked

    Please login or register to see this code.

     

    Children are a bit different. With "Clicked" it's only main QA when the "onReleased" field was empty - or it used to be.

    Link to comment
    Share on other sites

    OK so we have working solution for child devices with labels and buttons!

    Thanks

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

    4 hours ago, petrkl12 said:

    OK so we have working solution for child devices with labels and buttons!

    Thanks

    Wow. It was not available before.

    After some updates, it's now available. You found it. Nice.

    Link to comment
    Share on other sites

  • Topic Author
  • Ok, here is an example of the child with custom UI that @petrkl12 discovered.

    Please login or register to see this code.

     

    It's not that long but it's a fully functional Hue QA that supports lights that can be turned on/off and hue sensors.

    It creates 2 Childs using the table included in the beginning.

    We can provide a UI struct defining the buttons and slides and labels for the child.

    The QA make use of my toolbox as it is quite complex to construct viewLayout structures. The toolbox makes it easy to deal with Childs as classe and other useful data is saved with the child (using the child's quickAppVariables). Another issue is that the HC3 f**ks up the uiCallback structure that is essential for mapping buttons/sliders back to methods in the child. My Toolbox 'createChild' and 'loadChildren' saves away the original uiCallbacks structure and restores it when the child class is created,

    The full QA with toolbox files is here

    Please login or register to see this attachment.

    Beware that adding 'quickApp' interface to QuickAppChild is a hack. The HC3 GUI thinks it's a real QA in some respects and provide a code editor for it etc. However, all UIEvents and onApp events or funnelled back to the mother QA.

     

    A side note. 

    The Toolbox I use "hijacks" the :onInit method of the QA and does some configuration before.

    • First it leads the toolbox modules specified
    • Then it reads in the quickAppVariables and put them in a self.config table. This is very convenient as it is easier to use and we get nil back if the variable is not defined - not the empty string "" ? 
    • Then it loads the children. All info to restore the child are saved in the Child structure (like class)
    • Then it calls the user defined :onInit() method
    • Then it optionally calls QuickApp:main if it exists in a separate "thread" (setTimeout(main,0) so main is executed after we have exited :onInit.

     

    The pre config routine also checks if the QA is disabled and if so doesn't call :onInit()

    Normal QAs don't respect the UI checkbox "Device disabled" unless the QA explicitly checks for the property at startup.

     

    So, in the example below the QuickApp:onInit() can be coded quite clean...

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

    For child devices with labels and buttons:

    1. after creating is important to delete source code for main:

    Please login or register to see this code.

    2. After changing of name/room/... in child devices there is incorrect labels in child devices. For solving that issue the parent device has to be restarted.

     

    Overall it works correctly :)

     

    Example of child device with labels and buttons

    Please login or register to see this attachment.

     

     

    • Like 2
    Link to comment
    Share on other sites

  • Topic Author
  • This is an example of using the toolbox that, among other things, makes it easy to deal with triggers and events in a QuickApp.

     

    The QA named TriggerQA runs and watches events/triggers on the HC3 and sends them to other QAs that can subscribe on them. The format of the triggers try to adhere to the sourceTrigger format that we get in Scenes. There are some extensions for events that are not available in scenes.

     

    The idea is to have a generic triggerQA in the system that makes it easy for other QAs to deal with sourceTrigger events without having to poll /refreshStates and loading the system unnecessary. 

     

    Other QAs that want to receive events needs to declare

    Please login or register to see this code.

    The QA also needs to store in a quickAppVariable named 'TRIGGER_SUB' a list of triggers/events/sourceTriggers that it is interested in receiving.

    Ex.

    Please login or register to see this code.

     

    The TriggerQA will update it's list when it starts, when a QA is modified, created, or removed, or when it sets its 'TRIGGER_SUB' quickAppVariable.

     

    The actual code for this quite complex QA is quite simple due to the toolbox that provides all the primitive functions to deal with triggers and events. The dispatch mechanism is very efficient as it selects relevant events with a mix of hashing and optimised pattern matching.

     

    Supported events:

    Please login or register to see this code.

     

    The code looks like

    Please login or register to see this code.

     

    Please login or register to see this attachment.

     

    The QA was developed with the new HC3 emulator. It makes it very easy to run a testQA in parallel that can be deployed and removed and receive events from the triggerQA. 

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

  • Topic Author
  • Here is a little useful pice of code...

    Please login or register to see this code.

    It runs a function on a given interval in seconds,  starting at the even interval.

    So, seconds==3600 will run the function every hour on the hour.

    Seconds=60 will run the function every minute, on the minute.

     

    Well, we have seen many of these in the past - However, we are now approaching the time when daylight time savings (DST) happens. It's March 28, at 3AM here in Sweden, and all the timers (setTimeout) scheduled in QAs will fire wrongly if the were scheduled on a timespan crossing the DST.

     

    In my EventRunner framework there are a million timers I can't keep track of so here is a piece of code that restarts the QA the hour DST happens. ER will restart and all rules will be rescheduled and everything will be normal....

     

    So, intervalRunner runs a function every hour on the hour to see if DST changed and in that case restarts the QA:
     

    Please login or register to see this code.

     

    The code works on the HC3 but also in my emulator. Below is the log of a small scheduler that schedules 3 events during the day and does it at midnight.

    It's interesting to see how the timer scheduled at 02:00 actually fires at 03:00 (because time is moved forward). Then the QA is restarted and it reschedules the timers for 15:10 and 21:03 so they will run at the correct times.

    Please login or register to see this code.

     

    Two features in the emulator leveraged in this example. To be able to run faster than realtime (boring to watch long running code otherwise), and secondly the ability to set a start time. In this case the evening before the DST. 

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

  • Topic Author
  • Just now, Sjakie said:

    Jan, Thanks! added in main.

    //Sjakie

    Eventually I will add it to EventRunner so users can just set a flag if they want to restart on DST.

    Link to comment
    Share on other sites

    Jan, your TriggerQA must I see that as an extendend "remote"?

    When I migrated from HC2 to HC3 I grouped rules on a different way as before together.

    So on that way I minimized the number of remotes.

    In your rules its easy to start a scene so I am a bit lost I can benefit your trigger QA?

    //Sjakie

     

    Link to comment
    Share on other sites

  • Topic Author
  • 9 minutes ago, Sjakie said:

    Jan, your TriggerQA must I see that as an extendend "remote"?

    When I migrated from HC2 to HC3 I grouped rules on a different way as before together.

    So on that way I minimized the number of remotes.

    In your rules its easy to start a scene so I am a bit lost I can benefit your trigger QA?

    //Sjakie

    No, In the future I may modify ER4 so that it can leverage TriggerQA if it is on the same HC3.

    AT the moment TriggerQA is an example on how to do it. I don't have any QAs that uses TriggerQA at the moment.

    In theory a QA could check if there is an TriggerQA in the system and then use the Trigger QA to get its events, otherwise it can run its own /refreshStates loop - or let the user choose.

    Link to comment
    Share on other sites

    2 hours ago, jgab said:

    Eventually I will add it to EventRunner so users can just set a flag if they want to restart on DST.

    It could be perfect to add directly to ER4 :) thanks

    • Thanks 1
    Link to comment
    Share on other sites

    • 2 weeks later...
    W dniu 12.09.2020 o 12:49, jgab napisał:

    Here is a version of HC2Proxy that supports rollerShutter better - the buttons on the childDevice sends the right commands back to the HC2 device.

    v.1.10

    Please login or register to see this attachment.

     

    Hello,

     

    After last updates of HC3, proxy stopped working...Am I the only one who has a problem?

     

     

    [31.03.2021] [14:17:00] [ERROR] [QUICKAPP445]: QuickApp crashed

    [31.03.2021] [14:17:00] [ERROR] [QUICKAPP445]: Unknown error occurred:

    Link to comment
    Share on other sites

    W dniu 31.03.2021 o 14:24, dlizak napisał:

    @michal85pl it's not update fault. It's common issue with QuickApps:

     

    Thanks for the hint, I didn't know about this problem. After the reboot, the QA actually started working.
    Unfortunately there is a problem with HCL> HC3 communication, HCL does not send any messages to HC3. HC3 reads devices and values at the start of QA, enables switching on of switches, but does not receive feedback that a given switch changed state or motion was detected.

    @jgab do you know anything about this problem?

    Edited by michal85pl
    Link to comment
    Share on other sites

    Rolling back to version 5.050.13 and everything works as it should ... Unfortunately, HCL debug doesn't work anymore, so I can't tell what it's reporting ..

    Link to comment
    Share on other sites

    • 2 weeks later...
    On 2/10/2020 at 8:12 PM, jgab said:

    A thread to share some coding techniques for QuickApps? 

    Because QAs are "long running scenes" (they don't have to be loaded and restarted for every event) - it is actually worthwhile to build up a library of "nice to have" code and include them in QAs.

     

    Please login or register to see this link.

     

    Please login or register to see this link.

    s

    Please login or register to see this link.

    Please login or register to see this link.

     client

     

    List of posts:

    • Introduction to the QuickApp anatomy - tutorial
      • Please login or register to see this link.

        Lua functions and object-oriented programming. (QuickApp is a OO class, so we need that base)
      • Please login or register to see this link.

        The basic QuickApp functions and what they do... and how.
      • Please login or register to see this link.

        . More on QuickApp event handling - interaction with the UI and fibaro.call(<quickApp>,"name",...)
      • Please login or register to see this link.

        QuickAppChildren and how to raise them... what makes them tick?
      • Please login or register to see this link.

    • Please login or register to see this link.

    • Please login or register to see this link.

       (

      Please login or register to see this link.

      )
    • Please login or register to see this link.

      (fibaroapiHC3.lua)
    • Please login or register to see this link.

      (like fibaro.getSourceTrigger()) 
      • Please login or register to see this link.

    • Please login or register to see this link.

    • Please login or register to see this link.

    • Please login or register to see this link.

      (demonstrating the UI with buttons)
    • Please login or register to see this link.

      (demonstrates UI buttons that change labels/text to present options) It doesn't' actually schedules the profile yet. (

      Please login or register to see this link.

      )
    • Please login or register to see this link.

      - like when using net.HTTPClient() instead of FHTTP().
    • Please login or register to see this link.

      (without drifting)
    • Please login or register to see this link.

      (leveraging the "polling for triggers" code)
    • Please login or register to see this link.

       (and

      Please login or register to see this link.

      )
    • Please login or register to see this link.

      (files in a flat format)
    • Please login or register to see this link.

      (not strictly about QuickApps but related) - can speed-up time :-) 
    • Please login or register to see this link.

      (uses the QuickApp structure for asynchronous calls in a previous tip)
    • Please login or register to see this link.

      • Please login or register to see this link.

    • Please login or register to see this link.

       
    • Please login or register to see this link.

      - pushing events to external apps
    • Please login or register to see this link.

      - ex. power and battery and updating the properties (updates the little battery and power icon UI) @tinman
    • Please login or register to see this link.

      (and a few other)  ....    Ex. keyfob QA by @tinman
    • Please login or register to see this link.

      Please login or register to see this link.

      that makes it easier to develop QAs
      • 'basic' - Generic QA functions for loggin, loading modules, and management - used by all other modules. (

        Please login or register to see this link.

        )
      • '

        Please login or register to see this link.

        ' - QA functions to easily manage quickAppChild devices. Loading, saving state, getting UI events etc.
      • '

        Please login or register to see this link.

        ' - QA functions for defining event handlers and structuring your code accordingly. Makes it easy to setup timers in various ways...
      • '

        Please login or register to see this link.

        '  QA functions for recieving triggers like Scenes do. The events module will receive triggers if loaded, but own handler can be defined.
      • '

        Please login or register to see this link.

        ' - QA functions for declaring (synchronous) remote functions from other QAs
      • files - QA functionality for copying files between QAs
      • Please login or register to see this link.

        - QA functions for event publish/subscribe...
      • Please login or register to see this link.

        - QA functions for manipulation the UI elements of a QA
      • Please login or register to see this link.

        - QA function for emulating loadstring and evaluating Lua expression from strings
      • Please login or register to see this link.

        - Functions for timing code used in QA
    • Please login or register to see this link.

    • Please login or register to see this link.

      (MD5, HMAC, SHA-1, SHA-224, SHA-256, SHA-512/224, SHA-512/256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256) @tinman
    • Please login or register to see this link.

      @tinman
    • Please login or register to see this link.

      @tinman 

     

    Readers note. I started to call QuickApp devices for QDs (as in QuickApp Device, thought QAs sounded like Question and Answers). So, I use the word QD here and there but I'm not religious about it...

     

     

    hello @jgab

    hope all this finds you well

    quick question: if for a room, i'm having "n" different hue scene to apply and would like to have a QA with a button for each of them, 

    is there a way to automatically create Buttons and labels depending of the final number of scenes ?

    :)

    thanks in advance if you have any links or recommendation,

    don't manage to find something on this

    br

     

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, Mateo said:

     

     

    hello @jgab

    hope all this finds you well

    quick question: if for a room, i'm having "n" different hue scene to apply and would like to have a QA with a button for each of them, 

    is there a way to automatically create Buttons and labels depending of the final number of scenes ?

    :)

    thanks in advance if you have any links or recommendation,

    don't manage to find something on this

    br

     

    Yes there are. Search for posts in this thread on 'viewLayout'. That is the structure you need to manipulate to add UI elements to an QuickApp.

    Link to comment
    Share on other sites

    5 minutes ago, jgab said:

    Yes there are. Search for posts in this thread on 'viewLayout'. That is the structure you need to manipulate to add UI elements to an QuickApp.

     

    thanks :) immediately start my search :)

     

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