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

Calculating Times...


Question

Posted

Dear all,

 

I want to develop a customized presence simulation for my home. Therefore, I need to calculate with hours, but I don't know how to manage that in LUA. ;-)

 

Could someone help me, please?

 

I want to randomize a number between 1 and 10 and add this as minutes to the official SunseHour (For example 19:34 + 7 = 19:41). I tried that:

 

Please login or register to see this code.

That doesn't work..... could someone help me, please?

 

Thank you so much for your answer.

 

Dan

11 answers to this question

Recommended Posts

  • 0
Posted

Please login or register to see this code.

Please login or register to see this code.

 

  • Like 1
  • 0
  • Inquirer
  • Posted

    Great, thank you very much!!

    • 0
  • Inquirer
  • Posted (edited)

    @jgab One more question.... based on your script, I generated the "randomized" global variables Presence_Sunrise and Presence-Sunset.

     

    In another script, I want to show this times and add a number of minutes (here called "Fernsehzeit") to this time and print it again. Unfortunately, that doesn't work because it seems that it's not saved as time-value. 

     

    Please login or register to see this code.

    Could you probably help me how I can fix that? Thank you so much!!

     

     

    And I also want to add a auto-start trigger, when the global saved Start-Presence time is reached....

     

    Please login or register to see this code.

    How can I use here the Presence_Sunset-Variable as trigger when it should start?

     

    Thank you so much!!

     

    Dan

    Edited by Danielos
    • 0
    Posted (edited)

    Ok, this is an alternative...

    Please login or register to see this code.

    str2sec(<time-value>) converts a time-value string to number of seconds. This allow you to do arithmetic with the values

    sec2str(<number of seconds>) converts back to time-value string

     

    To generate your two sunset/sunrise values

    Please login or register to see this code.

     

    Then in the other script

    Please login or register to see this code.

    Ok?

    Edited by jgab
    • Thanks 1
    • 0
    Posted

    The answer to your second question, how to trigger on the values, I recommend a previous post on the topic

    The tonumber() function there is similar to the str2sec() function presented here. 

    The scene runs every minute and you can do tests inside the clock() function to see if its's time to do actions

     

     

    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Thank you, @jgab. Your code is much more complicated than mine - I see the difference between a newbie and a pro. :-) Thanks for your help!

     

    Now, one last part is missing for my script based on your suggestions.  I "overwrited" the official sunset time as demo. In this case with 10:15. And I want to check minutely, if the current time is 10:15 and if yes, print a text. Unfortunately, none of my 3 syntax worked.... ;-) I'm sure it's only a little mistake...but I can't find it. Any ideas?

     

    Please login or register to see this code.

     

    • 0
    Posted (edited)

    Ok, the clock() function is called every minute with the arguments you need

    Ex. clock("15:01","06:00","20:0","Monday",false)

    Where 15:01 is the current time (updates every minute of course), 06:00 and 20:00 is the sunrise and sunset values (changes every midnight), day is a string with the day name according to the table provided, and false is false if it is a weekday otherwise true.

    That means that if you have your values in time-value strings like you have in your globals you can just test with equality (==)

    I also recommend you to do fibaro:getGlobalValue("Presence_Sunset")==time so the loop will start to test against the new Presence_Sunset if you updates its value.

    I also think you should skip the addTime and use the sec2str and str2sec in my last example. str2sec is the same as the 'tonumber' provided in the code.

    I added a log in the beginning of clock to print the values.

    Please login or register to see this code.

     

    Have you tried to run it in my emulator? The _System.speed(true) in the beginning of the scene runs the scene quicker than realtime so you can see if you trigger at the right times without having to wait hours...

     

     

    Edited by jgab
    • Thanks 1
    • 0
  • Inquirer
  • Posted

    @jgab you're brillant!! Thank you very much, now everything works as I wanted it.... and I learnt a lot I can reuse for other scripts. Thank you so much!

    • 0
  • Inquirer
  • Posted

    @jgab: The scene works brillant... but because of the large time difference of the sunset time in summer time compared to winter time, the function isn't perfect at the moment. Therefore, I want to calculate the start better. Unfortunately, I still don't feel me comfortable with the time calculating functions. Could you help me how the string is for that case:

     

    1. A calcuation, how many minute are between 22:00 and the official sunset time (example: if the sunset time is 20:30, the time difference until 22:00 is 90 minutes)

     

    Thank you very much in advance.

    • 0
    Posted
    7 minutes ago, Danielos said:

    @jgab: The scene works brillant... but because of the large time difference of the sunset time in summer time compared to winter time, the function isn't perfect at the moment. Therefore, I want to calculate the start better. Unfortunately, I still don't feel me comfortable with the time calculating functions. Could you help me how the string is for that case:

     

    1. A calcuation, how many minute are between 22:00 and the official sunset time (example: if the sunset time is 20:30, the time difference until 22:00 is 90 minutes)

     

    Thank you very much in advance.

     

    Please login or register to see this code.

    gives you the minutes between sunset and 22:00 (if you run it inside the clock function the variable 'sunset' is set to the sunset time).

    If sunset happens after 22:00 (as it does in Scandinavia) it will return a negative value (you can use math.abs() to get the absolute value.

    • Thanks 1
    • 0
    Posted

    Hi Can anyone help me please? I am trying to calculate a time as follows, I need split the day in to 12 hours depeding on sunrise and sunset time and then calculate one and a half of these hours before sunset.

    So far I have the following:

     

    My Scene at the moment:

     

    local fmt = string.format

    function str2sec(str) local h,m,s=str:match("^(%d%d):(%d%d):?(%d*)") return 3600*h+60*m+(s~="" and s or 0) end

    function sec2str(sec) return fmt("%02d:%02d%s",math.floor(sec/3600),math.floor((sec % 3600)/60),sec % 60 > 0 and fmt(":%02d",sec%60) or "") end

     

    local SunRiseTime = str2sec("07:06")

    local SunSetTime = str2sec("17:31")

    local Plag = sec2str(SunSetTime - (string.format("%.0f",(((SunSetTime - SunRiseTime) /12) * 1.25)))) -- "16:25:54"

     

    Question:

    How can i round the results to only minutes and round up at 30 seconds?

    In the above example I would want to get "16:26"

     

    Thank you for any help provided

     

     

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