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 use sunset offset


rsjabbens

Question

Hello,

 

I have a script, found here, to set the time of day. This works fine, except that for the evening, in want to use 15 minutes before the sunset. I did try with blocks to take a look, and then convert to LUA, but i am not sure what to change.

 

Anyone who can help?

 

I have:

 

Please login or register to see this code.

And i found from converting a block to LUA:

Please login or register to see this code.

How can i use the 15 minutes offset, so that TimeOfDay is set to Evening, 15 minutes before sunset?

 

Can i use something like this:

 

Please login or register to see this code.

Any help is appreciated.

 

Thanks!

 

Regards,

Roland

 

 

Link to comment
Share on other sites

Recommended Posts

  • 0

Hi Roland,

 

I get below scene in my system - taken from Polish forum - (Remember, you have to create new variable - global - IsNight and give values 0 and 1 to it):

Next you can play with delays.

 

 

 

--[[ 
%% properties 
%% autostart 
%% globals 
--]] 
 
-- Initial parameters ------------------------------------------------------- 
 
if fibaro:countScenes() > 1 then 
 fibaro:debug("SCENE ABORT --------------------------") 
 fibaro:abort() 
end
 
sunriseDelay = 30
sunsetDelay = 30 
 
 
 
-- Functions ----------------------------------------------------------------- 
 
-- function is changing time in text format  "HH:MM" or os.date("*t") 
-- to number of minutes from midnight 
 
function toMinutes(czasHHMM) 
local a 
if type(czasHHMM) == "string" then 
a = tonumber(string.sub(czasHHMM, 1, 2)) * 60 + tonumber(string.sub(czasHHMM, 4, 5)) 
else    
a = tonumber(czasHHMM.hour) * 60 + tonumber(czasHHMM.min) 
end 
return a 
end 
 
-- changing minutes to "HH:MM" 
 
function toHHMM(minutes) 
local b = string.format("%02d",((minutes/60*100) - ((minutes/60*100) % 100))/100) 
local c = string.format("%02d",minutes - (tonumber(b)*60)) 
local d = b..":"..c 
return d 
end 
 
-- end of Functions----------------------------------------------------------- 
 
 
while true do 
  
-- counting how many minutes is from midnight to sunset and sunrise
 
local sunriseMinutes = toMinutes(fibaro:getValue(1, 'sunriseHour')) 
local sunsetMinutes = toMinutes(fibaro:getValue(1, 'sunsetHour')) 
 
-- counting how many minutes is from midnight to now 
 
local nowMinutes = toMinutes(os.date("*t")) 
    
-- set of global varaible isNight    
 
 
    if nowMinutes > (sunriseMinutes + sunriseDelay) 
       and nowMinutes < (sunsetMinutes + sunsetDelay) 
      then 
      fibaro:setGlobal("isNight", "0") 
      else 
      fibaro:setGlobal("isNight", "1") 
    end 
 
-- debug -------------------------------------------------------------------- 
 
fibaro:debug("Teraz jest: "..toHHMM(nowMinutes)) 
fibaro:debug("Sunrise: "..fibaro:getValue(1, 'sunriseHour')) 
fibaro:debug("Sunset: "..fibaro:getValue(1, 'sunsetHour')) 
fibaro:debug("isNight = "..fibaro:getGlobalValue("isNight")) 
fibaro:debug("-----------------------------------------") 
-- fibaro:log(fibaro:getGlobalValue("isNight"))
  
----------------------------------------------------------------------------- 
  
fibaro:sleep(60*1000) 
    
end -- end while
Link to comment
Share on other sites

  • 0

Please login or register to see this code.

local currentTime = os.date("%H:%M");

local beforeSunset = 15;

local sunDown = os.date("%H:%M", os.time() - beforeSunset * 60)

if currentTime == sunDown then

fibaro:debug("Setting Time of Day as Evening")

fibaro:setGlobal("TimeOfDay", "Evening")

end

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Hello PiotrBR, Thank you for your response! It is only that i need more day time options, like Morning, Day, Evening and Night to set my TimeOfDay. 

     

    Hello boerremk, I cannot get this working. I did try to place this in the script i have, but it doesn't work. Can you give me a help how i can change the whole script to get this working?

     

    Currently i use this script:

    Please login or register to see this code.

    So to integrate with this, for the evening:

    Please login or register to see this code.

    How can i get this working correct?

     

    Thanks!

     

    Roland

    Link to comment
    Share on other sites

    • 0

    I rewritten it a little bit... the way current time was generated was a little bit complex, os.date("%H:%M") is doing what you want.

    Didn't test it, so maybe i made a typo, but try and let me know if you need more assistence.

     

    I have also included the script I use, it not really based on 1 time slot, but a period, it is more stable (never misses a timeslot), also after reboot it will set the right TimeOfDay.

     

    Your script, rewritten

    Please login or register to see this code.

    The script I use:

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hello Remko, thank you for the support. I gave the modified script a try and it seems to work so far.

     

    I agree that the script you use is more stable, it can be run just every time to set the TimeOfDay correctly and is not triggered at specific times. The only problem i have, is that i want it based on sunrise/sunset, because this way i can trigger my light scenes correctly.

     

    I want to be able  to turn my lights off after or before sunrise and on after or before sunset. Because sunrise and sunset changes over the year, for me it is better to use the sunrise and sunset values from the HC2.

     

    Is it also possible to modify your script with sunrise and sunset values, including some offset minutes?

     

    Regards,

    Roland

     

     

    [DEBUG] 08:34:47: Script started 

    Link to comment
    Share on other sites

    • 0

    Here you go. Also changed night to 23:00, afternoon to day (and 10:00), and you can add time after sunrise (and before sunset):

    Didn't test the changes, please let me know if you run to any troubles.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thank you Remko!

     

    The script reports:

     

    [DEBUG] 13:48:39: HC2 start script at Sun Jan 10 13:48:39 2016
    [DEBUG] 13:48:39: evening
     
    Doesn't seem correct, because it is not evening. Time and timezone is correctly configured.
     
    TimeOfDay has these values at my variables: Night, Morning, Day & Evening. Maybe capital sensitive? I did try to change some of the names with capital, but with the same result only that, TimeOfDay is now also changed to evening.
     
    What could be wrong?
     
    Regards,
    Roland
    Link to comment
    Share on other sites

    • 0

    Hmm missed the capitals.... new script with capitals and a typo removed:

    I have the same name as Global Variable, but different periods, so testing is a little bit difficult, because the whole house can be locked up  (alarm on, heating off, blinds down, etc)

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Haha, i understand that you don't want to test this @home. My home is currently also partly dark with al the testing, but for now it is just a light scene that is used with this

    Please login or register to see this image.

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

     

    I do think that something stil is not going as supposed.

     

    It started good, at 16:35:37 the TimeOfTheDay seem to be changed, this was in the scene log:

     

    [DEBUG] 15:48:37: HC2 start script at Sun Jan 10 15:48:37 2016
    [DEBUG] 15:48:37: Day
    [DEBUG] 16:35:37: Evening
    [DEBUG] 16:36:37: Day
    [DEBUG] 16:37:37: Evening

     

    This repeated every minute, this is the last part:

     

    [DEBUG] 18:10:41: Day
    [DEBUG] 18:11:41: Evening
    [DEBUG] 18:12:41: Day
    [DEBUG] 18:13:41: Evening
    [DEBUG] 18:14:41: Day
    [DEBUG] 18:15:42: Evening
    [DEBUG] 18:16:42: Day
    [DEBUG] 18:17:42: Evening
    [DEBUG] 18:18:42: Day
    [DEBUG] 18:19:42: Evening
    [DEBUG] 18:20:42: Day
    [DEBUG] 18:21:42: Evening

     

    It  look like that both day and evening are somehow true after the first time that evening was true and set. Any idea?

     

    Regards,

    Roland

    Link to comment
    Share on other sites

    • 0

    Did you used the latest script, I changed it at around 18:00:

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • My bad, i used the older version. 

     

    Now it seem to work very good. Did some testing and no problems yet. Only changed: 

     

    local evening = toTimestamp(sunset, beforeSunset);

     

    to 

     

    local evening = toTimestamp(sunset, -beforeSunset);

     

    I let you now the next days how it is working!

     

    Thank you Remko!

    Link to comment
    Share on other sites

    • 0

    I changed the variables beforeSunset and beforeSunrise to offsetSunset and offsetSunrise, so it is more flexible, see previous post with new names.

    Link to comment
    Share on other sites

    • 0

    Roland, I have modified the original script you used and incorporated part of boerremk's script in so that it is more robust to handle offsets from sunrise/sunset. I have not really tested it out yet though initial trial run did not come up with error.

     

    boerremk, the 50 seconds sleep (now changed to 55 seconds) is due to a rare occurrence of abnormality (when using 60 seconds) where the script skipped a minute and end up not matching the exact time for the TimeOfDay conditions. Using 50 seconds, there has been no abnormality since using it for several months now.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    HI Chaicka

     

    "boerremk, the 50 seconds sleep (now changed to 55 seconds) is due to a rare occurrence of abnormality (when using 60 seconds) where the script skipped a minute and end up not matching the exact time for the TimeOfDay conditions."

     

    That why I never use == but always between 2 values.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hello Chaicka and boerremk,

     

    The script from boerremk is working fine, without any problem the last week. The time of day is set correctly.

     

    For me currently there is no reason to change to the latest chaicka script, or is there some reason i am not aware of?

     

    Regards,

    Roland

    Link to comment
    Share on other sites

    • 0
    Guest vipernor

    Is this the beginning of a VD for setting an offset on the time of day ? 

    Example I need to change time of day "night" for weekdays earlier then for weekends. 

    What's the best solution you think? 

    Link to comment
    Share on other sites

    • 0

    Hello

     

    Am i doing something wrong and get this message ??

     

     

    [DEBUG] 18:25:21: Script started because of: autostart
    [DEBUG] 18:25:21: Current Time: 18:25
    [DEBUG] 18:25:21: line 44: attempt to index global 'currentDate' (a nil value)

    Link to comment
    Share on other sites

    • 0

    Hello

     

    Am i doing something wrong and get this message ??

     

     

    [DEBUG] 18:25:21: Script started because of: autostart

    [DEBUG] 18:25:21: Current Time: 18:25

    [DEBUG] 18:25:21: line 44: attempt to index global 'currentDate' (a nil value)

     

    In the script of chaicka you have to replace currentDate for currDate (he forgot to change some entries), but you can also use post 11, that one will work

    Link to comment
    Share on other sites

    • 0

    In the script of chaicka you have to replace currentDate for currDate (he forgot to change some entries), but you can also use post 11, that one will work

     

    Bingo. Thanks boeeremk. I am able to spare some time last night (after visitings for Chinese New Year) to test and found the bug. I think I will look at merging both our scripts to make it more structured, easier for configurables section without others touching the lines of core engine and more robust for different use-cases so it will be suitable for globalised use.

    Link to comment
    Share on other sites

    • 0
    Guest vipernor

    Hi, I have been trying to rewrite the timeofday script so that there's a difference between weekdays and weekends when 'night' is activated. Could one of you maybe point me in the right direction. The thing is that I need the night mode to be activated say 24:00 on weekdays and 01:30 on weekends.  

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