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

Difference between sleep and SetTimeout


Question

Posted

What is the internal difference between the two.  I see people claiming that the latter is better, but should there be a difference ?  And if setTimeout is more efficient and I change my scenes to use it, does the instance have access to the variables in that scene ?

3 answers to this question

Recommended Posts

  • 0
Posted

Can i also join this @RohitNz, also why do many user use fibaro abort while others recommend kill scenes ??

  • 1
Posted (edited)
9 hours ago, RohitNz said:

What is the internal difference between the two.  I see people claiming that the latter is better, but should there be a difference ?  And if setTimeout is more efficient and I change my scenes to use it, does the instance have access to the variables in that scene ?

 

I believe that there were some thinking that fibaro:sleep somehow created a higher load on the HC2 but that seems not to be the case. 

However, fibaro:sleep do freeze everything running in the scene (which is kind of the idea), but it also freezes timers set with setTimeout and callbacks from httprequest etc.. which is why it's not recommended to mix fibaro:sleep and setTimeout in scenes.

 

So it mostly comes down to a preferred style of programming. 

However, it actually makes some complex scheduling easier and more efficient to program.

 

Ex. If you have a scene where you want to turn on lamp every 15 minute, turn it off after 4min you need to do something like

Please login or register to see this code.

which easily becomes messy if you want to turn on/off more than one light at different intervals. The alternative is to create a loop that every minute checks if you need to do something

Please login or register to see this code.

 

With setTimeout, in this example, we only need to specify when to run - which actually makes it a little bit more efficient (in line with the first example).

Please login or register to see this code.

Of course it's a matter of taste but I find the latter example also easier to understand.

 

...and we can abstract it into a function, which is not that easy to do with a fibaro:sleep model...

Please login or register to see this code.

...and run 3 independent loops that turn on/off lamps at different intervals. Both easy and efficient.

 

Internally setTimeout schedules the function to run after the delay specified, and it is only "woken up" after that delay. However, if you have several functions scheduled with setTimeout they don't run in parallell. One function needs to terminate before the next in turn can run. 

 

and yes, functions called by setTimeout can share variables defined in the scene.

Edited by jgab
  • Like 1
  • 0
  • Inquirer
  • Posted
    On 5/28/2019 at 8:17 PM, jgab said:

     

     

    However, fibaro:sleep do freeze everything running in the scene (which is kind of the idea), but it also freezes timers set with setTimeout and callbacks from httprequest etc.. which is why it's not recommended to mix fibaro:sleep and setTimeout in scenes.

     

     

    Thanks, that explains why I could not get my Wifi_Usage scene to work.  I was scanning the network connections , matching mac addresses and determining which of the family members were using wifis on their phones.  When I put  a delay in, I don't get a httprequest  :-)

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