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

From Scene - Press button on QuickApp


robw
 Share

Question

Hi,

 

I would appreciate any help to finalize the use of QuickApps and Scenes on my HC3 when using my iTach. My QuickApp is exactly like Fibaros example at the bottom page here: 

Please login or register to see this link.

. The TCP Connection example. I've set up a couple of Quick Apps for my TV, receiver etc., and they work.

 

In the button of every Quick App is a code like this: 

 

self:send("sendir,1:3,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)
 
The example above is the Power button for a device that turns on or off the device when pressed. When clicked it sends the IR commands to the device.
 
My question now is, how do I call that button in a LUA scene?
 
I would like to use scenes to trigger special button presses in a row. As an example:
1. Press Power button in QA "TV" (To start the TV)
2. Press Power button in QA "Receiver" (to start the receiver)
3. Press Power button in QA "BluRay" (to start the BluRay)
4. Press BD button in QA "Receiver" (to set HDMI to BD) etc...
 
In the scene I have in the triggers section:
{
  conditions = {},
  operator = "all"
}
 
And in the LUA part I have::
fibaro:call(212, "btn_Power")      To call the receivers QA id 212, and press the Power button with id btn_power
 
But that won't work. Could anyone kindly help me?
 
Cheers,
 
 
 
Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0
Guest kallecux

Hello @robw

 

 

I solved my problem by defining one or more functions in the QA and placing the function call on the button.

 

You can then call the respective function from other LUA scripts or by clicking the button.

 

Expample:

in the QA: 

function QuickApp:Schlafzimmer(status)

....

end

 

from other LUA code:

 

fibaro.call(239, "Schlafzimmer","On")

 

Greetings.

Edited by kallecux
Link to comment
Share on other sites

  • 0
  • Inquirer
  • 43 minutes ago, kallecux said:

    Hello @robw

     

     

    I solved my problem by defining one or more functions in the QA and placing the function call on the button.

     

    You can then call the respective function from other LUA scripts or by clicking the button.

     

    Expample:

    in the QA: 

    function QuickApp:Schlafzimmer(status)

    ....

    end

     

    from other LUA code:

     

    fibaro.call(239, "Schlafzimmer","On")

     

    Greetings.

     

    How?

     

    This is the LUA code inside the QuickApp:

     

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

     

    function QuickApp:send(dataToSend, waitForResponse)
        self.sock:close()
        
        self:connect(function()     
            local dataConverted = self:parseData(dataToSend) -- replace string starting with '0x' to hex value
            self.sock:write(dataConverted, {
                success = function()
                    if waitForResponse then
                        self:waitForResponseFunction()
                    else
                        self.sock:close()
                    end
                end,
                error = function(err)
                    self.sock:close()
                end
            })
        end)
    end
     
    function QuickApp:parseData(str)
        while true do 
            if string.find(str, '0x'then
                i,j = string.find(str, '0x')
                str = string.sub(str, 1, i - 1) .. self:fromhex(string.sub(str, i + 2, j +2)) .. string.sub(str, j + 3)
            else
                return str
            end
        end
    end
     
    function QuickApp:fromhex(str)
        return (str:gsub('..'function(cc)
            return string.char(tonumber(cc, 16))
        end))
    end
     
    function QuickApp:waitForResponseFunction()
        self.sock:read({
            success = function(data)
                self:debug("response data:", data)
                self.sock:close()
            end,
            error = function()
                self:debug("response error")
                self.sock:close()
            end
        })
    end
     
    function QuickApp:connect(successCallback)
        print("connecting:", self.ip, self.port)
        self.sock:connect(self.ip, self.port, {
            success = function()
                self:debug("connected")
                successCallback()
            end,
            error = function(err)
                self.sock:close()
                self:debug("connection error")
            end,
        })
    end 
     
    function QuickApp:onInit()
        self:debug("onInit")
        self.ip = self:getVariable("ip")
        self.port = tonumber(self:getVariable("port"))
        self.sock = net.TCPSocket()
    end
     
    -------------------------------
     
    This is a button in the QA (btn_Power):
     
    self:send("sendir,1:3,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)
     
     
    Link to comment
    Share on other sites

    • 0
    Guest kallecux

    I think, you can send from outside the QA via:

     

    fibaro.call(ID_of_QA,"send",dataToSend, waitForResponse)

     

    if i understand your question.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 13 minutes ago, kallecux said:

    I think, you can send from outside the QA via:

     

    fibaro.call(ID_of_QA,"send",dataToSend, waitForResponse)

     

    if i understand your question.

     

    I'm thinking we are getting close, but how to choose the button to press? It is nowhere in the LUA code "fibaro.call(ID_of_QA,"send",dataToSend, waitForResponse)"?

     

    I tried: 

    fibaro.call(212"btn_Power", dataToSend, waitForResponse)
    fibaro.call(212"send", btn_Power, waitForResponse)
    fibaro.call(212"send", "btn_Power", waitForResponse)
    But it does not work.

     

    Edited by robw
    Link to comment
    Share on other sites

    • 0
    Guest kallecux

    OK - i did not press the button, i call the same function than the button. 

     

    So i must not think about "pressing the right button" i only have to think about calling the right function. 

    Create a function pro button and you are ready. This is my way... :-)

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 19 minutes ago, kallecux said:

    OK - i did not press the button, i call the same function than the button. 

     

    So i must not think about "pressing the right button" i only have to think about calling the right function. 

    Create a function pro button and you are ready. This is my way... :-)

     

     

    Ah, ok, thank you for helping out. I will try.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I tried to code as below:

     

        QA button:

    function QuickApp:SamsungPower()

    self:send("sendir,1:3,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)

    end

     

        Scene:

    fibaro.call(213"SamsungPower")
     
    But then the button on the QA don't work. Running the scene did nothing, either. Sorry.
    Edited by robw
    Link to comment
    Share on other sites

    • 0
    Guest kallecux

    I think the button must have the following lua code:

     

    self:SamsungPower()

     

    This Code has to be in the main QA:

     

     

    function QuickApp:SamsungPower()

    self:send("sendir,1:3,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)

    end

    Edited by kallecux
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 14 minutes ago, kallecux said:

    I think the button must have the following lua code:

     

    self:SamsungPower()

     

    This Code has to be in the main QA:

     

     

    function QuickApp:SamsungPower()

    self:send("sendir,1:3,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)

    end

     

     

    Ok, I understand. I'll test that! Thank's!

     

    Edit: Yep, that did the trick! I have to move the IR codes into functions in the main code. Thank's again! Very appreciated!

    Edited by robw
    Link to comment
    Share on other sites

    • 0
    Guest

    Did you manage to get this working from a scene?? (Pressing a QA button)

    fibaro.call(213"SamsungPower")

     

     

    I have the following in main code QA:

     

    function QuickApp:turnOff()
        self:debug("NAD DAC Turned Off")
        self:send("Main.Power=On"..self.enter)
     
    In the button code:
    self:turnOff()
     
    Button and main code works,

    But calling this from a scene with the below does not work...

     

     fibaro:call(720"turnOff");
    Edited by tortho
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @tortho

     

    Hi,

     

    Yes, I got it to work with the help from @kallecux.

     

    Here is my part of the main code in the QA:

     

    -- IR codes. They must be as functions in order to being able to launch them from a scene.
     function QuickApp:btn_Power()
     self:send("sendir,1:3,1,37993,1,1,171,171,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,64,22,21,22,21,22,21,22,21,22,21,22,21,22,64,22,64,22,21,22,21,22,64,22,64,22,64,22,64,22,21,22,21,22,64,22,64,22,21,22,21,22,21,22,37000x0D0x0A"true)
    end
     
    In the button for the QA (named btn_Power), I put:
     
    self:SamsungPower()
     
     
    In the calling scene, I put in this code in the conditions/trigger section:
     
    {
      conditions = {},
      operator = "all"
    }
     
    And in the LUA section of the scene, I put in this code:
     
    fibaro.call(213"btn_Power")
     
    Hope it can help you get going!
    Cheers,
     
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 10 hours ago, tortho said:

    Thank you, managed to get it to work with your description now :-)

     

    Great! :)

    Link to comment
    Share on other sites

    • 0

    But how is the function of sending IR code implemented? HTTPClient:request(address, params)? Can I give the code completely? or attach .fqa ?  

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • On 4/19/2020 at 2:33 PM, Scandal said:

    But how is the function of sending IR code implemented? HTTPClient:request(address, params)? Can I give the code completely? or attach .fqa ?  

    Sorry, I missed your question. I hope by now you have solved it. Otherwise here is a full code of the Main section. In the QA variable settings, you have to enter the IP of the iTach. In this example, I just have one Power button. Continue to add buttons of your liking. In the button setting, under "onReleased", you should enter "uibtn_PowerOnReleased" for this example.

     

    -- Sample that works similar to virtual devices on Home Center 2 
    -- Please configure quickapp by filling ip and port variables
    -- In added buttons just use self:send(<sting to send>, <waitForResponse>) function.
    -- On button clicked QuickApp will connect provided ip send the message. 
    -- If waitForResponse parameter is true, it does the following scenario: connect->send message->disconnect
    -- If waitForResponse parameter is false, it does the following scenario: connect->send message->wait for response->disconnect
     
    function QuickApp:send(dataToSend, waitForResponse)
        self.sock:close()
        
        self:connect(function()     
            local dataConverted = self:parseData(dataToSend) -- replace string starting with '0x' to hex value
            self.sock:write(dataConverted, {
                success = function()
                    if waitForResponse then
                        self:waitForResponseFunction()
                    else
                        self.sock:close()
                    end
                end,
                error = function(err)
                    self.sock:close()
                end
            })
        end)
    end
     
    function QuickApp:parseData(str)
        while true do 
            if string.find(str, '0x'then
                i,j = string.find(str, '0x')
                str = string.sub(str, 1, i - 1) .. self:fromhex(string.sub(str, i + 2, j +2)) .. string.sub(str, j + 3)
            else
                return str
            end
        end
    end
     
    function QuickApp:fromhex(str)
        return (str:gsub('..'function(cc)
            return string.char(tonumber(cc, 16))
        end))
    end
     
    function QuickApp:waitForResponseFunction()
        self.sock:read({
            success = function(data)
                self:debug("response data:", data)
                self.sock:close()
            end,
            error = function()
                self:debug("response error")
                self.sock:close()
            end
        })
    end
     
    function QuickApp:connect(successCallback)
        print("connecting:", self.ip, self.port)
        self.sock:connect(self.ip, self.port, {
            success = function()
                self:debug("connected")
                successCallback()
            end,
            error = function(err)
                self.sock:close()
                self:debug("connection error")
            end,
        })
    end
     
    function QuickApp:onInit()
        self:debug("onInit")
        self.ip = self:getVariable("ip")
        self.port = tonumber(self:getVariable("port"))
        self.sock = net.TCPSocket()
    end
     
    function QuickApp:uibtn_PowerOnReleased(event)
    self:send("sendir,1:1,1,37878,1,1,340,169,22,21,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,63,22,21,22,63,22,21,22,21,22,63,22,63,22,21,22,21,22,63,22,21,22,63,22,63,22,21,22,21,22,1500,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,3633,340,84,22,37000x0D0x0A"true)
    end
    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.

     Share

    ×
    ×
    • Create New...