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

Schedule of times in LUA


KristianO

Question

Recommended Posts

  • 0

It's always tricky to compare times with equal (==), because you could miss the time windows. The main problem here however, is that setTimeout only run once and you will only trigger one of your calls if that happens to be the right time in 1 minute (60*1000) from you starting the scene.

A lightweight scheduler, invoking events on a daily basis can look like below. The trick is to call setTimeout again after your call so that it will run the next day again - which I believe is the thinking here...

The scene is "active" as long as there are timeouts/events waiting to execute or until you abort the scene (when all timers are canceled)

Please login or register to see this code.

Edit: added time parameter optionally being a function, i.e sunset/sunrise...

Edit2: added optional interval parameter

Edit3: fixed bug in bumping up the time

Edit4: added messages making it easier to see what is scheduled and when

Edit5: added debug msg when setTimeout is called, and offset function for sunset/sunrise

Edit6: fixed "bug" in calculating if time already passed

P.S would be nice if Fibaro allowed load/loadstring from scenes

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks a lot jgab! Im so graceful with your kindness! I owe you big time!

    I will test and let you know!

    Link to comment
    Share on other sites

    • 0

    Thanks, I tried it a bit and caught some bugs that I fixed (bumping timer). More testing may reveal more bugs so let me know. Added trace messages.

    Myself?, I'm running robmac's scheduler

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

    Link to comment
    Share on other sites

    • 0
    Guest GG

    KristianO, in your first if-statement you should test with "07:00" as you have %02d in your format string.

    I've been using the same type of code (with a sleep-statement) for more than a year without it missing a single time. To be sure it doesn't miss a time I sleep 59 seconds.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • One strange thing, Eg the id 46 should be on between 7AM until 10:30PM. When i now tried it and push "start" button, the light quickly switched OFF and then ON again... in a second.

    I did this just now, so before 10:30PM and it should be on.

    The id 42/44 that by this time should be Off and closed, nothing happened.

    [DEBUG] 21:32:19: Catchup Living room Window/On at Sun Jan 18 21:32:19 2015

    [DEBUG] 21:32:19: Catchup Bedroom Blind/Open at Sun Jan 18 21:32:19 2015

    [DEBUG] 21:32:19: Catchup Bedroom Light/On at Sun Jan 18 21:32:19 2015

    [DEBUG] 21:32:19: Catchup Bedroom Prepare at Sun Jan 18 21:32:19 2015

    [DEBUG] 21:32:19: Scheduled Night time at Sun Jan 18 22:30:00 2015

    [DEBUG] 21:32:19: Scheduled Living room Window/On at Mon Jan 19 07:00:00 2015

    [DEBUG] 21:32:19: Scheduled Bedroom Blind/Open at Mon Jan 19 10:00:00 2015

    [DEBUG] 21:32:19: Scheduled Bedroom Light/On at Mon Jan 19 16:00:00 2015

    [DEBUG] 21:32:19: Scheduled Bedroom Prepare at Mon Jan 19 18:30:00 2015

    Link to comment
    Share on other sites

    • 0

    Can you copy and paste the 'schedule" commands you have in your scene so I can have a look at them?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I just took away a few of the last ones.

    schedule("07:00:00", "Living room Window/On", function() fibaro:call(46, "turnOn") end);

    schedule("10:00:00", "Bedroom Blind/Open", function() fibaro:call(42, "open") end);

    schedule("16:00:00", "Bedroom Light/On", function() fibaro:call(44, "turnOn") end);

    schedule("18:30:00", "Bedroom Prepare", function() fibaro:call(42, "close"); fibaro:call(44, "turnOff") end);

    schedule("22:30:00", "Night time", function() fibaro:call(46, "turnOff") end);

    --schedule(sunset, "Kitchen/On", function() fibaro:call(46, "turnOn") end);

    --schedule(sunrise, "Kitchen/Off", function() fibaro:call(46, "turnOff") end);

    --schedule("07:00:00", "Kitchen/Off", function() fibaro:call(46, "turnOff") end, 60*60); -- Every hour from 07:00

    Link to comment
    Share on other sites

    • 0
    It's always tricky to compare times with equal (==), because you could miss the time windows. The main problem here however, is that setTimeout only run once and you will only trigger one of your calls if that happens to be the right time in 1 minute (60*1000) from you starting the scene.

    A lightweight scheduler, invoking events on a daily basis can look like below. The trick is to call setTimeout again after your call so that it will run the next day again - which I believe is the thinking here...

    The scene is "active" as long as there are timeouts/events waiting to execute or until you abort the scene (when all timers are canceled)

    Please login or register to see this code.

    Edit: added time parameter optionally being a function, i.e sunset/sunrise...

    Edit2: added optional interval parameter

    Edit3: fixed bug in bumping up the time

    Edit4: added messages making it easier to see what is scheduled and when

    P.S would be nice if Fibaro allowed load/loadstring from scenes

    Nice, The setTimeout makes a big difference. Have you tested with a few more events

    Link to comment
    Share on other sites

    • 0

    KristianO, robmac,

    Well, Kristian seem to have some strange problems.

    Kristian, your logs look ok compared to your scheduled commands. I tried to run the same schedule commands and it works for me

    Please login or register to see this image.

    /emoticons/default_icon_curve.gif" alt=":-/" /> The only thing I can currently imagine is that the catchup commands that run at startup are too close in time as to make some z-wave commands not reach the destination, like turning on and turning off 44 quickly - but that's tricky to debug... I will have another look at it tomorrow...

    robmac, no I haven't really run this seriously because I'm still running your scheduler...

    Link to comment
    Share on other sites

    • 0

    next edit add a parameter to sunset or sunrise for offset minutes + or - x

    catchup should be easy

    as long as we do not blow a limit on setTimeout it looks cool

    [ Added: 2015-01-18, 21:29 ]

    catchup may need to be global outside function and should it set to something on first run

    if it is an issue with too many too soon put a random short integer rather than 0.

    The 0 could also be the problem?

    [ Added: 2015-01-18, 21:31 ]

    also need delay at start to see if z-wave is running if a restart

    [ Added: 2015-01-18, 21:39 ]

    yep can see how that can do everything weekly, daily, certain days , yearly etc etc

    issue with sunset / sunrise ... it will be a day behind as it uses today's for tomorrow

    [ Added: 2015-01-18, 21:57 ]

    ooo

    what happens if the callback happens at the same time. How many can it support?

    Is that the issue

    [ Added: 2015-01-18, 22:07 ]

    or KristianO check the time on your system. is daylight saving ok?

    if not save your location again

    Link to comment
    Share on other sites

    • 0

    KristianO,

    Hi again, I've just added a debug message when the actual timeout is invoked ("Calling: ...") so if you test with that you will see if your provided calls are actually invoked by setTimeout. If they are there is something strange sending z-wave commands to the same device too close to each other and I will add a delay. If they are not invoked, there is a issue with setTimeout...

    I'm asking you to help me debug here because it seems to work for me

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

    What version are you running 4.030?

    robmac

    Thanks for the feedback, seems like it will become a real scheduler

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" /> will be back this evening with some more fixes then (Z-wave delay and sunset/sunrise issue)

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • jgab, Thanks alot for your help in this. I would like to hightlight that the schedular works very good so far. The light both at 10:30 yesterday and this morning at 7:00 was triggered fine. The only "flaw" i found were that when i pushed the "start" button in debug was that the light was flashing off and on. Not really a bug since the schedular is anyway working fine and the light should be on at that time.

    Im sorry if it was a missunderstanding.

    Link to comment
    Share on other sites

    • 0

    Yes looks good. Will create my vpn again and lend a hand if you want next week.

    Can add .net functions to schedule external control.

    If the bit that assumes 24 hr is changed to function that calculates based on active days then we can have weekend, weekday and any groups of days.

    Interval can be anything including year for anniversary and also one off for reminders

    A bit of leap year handling for monthly and yearly Feb 29ths.

    Schedule profiles is easy.

    Alarm clock can be done.

    I was looking at plugin but not happy with persistence so ut us is not complete.

    Link to comment
    Share on other sites

    • 0
    Guest David R

    Thanks for this I will try later. I'm a massive fan of fibaro but find it mad you have to put a script like this together just to do a schedule. Surely they could make it easier in block scenes to do this?

    Link to comment
    Share on other sites

    • 0
    jgab, Thanks alot for your help in this. I would like to hightlight that the schedular works very good so far. The light both at 10:30 yesterday and this morning at 7:00 was triggered fine. The only "flaw" i found were that when i pushed the "start" button in debug was that the light was flashing off and on. Not really a bug since the schedular is anyway working fine and the light should be on at that time.

    Im sorry if it was a missunderstanding.

    Yes, the "logic" is that events sheduled for times happening before the scene is invoked are run immediatly so that you arrive at the current "state"... I guess that sometimes that could be inconvenient (lights flickering).

    There is the possibility to add 'false' as the catchup parameter to the schedule function if it is neccessary and add code in the beginning of the scene to set up a known state.

    /J

    Link to comment
    Share on other sites

    • 0
    jgab, Thanks alot for your help in this. I would like to hightlight that the schedular works very good so far. The light both at 10:30 yesterday and this morning at 7:00 was triggered fine. The only "flaw" i found were that when i pushed the "start" button in debug was that the light was flashing off and on. Not really a bug since the schedular is anyway working fine and the light should be on at that time.

    Im sorry if it was a missunderstanding.

    Yes, the "logic" is that events sheduled for times happening before the scene is invoked are run immediatly so that you arrive at the current "state"... I guess that sometimes that could be inconvenient (lights flickering).

    There is the possibility to add 'false' as the catchup parameter to the schedule function if it is neccessary and add code in the beginning of the scene to set up a known state.

    /J

    In the scheduler I wrote only the last event is played back in catchup. Think this could be done here by holding table based on callbacks, ID, action. Each time new item scheduled check and cancel. Would need to delay catchup so all events loaded and unnecessary actions removed before catchup actions run

    Link to comment
    Share on other sites

    • 0

    In the scheduler I wrote only the last event is played back in catchup. Think this could be done here by holding table based on callbacks, ID, action. Each time new item scheduled check and cancel. Would need to delay catchup so all events loaded and unnecessary actions removed before catchup actions run

    Yes , only the last event of any given scheduled event is run. So if something is scheduled to run every 30min from 7.00 and the clock is 10.00 it will only be run once in the catchup and not 7 times...

    The general case with use cases spanning scheduled events is tricky. First there need to be an id connecting events that belong together, to only run the last (in this case if it is past 22.00)

    schedule(ID1, "07:00:00", function() fibaro:call(7,'turnOn') end)

    schedule(ID1, "22:00:00", function() fibaro:call(7,'turnOff') end)

    However, that will not work if people have more complicated events that affects things relative a previous state... Only running the last scheduled event (after 22.00) in the below case will not result in the state that the lamp being 81% on after 22.00.

    schedule(ID2, "07:00:00", function() turnOn(7) end)

    schedule(ID2, "18:00:00", function() dim(7,'-10%') end) -- dim lamp 7 with 10%

    schedule(ID2, "22:00:00", function() dim(7,'-10%') end) -- dim lamp 7 with 10% again

    Replaying all events and marking those events that should not be run as catchups seems to be the simplest model. For some simpler cases I guess one can add code in the beginning of the scene setting up the initial state of the devices affected and then mark all the schedule events as not catchup.

    On another note; it would be very cool to implement a cron/crontab format for the times/intervals

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I have run this for some days now and get a feeling that it stops working after a few days... Would it be possible to debug this?

    Link to comment
    Share on other sites

    • 0
    I have run this for some days now and get a feeling that it stops working after a few days... Would it be possible to debug this?

    After a few days?, that's tricky. Does the debug output say that it is scheduling the commands? I was planning to start to eat my own dogfood and run the scheduler but I didn't got around to do it this weekend. Will have a go at it this evening and see if I get the same behaviour.

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