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

    Sorry to say but im back with the old scheduler.

     

    I just want a scheduler that works with sunrise/sunset, Fixed times.

     

     

    Would it be possible to add to this scheduler:

     

    - Conditions of global variables

    - Conditions for weekday/weekend

    - Recouring events

     

    Like:

     

    schedule(hour( "21:00", *), "Guest Window light Off", function() fibaro:call(246, "turnOff") end); -- All the time at 21:00

    schedule(days("Weekdays", "21:00", Varible1 = 'Yes'), "Guest Window light Off", function() fibaro:call(246, "turnOff") end); -- All Weekdays at 21:00 and globalvarible = Yes

    schedule(days("Weekdays", "21:00", *), "Guest Window light Off", function() fibaro:call(246, "turnOff") end); -- All weekdays at 21

    schedule(Intervall( "21:00", 0/15), "Send weather info", function() fibaro:call(246, "pressButton", "6") end); -- Starting at 21:00 and then every 15 min

    • 0
    Posted (edited)

    Something like this?

    Please login or register to see this code.

     

    EDIT  Sep26: fixed error in sunset patch rule
     

    In the last rule I added that it should stop 23 (e.g. 23:45), if you specify "0/15 21" it will translate to 21.00, 21.15, 21.30, 21.45 and stop.

     

    SUNSET/SUNRISE is now @sunset/@sunrise

     

    run(true) runs the scheduler in simulated debug mode (to see if the rules fires correctly). run() runs the scheduler in normal mode...

     

    Short doc:

    add(<time/date-string>,<additional test>,<action>,<optional doc string>,<optional catchup>)

    <additional test> -> Lua function or "struct expr" used as additional test if action should fire
    <action> -> Lua function or "struct expr" containing the action
    <optional doc string> -> Log string logged when action fires
    <optional catchup> -> 'true' if rule should be run once at startup (i.e. initialising, turning off lamps etc)
     
    <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")..
     
    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+15min every Saturday and Sunday
           
    Examples
     
    At minute 0, on hour 0, every day (‘*’) and every month(‘*’) and every weekday (‘*’)
    add("0 0 * * *",true,function() print("Hello") end, "At midnight")
     
    At minute 10, every hour (‘*’), every day (‘*’) and every month(‘*’), but only on Saurdays
    add("10 * * * sat",true,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",true,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",true,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",true,function() print("Hello!") end, "Sunset+10")
     
    15 minutes before sunrise (‘@sunrise -15’) on the first of every month  
    add("@sunrise -15 1 * *",true,function() print("Goodbye!") end, "Sunrise-15")
     
    Turn on light 34 and 36 every day at 21:00 but only if the global variable 'Home' is equal to 'Yes'
    add("0 21 * * *",{'eq',{'get','Home'},'Yes'}, {'turnOn',34,36}, "Turning on lights at 21 if people are at home")
    Edited by jgab
    Added dim and push
    • Thanks 1
    • 0
  • Inquirer
  • Posted

    This is stunning! Are you for real?

     

    I added these 2 lines for test (at 23:18, so past sunset and time for turning Off), but what says the log?

     

    add("@sunset 0 * * *",false,{'turnOn',246},"Guest Window light On") -- All days at sunset

    add("* 21 * * *",false,{'turnOff', 246}, "Guest Window light Off") -- All days at 21:00
     
    Log:

    [DEBUG] 23:15:34: Waiting for Z-Wave engine to start

     

    [DEBUG] 23:15:44: Aligning scheduler to 15s past next minute...

     

     

    [DEBUG] 23:16:15: Starting scheduler

     

    [DEBUG] 23:16:15: Sunset today:18:16

    [DEBUG] 23:16:15: Sunrise today:06:25

    [DEBUG] 23:16:15: Run 1/0/16/3/4:Updating sunset/sunrise

    [DEBUG] 23:16:15: Run 1/0/17/3/5:Updating sunset/sunrise

     

     

     

    It would be nice to see more what is planned in the schedule and when?

     

     

     

     
    • 0
    Posted

    Almost there!

    Please login or register to see this image.

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

     

    There was a bug in the eval function, seems like cut/paste sometimes changes the quote characters

    Please login or register to see this image.

    /emoticons/default_icon_curve.gif" alt=":-/" /> Edit it in the previous post.

    Your last rule will fire every minute during 21 as you have a * in the first minute field, If you want it to fire only 21:00 it should be "0 21 * * *". Unix crontab format takes some getting used to...

     

    The first argument to add(...) is the "cron time string". The second argument is an extra test. I had set it to false in your rules when I was testing it which was the second bug (fixed it now in the previous post). It should be 'true'. This test can be used for checking states of variables or other lights etc and has to be true together with the cron-time for the action to fire. The third argument is the 'action', like turnOff light etc...

     

    If I changed your rules to 

    add("@sunset 0 * * *",true,{'turnOn',246},"Guest Window light On") -- All days at sunset

    add("0 21 * * *",true,{'turnOff', 246}, "Guest Window light Off") -- All days at 21:00
     
    and run(true) I get this log:
     

    Aligning scheduler to 15s past next minute...

    Starting scheduler
    Sunset today:20:05  -- When I run in my environment these values are simulated, but it should be correct...
    Sunrise today:08:20
    Run 5/20/16/3/4:Guest Window light On -- min=5, hour=20, day=16, month=3, weekday=4, your first rule fire (sunset+0)
    Run 0/21/16/3/4:Guest Window light Off -- min=0, hour=21, day=16, month=3, weekday=4, your second rule fire (light off)
    Run 1/0/17/3/5:Updating sunset/sunrise -- Internal scheduler rule updating the sunset/sunrise values, always run 1 min past midnight
    Run 5/20/17/3/5:Guest Window light On -- min=5, hour=20, day=17, month=3, weekday=4, your first rule fire (sunset+0)
    Run 0/21/17/3/5:Guest Window light Off -- min=0, hour=21, day=17, month=3, weekday=4, your second rule fire (light off)
    Run 1/0/18/3/6:Updating sunset/sunrise -- Internal scheduler rule updating the sunset/sunrise values, always run 1 min past midnight
     
    ...and it runs for 48 hours and stop. run(false) or run() starts the real scheduler.
    I may spend a little bit more time on the "struct-exp" functions and put together some documentation on it... 
    • 0
  • Inquirer
  • Posted

    Thanks!

     

    Seems to be ok!

     

    I will test it even further.

     

    Would It be possible to locate a time from a global variable?

     

    eg: TimeVariable with value "21:00:00" or similar. (Wake up time)

     

    Running in simulation I run in run(true) and all lines with true are simulated?

     

    Run it for "real" then all schedule lines should be false and execution with run(false) or run()?

     

    Do I then need to update the sunrise/sunset line to, changing true to false?

    add("1 0 * * *",true, patchrules, "Updating sunset/sunrise",true)

     

    Kristian

    • 0
    Posted

    Hi,

    let me come back on the TimeVariable... I have some ideas how it could be solved.

    With run(true) all lines/rules are simulated, e.g. the actions are not run but the the rule is logged...

     

    Now, for "real" you just do run(). The false/true you refer to in the lines/rules are an extra test that together with the time has to be true for the action to run.

    If you set that to false in a line/rule it will never be triggered (a way to disable the rule).
    The syntax for add is;

    add(<time string ex "0 21 * * *">,<test that also needs to be true -  LUA function or struct-expr>,<action LUA function or struct-expr>,<documentation string used at logging>)

     

    The rule you refer to: (add("1 0 * * *",true, patchrules, "Updating sunset/sunrise",true)) is an internal rule the scheduler use to update some internal data...

     

    If you have a rule that should fire at 21:00 only if you are home and you have a global variable "Home" that is set to "Yes" when you are home, you can create this rule:

    add("0 21 * * *",{'eq',{'get','Home'},'Yes'}, {'turnOn',34,36}, "Turning on lights at 21 if people are at home")

     

    Instead of 'true' in this case the test argument is {'eq',{'get','Home'},'Yes'}} to test if the variable is equal to 'Yes' and the action is  {'turnOn',34,36} (turnOn can take more than one id..). So when the time field is true (it is "0 21") and the test is true (Home=='Yes') the action is triggered and lamp 34 and 36 is turned on...

     

    ​Edit: fixed an extra parenthesis in the add example

    • 0
  • Inquirer
  • Posted

    I guess the catch-up is not implemented? Started the schedular at 18:56 and was expecting my light above to turn on. This since sunset is at 18:17 and fixed of at 21:00.

    • 0
    Posted

    Catchup only run the rule directly no matter of the time/test which is not a real catchup because it triggers even if the time has past. The concept of catchup is really tricky and is not easily solved in the general case. In UNIX/crontab there is no concept of catchup, and I think that the way of think about it is that the scheduler automates things from when you start it. If you want some "initial state" just add lua code in the beginning to set up stuff the way you like it.

     

    I made some modifications to the struct-expressions evaluator in the last post (there were also some bugs) and will come back with some more interesting rules that can be done now.

    • 0
    Posted

    I have a light in the kitchen (id 225) and a motion sensor (id 137) and sometimes someone (typically the kids) goes up in the night and turn on the light and forgets it. With this rule, between 23-05 and the light is on and if there is no motion for 5 minutes the light is turned off.

    There are many solutions of automating lights in this fora but it shows that some stuff can be done with a scheduler and a test. And it is very flexible on time/day constraints.

    {'last', ID} returns the 'lastBreached' value minus os.time(), e.g. number of seconds since the last breach.

     

    add("* 23-5 * * *",{'and',{'isOn',225},{'>',{'last',137},60*5}},{'turnOff',225},"Turning off kitchen light after 5min between 23 and 05")

     
    The scheduler is not suitable for triggering events like turning on lights triggered by motion sensors as it is only run once a minute. But I have some ideas about a more general event trigger script to complement the scheduler. A scheduler like UNIX/cron is really for housekeeping/cleaning up task, like turning on lights and turning off lights depending on general schedules or house rules. Stuff that needs to happen immediately due to occasional events or combination of events (sensors) are more suitable for another model/script.
    • 0
    Posted

    Thx @jgab! I have been using @robmac scheduler in page 1 of this thread for years (very reliable!), now moved to yours a few posts above to have some more flexibility. One edit/bugfix though: line 10 of the parserules function has a typo:

     

    Please login or register to see this code.

     should be:

    Please login or register to see this code.

     

    Thx again!

     

    Rene.

    • 0
    Posted
    On 1/2/2021 at 9:35 PM, ReneNL said:

    Thx @jgab! I have been using @robmac scheduler in page 1 of this thread for years (very reliable!), now moved to yours a few posts above to have some more flexibility. One edit/bugfix though: line 10 of the parserules function has a typo:

     

    Please login or register to see this code.

     should be:

    Please login or register to see this code.

     

    Thx again!

     

    Rene.

    Thanks!

    This is really old code. I moved to EventRunner for my rules.

    However, if you understand this scheduler and it works for you be my guest :-)

     

    • 0
    Posted
    6 hours ago, jgab said:

    Thanks!

    This is really old code. I moved to EventRunner for my rules.

    However, if you understand this scheduler and it works for you be my guest :-)

     

    Thx jgab, I am reading through your docs on eventrunner today: impressive! I'll give it a try when I find some time:-)

     

     

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