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
  • In the latest release 5.142.83 beta we have access to Lua metatables (

    Please login or register to see this link.

    )

    They may not be exceptionally useful but they have their uses - ex. having custom tostring functions for tables

    or overloading functionality for Lua operators with your own custom types.

     

    Ex. If we would like to truncate Lua strings we could write a function

    Please login or register to see this code.

    and use it like

    Please login or register to see this code.

    it will make sure that the string is max 'len' characters and add ".." at the end if it is truncated (total length incl the ".."). 

    This can be useful when we have to make sure that a string fits inside a UI label ex.

     

    With metatables we can define this as an operator on strings. We can repurpose the integer divide operator "//"

    Please login or register to see this code.

    Here we retrieve the metatable assigned to the String data type and we add a function for the "//" idiv operator.

    Now we can do

    Please login or register to see this code.

    Is it better?, well sometimes it can help the coding abstraction...

     

    Your own defined Lua tables don't have a metatable assigned to them so we need to assign one.

    A useful meta method is .__tostring that is used to convert an object to a string when we do tostring(obj)

    Please login or register to see this code.

    Here we assign a metatable with the __tostring method set to the json.encode function.

    This means that when we do

    Please login or register to see this code.

    ...we get our table automatically encoded when we print it out to the console - which can be helpful when debugging.

    It turns out that the __tostring method is called when we do print, tostring or string.format. However, not when we concatenate with the ".." operator

    Please login or register to see this code.

    gives an error as myTable is not a string and __tostring is not called in this case.

    However, we can overload the ".." operator for our table to make it work...

    Please login or register to see this code.

     

    Custom data types can be implemented, like a temperature datatype

    Please login or register to see this code.

     

    ...more fun to come :-) 

    • Like 3
    Link to comment
    Share on other sites

    • 3 weeks later...
    On 5/3/2020 at 3:16 PM, jgab said:

    Tip.

    We can set an UI element with self:updateView(<element>,<type>,<value>)

    but there is no ready made function to get the value. We do get the values in the button/slider callbacks, but sometimes it would be nice to get the value directly.

    To get the value or text from an UI element 

    Please login or register to see this code.

    It take the deviceId as the first argument.

    Ex.

    print(getView(39,"myLabel","text"))

     

    There is an issue with slider element. They seem to have troubles updating their values when activated (moved)

    I have noticed that explicitly updating the view when a slider is activated helps to set the 'value' field of a slider.

    Ex.

    Please login or register to see this code.

     

     

    Thanks so much for this - I have been after this solution for days now!!

    Link to comment
    Share on other sites

    • 3 weeks later...

    Hello, I bought three Tuya wifi roller shades. I found quick app Tuya_generic_sniffer_v0.1.fqa (from page

    Please login or register to see this link.

    ). When I used it for one blind, it works normally (picture 1). There is a problem with the other two blinds (Figure 2). Where is problem? What is the cause? Help me please.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    W dniu 28.08.2022 o 01:09, tinman napisał:

     

    TUYA over cloud is nice (easier to recognize device type/class), however i prefer to use local access. Attached TUYA QuickApp, for Näve Divora (Wisdom) lamps (CW/WW) and NEO Smart Bulb RGBW E27 (RGBW-WW).

     

    For local access you need the obtain the key and id from your device, so the "long way" over tuya dev portal, free account and dev local_key readout over web console is necessary.

    The good thing about tuya, once you get these keys, your subsripton can expire, the local_key never expires once generated and stored on device (whch happens during registration of device in the app itself).

     

    I will post soon other tuya QuickApps, and cloud maual (updated, that picture below is year old or so), but for now for these who knows how to use (or who wish to cry about my code hehe).

     

     

    Please login or register to see this link.

     

    Please login or register to see this attachment.

     

    Please login or register to see this attachment.

     

     

     

     

    EDIT: this is really dirty implementation, but of course working. The best way to work with TUYA is to read device template / information over cloud api, filter the included classes, types and conversions, assign then matching BUI element and create such child QA. But that means, one need still, at leat at time of creation, cloud acces, and when this is offline, one would be unable to create anything. So my implementation (wrote some of them for tuya) is always hardcoded for specific device, lot of them will match one of the other QA, but for others one need to change part of the code.

     

    EDIT2: When trying to implement / change the QuickAps above, to support your specific tuya device, you might use the attached tuya sniffer:

     

    Please login or register to see this attachment.

     

    How to use it:

    - instal the sniffer QA

    - ensure non of your mobile devices has TUYA app opened, if so, close them

    - set IP, device id and device key and save

    - disable wifi ony you mobile device, open the TUYA app (TUYA can handle only ONE connection locally, that's why)

    - click on "connect" button from this QuickApp (if the QuickApp is crashing, won't connect, click few times on disconect, and try again to connect)

    - click / select this you wish to sniff, e.g. you wish to know what need to be send to turn light on, cick "On" in the TUYA app

    - watch the console window on your HC3, you should see some messages:

     

    -- CB: 9 CMD: HEART_BEAT -> these are just "pings", the QuickApp need to ping the TUYA device from time to time

    -- CB: 10 CMD: DP_QUERY -> these are queries, showing your TUYA device current state, e.g.

     

     {"dps":{"23":736,"20":true,"22":467,"21":"white"},"devId":"f4326db31e606356632c"}

     

    dps -> is command set, it contains number:value, where number is the TUYA command and value the current value

    from above this means, TUYA command 23 is set to 736, 20 to true, 22 to 467, 21 to "white". From developer portal you might check what these command are representing, 20 is on/off, 23 (in this class) warm/cold white value * 10, 22 is brightness * 10 and so on. However, there is as well bit older set, e.g. 1 is o/off, 2 is mode, 3 is intensity/level of 2, etc. So  just don't worry if the QuickApp is showing different things as you might expect from API docummentation (you simply reading wrong one).

     

    devID: is your TUYA device id

     

    -- CB: 8 CMD: STATUS, followed by e.g.

     

      {"t":1665264639,"dps":{"20":false},"devId":"f4326db31e606356632c"}

     

    t is unix time timestamp, dps is again TUYA command, which the sniffer have seen while you did something in the TUYA app. In this case i turned light off

    devID: is your TUYA device id

     

    These TUYA command are of course defined in TUYA API, one might get crazy however if the device is combination of two or more devices, e.g. ceiling fan with light - the turn on/off is for the major device, ceiling fan, not for light :) But as said above, with the sniffer one can check every function within few minutes.

     

    Having all the TUYA command (and values) information it's easy to wrie own function, e.g

     

    local chandata = {
                    ['20'] = true
            }

    self:sendCommand(chandata)

     

    will send TUYA Command 20 with value "true"

     

    You might see other CBx CMD:yyy messages as well, they are more or less docummented on TUYA dev page (or in the QuickApp itself, tuyaAPI file)

     

    Hello, is it still working? 

    Link to comment
    Share on other sites

    6 hours ago, domin12 said:

    Hello, is it still working? 

     

    it worked 2M ago, so if TUYA haven't changed anything special, it should still work.

     

    Oh wait, you have frist to close any local tuya app, run the sniffer quickapp, and then open tuya again, sometimes even only ovr cloud. This is because tuya acceppt only one local connection at time, so if it get con refused it does switch automatically to cloud even if you try to connecct localy

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

    • 3 weeks later...
    On 7/29/2020 at 7:20 PM, jgab said:

    Interactive push works (sometimes,. I got some errors)

    Anyway, we can now do an interactive push in a QA that makes a callback to a function of our choice if the user press Yes. This was a bit more complex on the HC2.

    Please login or register to see this code.

     

    Hi Jan
    Can this be done through Eventrunner 5 ?

    Link to comment
    Share on other sites

  • Topic Author
  • 16 minutes ago, ChristianSogaard said:

    Hi Jan
    Can this be done through Eventrunner 5 ?

    I haven’t been able todo interactive push for a longtime- comes through as regular pushes.

    Link to comment
    Share on other sites

    2 minutes ago, jgab said:

    I haven’t been able todo interactive push for a longtime- comes through as regular pushes.

    I tested it a couple of times this week. I got all messages at the phone and feedback works as well. I see it in the log

    using iPhone newest fw 

    Edited by ChristianSogaard
    Typo
    Link to comment
    Share on other sites

    Works fine for me :-)
    It took me a little while before i discovered that i need to do the long press to activate the Yes/no
     
    image.jpeg.6e75d432d73c3c3a74c0205e2f5694af.jpeg
     
    [14.12.2023] [09:52:20] [DEBUG] [QUICKAPP1862]: onInit 1862
    [14.12.2023] [09:52:45] [DEBUG] [QUICKAPP1862]: onAction: {"args":["0xaaaad5881450"],"actionName":"OK_BUTTON","deviceId":1862,"manual":true}
    [14.12.2023] [09:52:45] [DEBUG] [QUICKAPP1862]: User said Yes!
    Edited by ChristianSogaard
    Typo
    Link to comment
    Share on other sites

  • Topic Author
  • 40 minutes ago, ChristianSogaard said:
    Works fine for me :-)
    It took me a little while before i discovered that i need to do the long press to activate the Yes/no
     

    Please login or register to see this link.

     
    [14.12.2023] [09:52:20] [DEBUG] [QUICKAPP1862]: onInit 1862
    [14.12.2023] [09:52:45] [DEBUG] [QUICKAPP1862]: onAction: {"args":["0xaaaad5881450"],"actionName":"OK_BUTTON","deviceId":1862,"manual":true}
    [14.12.2023] [09:52:45] [DEBUG] [QUICKAPP1862]: User said Yes!

     

    Ok, I will test today - fingers crossed :-) 

    Link to comment
    Share on other sites

  • Topic Author
  • (Ok, this is the wrong thread for this discussion - moving to ER5 thread)

     

    Try this, if it works I will add the ask function  ER

    Inside main()

     

    Please login or register to see this code.

     

    The last argument to ask, 10 in the example is the timeout in seconds.

    ask returns true or false depending on if user chose Yes" or "No"

    Interactive push doesn't seem to return anything when "No" is pressed. Instead we timeout after the specified number of seconds, 10s in this case, and return false.

     

    Edited by jgab
    Link to comment
    Share on other sites

    Hello, I am wondering if someone could help me to get my Temp Readings on diffrent lines in the Label:

    This is how it is now:

    self:updateView("info8", "text","🌡️Outdoor: "..TempOutdoor.."°C🌡️Floor: "..TempFloor.."°C🌡️Room: "..TempRoom.."°C" )
    self:updateView("info9", "text","🌡️Last Temp Reading:"..TempTimeStamp.."🕒" )
     

    Please login or register to see this image.

    /monthly_2023_12/image.png.a273288fe4470c66e36e5b4e29a8a630.png" />

     

     

    My goal is to get the outdoor, Floor And room on seperate lines without making more labels.

    Because ill get that big distance between the lines then. like it is in the picture.

     

    and one more thing, how may I make more space beetween the text on the same line? since space on make 1 "space" even if ill make 5 spaces. 

     

    May someone give me an exampel? :) 

     

     

     

    Link to comment
    Share on other sites

    38 minutes ago, Brors94 said:

    Hello, I am wondering if someone could help me to get my Temp Readings on diffrent lines in the Label:

    This is how it is now:

    self:updateView("info8", "text","🌡️Outdoor: "..TempOutdoor.."°C🌡️Floor: "..TempFloor.."°C🌡️Room: "..TempRoom.."°C" )
    self:updateView("info9", "text","🌡️Last Temp Reading:"..TempTimeStamp.."🕒" )
     

    Please login or register to see this link.

     

     

    My goal is to get the outdoor, Floor And room on seperate lines without making more labels.

    Because ill get that big distance between the lines then. like it is in the picture.

     

    and one more thing, how may I make more space beetween the text on the same line? since space on make 1 "space" even if ill make 5 spaces. 

     

    May someone give me an exampel? :) 

     

     

     

    Breaking lines in html you can use \n at the end of the line. This is working in Fibaro. Adding space between texts i have never tested, but in HTML you usually use spaceholders, like &nbsp, but i have never tried it in Fibaro, maybe it will work. Be aware, that you will not see effects while in editor. Leave the editor to have changes take place.

    Please login or register to see this code.

     

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

    11 minutes ago, Neo Andersson said:

    Breaking lines in html you can use \n at the end of the line. This is working in Fibaro. Adding space between texts i have never tested, but in HTML you usually use spaceholders, like &nbsp, but i have never tried it in Fibaro, maybe it will work. Be aware, that you will not see effects while in editor. Leave the editor to have changes take place.

    Please login or register to see this code.

     

    Both works perfect :D Thanks!

    Link to comment
    Share on other sites

  • Topic Author
  • Here is a code snippet that is inspired by @petergebruers

     

    This works on the HC3 QAs. It will warn (or throw errors) when you try to assign or read a global variable that is not declared.

    It is useful because it could be a local Lua variable you want to assign/read but you misspell the name and it is 
    interpreted by Lua as a new global Lua variable and it can be hard to detect.

    Improvements are welcome.

     

    Please login or register to see this spoiler.

     

    So in a QA add the code above to a separate file.

     

    Then you can test

    Please login or register to see this code.

    and

    Please login or register to see this code.

     

     

    Edited by jgab
    Link to comment
    Share on other sites

    I have run into a problem again and are hoping for some help.

     

    I have this:

    Test = hub.getValue(1648, "centralSceneSupport")  ---ID of Heatit Dimmer
    Test2 = json.encode(Test)
    Test4 = hub.getValue(37, "sceneActivation")  ---ID of dimmer2 light.
    print("Test:",Test)   --- Gives: table: 0xaaaaba971f50
    print("Test2:",Test2)     ---Gives: [{"keyAttributes":["Pressed","Released","HeldDown","Pressed2","Pressed3"],"keyId":1},{"keyAttributes":["Pressed","Released","HeldDown","Pressed2","Pressed3"],"keyId":2}]
    print("Test4:",Test4)    --Gives: 20   ---- This one I can subscribe to, but i dont know how to subscribe to thes Test2

    Log:

    Please login or register to see this attachment.

     

    As I use this in a Refreshstatesubscriber and this works:

     

    hub.getValue(37, "sceneActivation") == 20

     

    but I cant figure out how to use thes?:

    hub.getValue(1648, "centralSceneSupport") == value  {
            keyAttribute = "HeldDown",
            keyId = 2
          }

     

    any1 got a suggestion? :) 

     

     

     

    Link to comment
    Share on other sites

    I use Tuya_generic_sniffer_v0.1.fqa to control blinds. Can someone please advise me how to supplement the application so that the state of individual blinds is stored in a global variable so that I can use it in scenes (the percentage of the blind opening is enough).

    Edited by Martin F.
    Link to comment
    Share on other sites

    On 12/28/2023 at 1:57 PM, Brors94 said:

    I have run into a problem again and are hoping for some help.

     

    I have this:

    Test = hub.getValue(1648, "centralSceneSupport")  ---ID of Heatit Dimmer
    Test2 = json.encode(Test)
    Test4 = hub.getValue(37, "sceneActivation")  ---ID of dimmer2 light.
    print("Test:",Test)   --- Gives: table: 0xaaaaba971f50
    print("Test2:",Test2)     ---Gives: [{"keyAttributes":["Pressed","Released","HeldDown","Pressed2","Pressed3"],"keyId":1},{"keyAttributes":["Pressed","Released","HeldDown","Pressed2","Pressed3"],"keyId":2}]
    print("Test4:",Test4)    --Gives: 20   ---- This one I can subscribe to, but i dont know how to subscribe to thes Test2

    Log:

    Please login or register to see this attachment.

     

    As I use this in a Refreshstatesubscriber and this works:

     

    hub.getValue(37, "sceneActivation") == 20

     

    but I cant figure out how to use thes?:

    hub.getValue(1648, "centralSceneSupport") == value  {
            keyAttribute = "HeldDown",
            keyId = 2
          }

     

    any1 got a suggestion? :) 

     

     

     

    Would you be up to use

    Please login or register to see this link.

    by JGAB in this forum? - a lot of the stuff you are doing are already there and easy to use.

     

    Link to comment
    Share on other sites

    21 minutes ago, ChristianSogaard said:

    Would you be up to use

    Please login or register to see this link.

    by JGAB in this forum? - a lot of the stuff you are doing are already there and easy to use.

     

     

    I actually ended up with this when I understood how it worked and how to use it 😅

     Got this out of it:D 
     

     local Heatits2HeldDown = SourceTriggerSubscriber()
        Heatits2HeldDown:subscribe(
    {type='device', id=PirIDHeatit, property='centralSceneEvent', value={keyId=2, keyAttribute='HeldDown'}},
    function(event)      

     

    I tried the Eventrunner 5 but I am waiting a bit before jumping in on it :D 

    Want to learn a bit more of the basic Lua in the quickapps and understand some of that work too. 

     

     

     

     

     

    • Like 1
    Link to comment
    Share on other sites

    On 10/5/2023 at 3:32 PM, tinman said:

     

    ensure no space signs in the variables, and ensure you copy/paste the special characters from tuya local password via tool like notepad (to not copy with formating/wrong character set)

     

     

    yes, they changed last year how the local password looks like (it contains now special characters as well) and i had already some issues with one person, however i boudht exact same tuya device and it's working for me. 

    thx, it didn't help, but i noticed is that it returns such "unformated" strings for my curtain motor devices, but for some other the sniffer (same QAPP) returns the expected dps codes. So it seems that for some devices commands are coded diffrently. I hope it helps somebody.. 

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