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

Time of Day global variable - SIMPLE LUA


U5tabil

Question

Thanks to petergebruers for helping making this "Time of day" LUA scene variable.

This is probably common to experienced LUA coders but is a nice and easy LUA code for beginners.

Instead of having many scenes explaining each "Time_of_Day" variable, one can have all in one simple code.

All you have to do is make global variable (change names if you like).

Time_of_Day

-Morning (i set morning for sunrise)

-Afternoon (at 12:00 change if you like)

-Evening (at sunset)

-Night (at 22:00)

Please login or register to see this code.

  • Like 1
Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 1
Guest Charly

The code could be extremely shortened by removing all the currentDate.wday if you use all day 1 to 7 you do not need to check.

Your code looks a lot cleaner and every if will take processor time.

Link to comment
Share on other sites

  • 0

I'll look into it today. It needs cleaning, I see you added different and similar pieces of code. And I think you overlooked this:

Please login or register to see this code.

Code is excuted from top to bottom. So the first checks will work, but after the checks the scene jumps back to the "while true" above it. So the code below the first loop is unreachable, the "do second checks" never runs.

Put everything in one "while true" construct and keep only one sleep (and make it 50 seconds)

Link to comment
Share on other sites

  • 0
  • Inquirer
  • OK. Cause the "while true" is just from when i made boxes of it then converted to lua. So i kept it.

    But you mean the code should be like this maybe? or am i missing something?

    Please login or register to see this code.

    [ Added: 2015-01-07, 11:40 ]

    Only thing is that each scene has this after the "end" of it. I don't know what this piece of code do actually.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    Hi! I'm checking and "pimping" your code... You are a fast learner I see

    Please login or register to see this image.

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

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi! I'm checking and "pimping" your code... You are a fast learner I see

    Please login or register to see this image.

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

    Thank you. Im so green with this code stuff. But im really trying to understand it. I want to be part of this. So i can also help others in future.

    But one thing is for sure, after iv changed and use LUA only instead of those stupid boxes i have less problems/bugs with the system. (except for GPS and Netatmo not working as it should now)

    I look forward to see your version

    Please login or register to see this image.

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

    Strange nobody made a proper LUA code for this before? Would guess lots of people use this type of global variable and want it all in 1 code.

    Link to comment
    Share on other sites

    • 0

    Ah, you have reached the lua-forever-point-of-no-return

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    Here's the code. I tested it without actually trying different global variables... But I think that should be no problem with de debugging in place. Have fun!

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thank you so much!

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" /> This looks really good. Now i understand more aswell.

    You put this debug under night

    Please login or register to see this code.

    What you mean with this?

    Please login or register to see this image.

    /emoticons/default_icon_razz.gif" alt=":-P" /> Like if current time does not active anything like a hole in time or something?

    Please login or register to see this image.

    /emoticons/default_icon_razz.gif" alt=":-P" />

    Does it have something to do with the debug in top?

    Please login or register to see this code.

    And why have it in the bottom and not in top any reason for that?

    Anyway thanks i will update this thread for other to copy/use

    Please login or register to see this image.

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

    [ Added: 2015-01-07, 16:31 ]

    I was trying to put in that evening started 60min after sunset instead, see if it was working but im not getting it to work

    Please login or register to see this image.

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

    Now the code is

    Please login or register to see this code.

    And this make Evening be at sunset.

    So by changing to this it should have worked right?

    Please login or register to see this code.

    But its not. your debug says:

    [DEBUG] 16:30:32: Time_of_Day: Evening

    [DEBUG] 16:30:32: Current time doesn't match any scenario

    Please login or register to see this image.

    /emoticons/default_icon_mrgreen.gif" alt=":mrgreen:" />

    Link to comment
    Share on other sites

    • 0

    U5tabil, Have a look at the code, it requires patience actually. It will write to global variable (and tell you it is running a particular scenario - to answer your other question) when one of the == scenario conditions is met (sunrise, noon, sunset, 22:00). So the debug will say whatever you set your global variable initially until one of those conditions is met. Then it will read the new value of the global variable for you (correct one) untill another condition is met (and new value written to GV).

    or something along those lines

    Please login or register to see this image.

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

    Link to comment
    Share on other sites

    • 0

    I am also working on integrating the maintenance of this variable in the Smart House status VD. Will be released when the eventpanel works again... As I can not finish it now due to this bug.

    Link to comment
    Share on other sites

    • 0
    Guest fat

    I am getting an error of "Current time doesn't match any scenario" and time is 15:22

    I am also seeing time of day string as morning. I am a complete n00b with Lua so it is probably something I am doing wrong

    Link to comment
    Share on other sites

    • 0

    I am getting an error of "Current time doesn't match any scenario" and time is 15:22

    I am also seeing time of day string as morning. I am a complete n00b with Lua so it is probably something I am doing wrong

     

    Refer to lukaszpiech's post above and you should understand. In short, the current time need to be exact match of the parameter (eg. sunrise time), else it will always return debug message as not match any scenario.

    Link to comment
    Share on other sites

    • 0

    Thanks for this script love it but i do have a question. im trying to change the sunrise to an hour before sunrise. i was looking at block then converting it to LUA but looks different so i cant quite work it out this is the 

     

    os.time()+60*60) == fibaro:getValue(1, "sunriseHour")) )
    os.time()+45*60) == fibaro:getValue(1, "sunriseHour")) )
     
    So i get the +60 and 45 is the before sunrise how do i modify the line 
    TimeAsString == fibaro:getValue(1, "sunriseHour"
     
    to be an hour before sunrise 
     
    thankls
    Link to comment
    Share on other sites

    • 0

    Mad-Cat If you want to modify TimeAsString value to hour before that, you can convert its first 2 digits to number, change it and make a full hour at the end. But why manipulate the hour when you can change the vale you compare it to?

    Link to comment
    Share on other sites

    • 0

    Thanks Nowacki

     

    i don't quite understand sorry... Sorry very new to LUA love it but im no programmer.

     

    the first few lines where just a cut from a block scene but i dont know how to add the hour before into the script here below that (the one in this thread)... sorry i know i must seem like a TOTAL NOOB i guess i am but if u can help thanks if not i know i can manual set the time and i can revisit it later when i do know more... 

     

    thanks Again 

     

    Dave

    Link to comment
    Share on other sites

    • 0

    May I just suggest that you remove this code in all tests:

    Please login or register to see this code.

    Why? Well there are seven weekdays, 1 to 7, and testing them all does not add anything - just makes the code larger without any reason.

    Link to comment
    Share on other sites

    • 0

    ...

    [ Added: 2015-01-07, 16:31 ]

    I was trying to put in that evening started 60min after sunset instead, see if it was working but im not getting it to work

    Please login or register to see this image.

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

    Now the code is

    Please login or register to see this code.

    And this make Evening be at sunset.

    So by changing to this it should have worked right?

    Please login or register to see this code.

    But its not. your debug says:

    [DEBUG] 16:30:32: Time_of_Day: Evening

    [DEBUG] 16:30:32: Current time doesn't match any scenario

    Please login or register to see this image.

    /emoticons/default_icon_mrgreen.gif" alt=":mrgreen:" />

     

     

     

     

    Thanks for this script love it but i do have a question. im trying to change the sunrise to an hour before sunrise. i was looking at block then converting it to LUA but looks different so i cant quite work it out this is the 

     

    os.time()+60*60) == fibaro:getValue(1, "sunriseHour")) )
    os.time()+45*60) == fibaro:getValue(1, "sunriseHour")) )
     
    So i get the +60 and 45 is the before sunrise how do i modify the line 
    TimeAsString == fibaro:getValue(1, "sunriseHour"
     
    to be an hour before sunrise 
     
    thankls

     

     

    Hi guys, in matter of fact I'm also looking to change my sunset hour to 30m past sunset time, but as U5tabil and Mad-Cat I can't get it working, can anybody give us a help, PLEASE?

     

    I using a 'lighter' version of this lua scene, but this is really important because it is used in a lot of scenes, so if you can give me a hand I will be grateful.

     

    Thanks.

    Link to comment
    Share on other sites

    • 0
    On 1/8/2015 at 11:07 AM, Guest Charly said:

    The code could be extremely shortened by removing all the currentDate.wday if you use all day 1 to 7 you do not need to check.

    Your code looks a lot cleaner and every if will take processor time.

    it's true it is not needed at all

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