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

HTTP request


Question

Posted

I have a while-loop in a LUA-scene

deviceStopDuration=6
countdown=-1
limit = 10
 
function sendText(message)
    local url=[URL]
    net.HTTPClient():request(url,{
        options={
            method = 'GET',
            timeout=2000
        },
        success = function(response)
            print("response.status .. "\n - ".. response.data)
        end,
        error = function(message)
            error("Error: " .. response.status .. " - ".. response.data .. " - ".. message)
        end
    })
end
 
hub.debug(tag,"-START---------------------------")
while not cancel do
    hub.debug(tag,countdown)  
    if measuredvalue > limit then  
       if countdown == -1 then
            sendText("Text to send when starting!")
      end
      countdown = deviceStopDuration
    else
        if countdown > 0 then
            countdown = countdown - 1
        elseif countdown < 0 then
            cancel=true
        end
    end
 
    if countdown == 0 then
        sendText("Text to send when done!")
        cancel=true
    elseif countdown > 0 then
        hub.sleep(1000)
    end
end
hub.debug(tag,"-END---------------------------")

where I want to send a HTTP request when:

1) I enter the loop

and

2) When the loop is exited

 

The request is executed in both situation, but the output from the code is:

[2024-06-27] [12:29:53] [DEBUG] [SCENE55]: -START---------------------------
[2024-06-27] [12:29:54] [DEBUG] [SCENE55]: 6
[2024-06-27] [12:29:56] [DEBUG] [SCENE55]: 5
[2024-06-27] [12:29:57] [DEBUG] [SCENE55]: 4
[2024-06-27] [12:29:58] [DEBUG] [SCENE55]: 3
[2024-06-27] [12:29:59] [DEBUG] [SCENE55]: 2
[2024-06-27] [12:30:00] [DEBUG] [SCENE55]: 1
[2024-06-27] [12:30:00] [DEBUG] [SCENE55]: -END---------------------------
[2024-06-27] [12:30:01] [DEBUG] [SCENE55]: Text to send when starting!
[2024-06-27] [12:30:01] [DEBUG] [SCENE55]: Text to send when done!

 

Everything is working, except that both texts are send simultaneously when the loop exits?

Why is the bold line above (12:30:01) written first upon exit of the loop??? What can be done to solve this?

 

I hope you guys understand and can guide me!

 

 

7 answers to this question

Recommended Posts

  • 0
Posted

Http requests are asynchronous so there will be no time for the http callback until your while loop is done.

 

  • 0
  • Inquirer
  • Posted

    Is there any way that I can send 2 requests, with a loop between, on 1 trig of the scene?

    • 0
    Posted (edited)

    The easiest way is to replace the while loop with an "setInterval loop"

    Please login or register to see this code.

     

    Edited by jgab
    • 0
  • Inquirer
  • Posted

    Hej @jgab!

    And thanks for your reply!

     

    Are setInterval and clearInterval possible to use in scenes? Or just in quickapps?

     

    I get this error when implementing your suggestion:

    (load):47: attempt to call a nil value (global 'setInterval')

    • 0
    Posted (edited)

    Sorry, only available in QAs... 😕

    You can implement them yourself...

    Please login or register to see this code.

     

    Edited by jgab
    • 0
  • Inquirer
  • Posted (edited)

    Ahaaa, that's what I thought! Thanks!

     

    Is there any way to make a QA run when a condition is met (i.e. temperature over xx degrees)? Or do I need to create a loop and check with an interval? Wouldn't that use to many system resources??

    Edited by MiZ
    • 0
  • Inquirer
  • Posted

    Hej @jgab, do you know how to trigger the request twice?

    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.

    ×
    ×
    • Create New...