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


Question

Posted

Hi,

Why can't this be managed?

Please login or register to see this code.

Recommended Posts

  • 0
  • Inquirer
  • Posted

    The debug seems to be spamed with schedules. It seems like the last schedule is causing it.

    I re-run the scene at 19:46 (Since it stopped). It run fine and scheduled correctly.

    but then after the last sch. event of the day it keep re-schedule the same time (22:30).

    [DEBUG] 19:46:30: Catchup Living room Window/On at Sun Jan 25 19:46:30 2015

    [DEBUG] 19:46:30: Catchup Bedroom Blind/Open at Sun Jan 25 19:46:30 2015

    [DEBUG] 19:46:30: Catchup Bedroom Light/On at Sun Jan 25 19:46:30 2015

    [DEBUG] 19:46:30: Catchup Bedroom Prepare at Sun Jan 25 19:46:30 2015

    [DEBUG] 19:46:30: Scheduled Night time at Sun Jan 25 22:30:00 2015

    [DEBUG] 19:46:30: Scheduled Living room Window/On at Mon Jan 26 07:00:00 2015

    [DEBUG] 19:46:30: Scheduled Bedroom Blind/Open at Mon Jan 26 10:00:00 2015

    [DEBUG] 19:46:30: Scheduled Bedroom Light/On at Mon Jan 26 16:00:00 2015

    [DEBUG] 19:46:30: Scheduled Bedroom Prepare at Mon Jan 26 18:30:00 2015

    [DEBUG] 22:30:00: Scheduled Night time at Sun Jan 25 22:30:00 2015

    [DEBUG] 22:30:00: Scheduled Night time at Sun Jan 25 22:30:00 2015

    [DEBUG] 22:30:00: Scheduled Night time at Sun Jan 25 22:30:00 2015

    [DEBUG] 22:30:00: Scheduled Night time at Sun Jan 25 22:30:00 2015

    ...There are even more (38 times to be precis)

    Then it passes 7:00 AM and then same thing happens.

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

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

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

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

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

    ...

    And the same thing at 10 AM.

    • 0
    Posted

    Ok, it seems like an ordinary bug in the code. It could work if you change the line with

    Please login or register to see this code.

    to

    Please login or register to see this code.

    I will have another look at it tonight if there is something else I missed and also test the code myself too

    Please login or register to see this image.

    /emoticons/default_icon_curve.gif" alt=":-/" />

    • 0
  • Inquirer
  • Posted

    I did this update but still it seems only to work in the "beginning". After a while it stopps from working. Even though i can see the scheduled event in the log.

    Eg. Today none of the schedules triggered.

    • 0
    Posted

    KristianO,

    Ok, you see "Scheduled" in the log - can you see the "Calling:" also?

    I started to run it myself this morning and so far it performs ok, will see tonight and tomorrow morning if it starts to misbehave for me too.

    • 0
  • Inquirer
  • Posted

    It seems like i did not have the latest version (icl the calling part). So i have now impl. the latest and re-scheduled. Lets try again.

    • 0
    Posted

    KristianO,

    Ok, I've been running the code for 3 days now and it is turning off and on stuff morning, evening and nights - so it seems to be performing like a clock. I made some simplifications (less is more) and the code I use now is included below (incl. my schedules)

    Please login or register to see this code.

    Still the limitations that the sunrise is typically one day behind (not a big deal for me) and that you need to sort the schedules chronological to assure the the catchup logic makes sense.

    It is possible to create a helper function like below to make a more compact format (same schedules as above) and to make sure that the events are sorted chronologically.

    Please login or register to see this code.

    It is kind of cool to have a scheduler that is a function because then you can easily schedule events that schedule events... I'll be coming back on that

    Please login or register to see this image.

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

    • 0
    Posted

    Looking good. I like the schedule a schedule. You should be able to schedule sunrise sunset events to set events at correct time

    Please login or register to see this image.

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

    • 0
    Posted
    Looking good. I like the schedule a schedule. You should be able to schedule sunrise sunset events to set events at correct time

    Please login or register to see this image.

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

    Yep, that's my plan

    Please login or register to see this image.

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

    • 0
    Posted

    Nice. And for weekend only and day sets? Make diff a function and add some parameters?

    [ Added: 2015-01-30, 22:35 ]

    If you make a table of functions indexed by your 'set' and other words you can avoid the if else ... In your function.

    [ Added: 2015-01-30, 22:40 ]

    Fibarolookup and fibmap in scheduler Lift it if it helps

    • 0
    Posted

    Hi guys,

    I've made a simple scene that update variable base on time of the day but as the time pass, the variable not updating unless I manually run this scene.

    Any idea how to update the variable automatically using the scene like this without having to click Run manually everything?

    Thank you.

    Please login or register to see this code.

    • 0
    Posted

    tuanha,

    You need to wrap that code in a loop to continuously check the time.. or use the scheduler in the previous post like this:

    (bit unsure if I understood your time intervals correctly)

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted

    jgab, Run it for 2 days now. Works perfectly! Thanks a lot!

    • 0
    Posted
    jgab, Run it for 2 days now. Works perfectly! Thanks a lot!

    Thank you so much jgab, it's a surprise that you spend time write the whole code for me. Work like a champ now xD

    • 0
    Posted

    Ok, here is an attempt to schedule schedules... I believe it is working but I don't encourage anyone to really use it

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" /> ... The idea is just to show that if you have schedule as a LUA function instead of a scene/sleep you can make some interesting functional compositions and achieve quite compact code. The example below allows schedules for weekdays and solves the problem in my previous attempt with sunrise being a day off.

    Please login or register to see this code.

    • 0
    Posted

    @jgab first of all, great code! But I have a question how to modified it. For example. I have some scenes that they are not supposed to start when I'm not in home. So I add some "if" condition. But it doesn't work. The "if" condition works only on condition which recognize at exact moment when scene is starting.

     

    I've got some globals variables like "I'm in home" or "I'm on vacation".

     

    I don't know if you understand me, so I paste my code below:


    Please login or register to see this code.

    • 0
    Posted

    Ok, I have been running my old scheduler for a year now and it has served its purpose. A bit bored yesterday (local Eurovision song contest final in Sweden) and thought about something more flexible and made a "cron" clone. I have tried it for a day and it seems to work (there is a run_debug version of the scheduler that helps you to see if it fires correctly for the next simulated 48 hours...)

    Minimal error checks on input. Hack for sunset/sunrise using the second field for offset. Like cron it wakes up every minute and check the rules.

    Please login or register to see this code.

    Edit: of course needed to schedule a rule to patch the sunset/sunrise once a day...

    • 0
  • Inquirer
  • Posted

    Sweet! Can you please explain the possibilities with the scheduling?

    E.g 0/15 1/2 9-17 3-5 mon-fri

    • 0
    Posted

    It’s very similar to standard crontab syntax and most tutorials out there should give an idea how to use it. Here is a short explanation.

     

    Fields

    "<minute> <hour> <day> <month> <weekday>"

     

    <minute> -> 0 .. 59

    <hour> -> 0 .. 23

    <day> -> 1 .. 31

    <month> -> 1 .. 12

    <weekday> -> 1 .. 7 , Sunday=1 and Saturday=7

     

    Same numerical values you get from Lua's os.date("*t")..

     

    Three letter lowercase abbreviation allowed for days and months. Ex ‘sun,mon,tue,..’ ‘jan,feb,mar,…’

     

    *          -> matches any value for that field. Ex. ‘*’ in minute field matches 0 .. 59

    x,y,z   -> matches the listed values for that field. Ex. ‘1,20’ in month field matches the 1st and the 20th day of month

    x-y     -> matches values in interval for that field. Ex. ‘sun-sat’ in week day field matches 1,2,3,4,5,6,7

    x/y     -> matches  values starting at x with y increments. Ex. ‘0/1’ matches ‘0,1,2,3,4,5,…    ‘0/15’ matches 0,15,30,45   ‘1/2’ matches ‘1,3,5,7…’

     

    Combination allowed like ‘mon-fri,sun’ for every day except Saturday.

     

    Minute field can be replaced with ‘SUNRISE’ or ‘SUNSET’ and hour field with offset in minutes.

    Ex. “SUNRISE -10 * * mon-fri” means sunrise-10min weekdays (Monday to Friday)

           “SUNSET 15 * * sat-sun” means sunset+10min every Saturday and Sunday

     

    Examples

     

    --At minute 0, on hour 0, every day (‘*’) and every month(‘*’) and every weekday (‘*’)

    add("0 0 * * *",function() print("Hello") end, "At midnight")

     

    --At minute 10, every hour (‘*’), every day (‘*’) and every month(‘*’), but only on Saurdays

    add("10 * * * sat",function() print("Hello") end, "10 past every hour")

     

    -- Every 5 minutes starting at minute 0 (‘0/5’), every even hour starting at 0 (‘0/2’) the 9th,13th and 14th  (‘9,13,14’) of month March (‘3’), but only if these days happens on a Saturday,Sunday,Monday or Tuesday. <day> and <weekday> field both have to be true.

    add("0/5 0/2 9,13,14 3 sat-tue",function() print("Hello") end, "Every 5min every even hours")

     

    -- Every 15 minutes (‘0/15’), every odd hour starting at 5 (‘5/2’) , day 9 to 17th of the month (‘9-17’), March to May (‘3-5’) and Monday to Friday (‘mon-fri’)

    add("0/15 5/2 9-17 3-5 mon-fri",function() print("Hello again") end, "Every quarter odd hours")

     

    -- 10 minutes past sunset (‘SUNSET 10’) on odd days (‘1/2’)  on even months (‘0/2), but only weekdays Monday to Friday (‘mon-fri’)

    add("SUNSET 10 1/2 0/2 mon-fri",function() print("Hello!") end, "Sunset+10")

     

    -- 15 minutes before sunrise (‘SUNRISE -15’) on the first of every month  

    add("SUNRISE -15 1 * *",function() print("Goodbye!") end, "Sunrise-15")

    • 0
  • Inquirer
  • Posted

    Cool! I will try it!

     

    So turn On a light at 15 before sunset and turn off the same at 21:00?

     

    add("SUNSET -15 * * *",function() fibaro:call(246, "turnOn") end, "Device 246 On at Sunset-15")

    add("* 21 * * *",function() fibaro:call(246, "turnOff") end, "Device 246 Off at 21:00:00")
     
    It would be nice the have a log that only states what just happend and what is scheduled to be the next.
     
    Eg:
     
    DEBUG: 17:09:00 Device 246 On at Sunset-15 scheduled at - 18:10:00 
    • 0
    Posted

    Yes, those rules would accomplish that.

    The "documentation string" given to the add function will be printed/logged when the rule is activated. Unfortunately, the "simplistic" way cron works (check rules every minute) makes predictions about when next event is going to happen difficult. The benefit is that it allows to add support for including state of global variables in the rules too, like "@HOME=TRUE" or something like that... 

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