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

Tricks: Balancing HC3 load during minute


afendikov

Question

Hi all,

 

Want to share - it may be useful for your HC3 to balance CPU load.

 

I have several QA working every minute ( power control, data export, etc.. ) and I started to give every QA their own time space during "one minute" time.

 

when QA starts, it wait till right second, which I define in secondToExecute - before start.

 

for example - export to Influx start on 30th second within one minute.

and main loop also calculate how much seconds taken by all routines and correct "sleep" time to run again on 30th second

 

As I know, Fibaro & LUA can't provide milliseconds timing, so - it can be some un-sync in time, because milliseconds give some extra timing.

But this is not dramatical situation - solution can be - restart HC3 every 1 week to restart all QA's and sync them again to their timings

 

Code example below:

 

function QuickApp:loop(refresh)
    local timeNow = os.time()

 

-- any code to run

 

    loopSeconds = os.time() - timeNow
    self:trace('loop seconds = '..loopSeconds)

    fibaro.setTimeout((refresh-loopSeconds) * 1000, function() 
        self:loop(refresh)
    end)
end

 

function QuickApp:syncTime(timeout)
    local secondToExecute = 30


    self:trace ("now is "..os.date("%H:%M:%S").." | trying to sync to "..secondToExecute.." sec" )
    timer = secondToExecute - ( os.time() - 1591477200 ) % 60
    if timer == 0 then
        self:loop(timeout)
    elseif timer < 0 then
        timer = 60 + timer
    end
    self:trace ("need to sleep "..timer.." sec for sync" )

    fibaro.setTimeout(timer * 1000, function() 
        self:loop(timeout)
    end)
end

 

function QuickApp:onInit()
    self:debug("Starting... QuickApp Init")
    self.loop1timeout  = 60

 

    self:syncTime(self.loop1timeout)
end
 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

@afendikov Kostya, actually @jgabJan already said about this.

please use setInterval(function() self:updateWeather() end,1000 * 60 * 15) -- 15 minutes

Link to comment
Share on other sites

  • 0
  • Inquirer
  • 28 minutes ago, 10der said:

    @afendikov Kostya, actually @jgabJan already said about this.

    please use setInterval(function() self:updateWeather() end,1000 * 60 * 15) -- 15 minutes

     

    It doesn't work for me without correction:

     

    [07.06.2020] [11:50:31] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519830

    [07.06.2020] [11:50:38] [TRACE] [QUICKAPP309]: loopSeconds = 8

    [07.06.2020] [11:51:38] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519898

    [07.06.2020] [11:51:43] [TRACE] [QUICKAPP309]: loopSeconds = 5

    [07.06.2020] [11:52:43] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519963

    [07.06.2020] [11:52:49] [TRACE] [QUICKAPP309]: loopSeconds = 6

     

    I used     setInterval(function() 
            self:loop(refresh)
        end, refresh * 1000)

     

    where refresh = 60

     

    But main post was about to start QA in exact second.

     

    Link to comment
    Share on other sites

    • 0
    5 minutes ago, afendikov said:

     

    It doesn't work for me without correction:

     

    [07.06.2020] [11:50:31] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519830

    [07.06.2020] [11:50:38] [TRACE] [QUICKAPP309]: loopSeconds = 8

    [07.06.2020] [11:51:38] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519898

    [07.06.2020] [11:51:43] [TRACE] [QUICKAPP309]: loopSeconds = 5

    [07.06.2020] [11:52:43] [TRACE] [QUICKAPP309]: lastUpdateTS :1591519963

    [07.06.2020] [11:52:49] [TRACE] [QUICKAPP309]: loopSeconds = 6

     

    I used     setInterval(function() 
            self:loop(refresh)
        end, refresh * 1000)

     

    where refresh = 60

     

    But main post was about to start QA in exact second.

     

     

    Start on even second (+ optional offset). This doesn't loop, just start a function at specified time.

    Please login or register to see this code.

    To loop without drift just do this

    Please login or register to see this code.

    To loop 15s past even minute do

    Please login or register to see this code.

    There was another post here how to patch setInterval to get a drift free version.

     

    Edited by jgab
    • Like 3
    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.

    ×
    ×
    • Create New...