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

yet another time check function


10der

Question

just share with you :)

 

the function was re-written by @jgab

 

* can work with pre-defined global values for example

"1.SunSet-5.SunRise" where SunSet and SunRise string time values like 16:00, 07:00

 

notice: notation 1.08:00-2.20:00 means Mo: 08:00-20:00 and Tu 08:00-20:00 and not mean from Mo 8:00 till Tu 20:00

Mo 8:00 till Tu 20:00 can decribed as 1.08:00-1.23:59, 2.00.00-2.20:00

Please login or register to see this code.

Hey, Fibaro, when you allow one created global functions/libs for using (including) in all scenes? :roll:

 

 

Edited by 10der
  • Thanks 1
Link to comment
Share on other sites

Recommended Posts

  • 1

I think it suffers from requiring day number for first time being lower than day number for second time. e.g.  print(between("6.07:00-3.12:00")) doesn't work so well.

Trick is to include day in seconds calculation...

Please login or register to see this code.

 

Edited by jgab
  • Thanks 1
Link to comment
Share on other sites

  • 0

Hmm, your last version still suffer from the bug that "6.00:00-1.00:00" is false on a Sunday (7) . You need to include day number in calculation...

I made a version of the function with a slightly different time format;

Please login or register to see this code.

Where days are 'Mon', 'Tue' etc. and globals/sunrise/sunset can be inserted.

Ex.

between("Mon.10:00 Tue.15:00, Wed.11:00 Sat:17:00") is true between 10 on Monday to 15  on Tuesday,  and 11 on Wednesday to 17 on Saturday

If sunrise is "07:33" and sunset is "18:45" then

between("Mon.$sunset Tue.$sunrise") is true between Mon 18:45 and Tue 07:33

between("Mon.$sunset-10 Tue.$sunrise+15") is true between Mon 18:35 and Tue 07:48

It works with globals too. If fibaro:setGlobal('test',"11:10") then

between("Mon.$sunset-10 Tue.$test+15") is true between Mon 18:35 and Tue 11:25

Disclaimer; no error checks if time format is wrong...

 

Edited by jgab
  • Like 1
Link to comment
Share on other sites

  • 0
  • Inquirer
  • 1 hour ago, jgab said:

    "6.00:00-1.00:00"

    Sir.

     

    1.06:00-3.20:00  mean:

    1.06:00-1.20:00

    2.06:00-2.20:00

    3.06:00-4.20:00

     

    and doesn't mean:

    from Mo 06:00 till  We 20:00

     

    thank you for your comment :)

    1 hour ago, jgab said:

    local dayMap = {Mon=2,Tue=3,Wed=4,Thu=5,Fri=6,Sat=7,Sun=1}

    Mmm! Cool! 

    your are a professional. It's really comfortable!

    Link to comment
    Share on other sites

    • 0

    Ok, then I misunderstood you, thought the idea was to have intervals spanning days...

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Just now, jgab said:

    hought the idea was to have intervals spanning days...

    but your idea OK... I don't know how to mix your idea with spanning days and my idea :(

    with easy notation

    btw, your last fix fail with simple test-case

     
    print(between("10:00-22:00"))
    Link to comment
    Share on other sites

    • 0

    I've changed the time format, it's "10:00 22:00", not "10:00-22:00". To allow !$sunrise-10 $sunset+10"...

    • Thanks 1
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 25 minutes ago, jgab said:

    I've changed the time format, it's "10:00 22:00", not "10:00-22:00". To allow !$sunrise-10 $sunset+10"...

    nice. probably we can use .. :)

     

    10:30..21:00 :)

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • btw. what do you think about 

    local dayMap = {Mo=2,Tu=3,We=4,Th=5,Fr=6,Sa=7,Su=1}
    local k, v = next(dayMap); local wlen = string.len(k)
     
    Edited by 10der
    Link to comment
    Share on other sites

    • 0
    11 minutes ago, 10der said:

    btw. what do you think about 

    local dayMap = {Mo=2,Tu=3,We=4,Th=5,Fr=6,Sa=7,Su=1}
    local k, v = next(dayMap); local wlen = string.len(k)
     

    I'm missing something, what are you trying to achieve? code missing?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Please login or register to see this code.

    Please login or register to see this code.

     

    your code

     

    I am proposing: 

     

    local dayMap = {Mo=2,Tu=3,We=4,Th=5,Fr=6,Sa=7,Su=1}

     

    or national localization

    local dayMap = {lun=2,mar=3,mer=4,jeu=5,ven=6,sam=7,dim=1}

    etc

    local k, v = next(dayMap); local wlen = string.len(k)

    and 

     

    d1 = d1 == "" and dt.wday or dayMap[d1:sub(1,wlen)]
    d2 = d2 == "" and d1 or dayMap[d2:sub(1,wlen)]
     


     

     

     

    Edited by 10der
    Link to comment
    Share on other sites

    • 0
    11 minutes ago, 10der said:

    Please login or register to see this code.

    Please login or register to see this code.

     

    your code

     

    I am proposing: 

     

    local dayMap = {Mo=2,Tu=3,We=4,Th=5,Fr=6,Sa=7,Su=1}

     

    or national localization

    local dayMap = {lun=2,mar=3,mer=4,jeu=5,ven=6,sam=7,dim=1}

    etc

     

    and 

     

    d1 = d1 == "" and dt.wday or dayMap[d1:sub(1,wlen)]
    d2 = d2 == "" and d1 or dayMap[d2:sub(1,wlen)]
     


    Ah,  I understand. Actually, I was a bit sloppy. It's only to remove the last char (the '.') at d1 and d2. No need to know the length. Then you can use whatever day names as long as they are letters..

    Please login or register to see this code.

     

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 1 minute ago, jgab said:

     

    wow! 

    Interesting.

    is Fibaro support Cyrillic chars here? so, %a in match 

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    Well, some tweaking of the pattern to exclude the '.' makes it even simpler

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0

    Other alphabets are always tricky. This works for Swedish;

    Please login or register to see this code.

    Remember you need also to change in the row where start and stop is matched.

    This is the version that excludes the '.' from the match..

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • wow. umlaut is supported.  That is good to know.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @jgab do you know, is applicable here %S ?

     

     

    local start = "вівторок.10:00"
    local d1, h1, m1 = start:match("^([%S%a%.]-)(%d+):(%d+)$")
    print(d1, h1, m1)
     

    Please login or register to see this image.

     

    works for me (Cyrillic support)

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • some fixes

     

    Please login or register to see this code.

    idk, but we should check o and p value on '' otherwise  

    doesn't work

    print(between("Mo.$sunset Su.$sunrise"))

    also we can use more friendly syntaxis 

     

    print(between("Mo.$sunset..Su.$sunrise"))

     

    Please login or register to see this code.

     

     

    Edited by 10der
    Link to comment
    Share on other sites

    • 0

    Ok, easier to do

    Please login or register to see this code.

     

    • Thanks 1
    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...