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

lua command 30 minutes before sunrise and after sunset


rjansen

Question

Hi,

I have a goog working Lua script for  Dag (Day) and nacht (night) variable.
The settings are for the variable Day and night are exactly start at sunrise or sunset.

 

But now i want to program 30 minuts. So the variable Dag (Day)  for sunrise must start 30 minutes later  and for Sunset 30 minuts earlyer.

Dous anyone now where and how i must programm a 30 minutes code?

 

This is my Lua code:

 

--[[
%% autostart
%% properties
%% globals
--]]

--Deze senne maakt van variable Nacht.
 
--Wat heeft de scene gestart? 
local sourceTrigger = fibaro:getSourceTrigger();
 
--------------------------------------------------
if (sourceTrigger["type"] == "autostart") then
 
   --Controleer elke minuut het tijdstip
   while true do
 
   --Defineer de tijden
   local currentDate = os.date("*t");
   local TimeNow = (string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min))
   local sunrise=fibaro:getValue(1, "sunriseHour")
   local sunset=fibaro:getValue(1, "sunsetHour")
 
   local startSource = fibaro:getSourceTrigger();
 
   fibaro:debug("Het is nu: "..TimeNow)
   fibaro:debug("Zonsopkomst: "..sunrise)
   fibaro:debug("Zonsondergang: "..sunset)
   --Bij zonsopkomst    
  if (TimeNow == sunrise) then
      fibaro:setGlobal("Dag_Nacht","Dag")
  end
 
   --bij zonsondergang    
   if (TimeNow == sunset) then
       fibaro:setGlobal("Dag_Nacht","Nacht")
   end
    
   --wacht 1 minuten   
   fibaro:sleep(60*1000);
   end
else
   --dit gebeurt als je handmatig scene start
   ---------------------------------------------------
   local Dag_Nacht=fibaro:getGlobalValue("Dag_Nacht")
   if Dag_Nacht == "Nacht" then
       fibaro:setGlobal("Dag_Nacht","Dag")
   else
       fibaro:setGlobal("Dag_Nacht","Nacht")
   end
end
 

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • My scene is also running in the backround, and is working perfect for exact givinging the sunrise and sunset moment for my Day_Night variable.

    Also my script looks less complex as yours with all respect of course.

     

    So i only need the rigth code extra for 30 minutes (or what ever) to give the sunrice and sunset moment a delay of earlyer.

    Edited by rjansen
    Link to comment
    Share on other sites

    • 0
    1 hour ago, rjansen said:

    My scene is also running in the backround, and is working perfect for exact givinging the sunrise and sunset moment for my Day_Night variable.

    Also my script looks less complex as yours with all respect of course.

     

    So i only need the rigth code extra for 30 minutes (or what ever) to give the sunrice and sunset moment a delay of earlyer.

     

    The point is that you don't need to deal with the Timer scene - it runs in the background, your own scene just declares the hours it want to be triggered - and you can easily add variations for days, weekends months etc.

    It keeps the scene clean, doesn't need to loop every  minute 24x7 to check times  and I think it's easier to understand - but that's a matter of taste.

     

    Btw, your scene is not working perfect - it can fail the tests "TimeNow == sunrise" or "TimeNow == sunset". The reason is that your scene drifts as the loop takes a little longer (a few milliseconds) than 60*1000 milliseconds. This will result that once in a while it will skip a minute and if that skipped minute coincides with the sunrise/sunset times it will miss it. Murphy's law states that it will happen that day when you really really rely on the variable being updated...

    In general it's not advised to do exact time comparison like 'now == time'. Alternatively fix the loop so it doesn't drift.

     

    Please login or register to see this code.

     

    You can also fix your scene by adding:

    Please login or register to see this code.

    ...and test your sunrise/sunset against TimeNowPlus30 and TimeNowMinus30...

     

    and avoid Murphy by changing your 'while true' loop to

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi Jgab,

     

    A oke, i understeand what you are meaning.

     

    I will try your scene for look and feel.

     

    Thnx for so far

     

    by the way. It dousnt matter to me that one time the 1 minut trigger miss the sunset/sunrise time. It will activatet a minut later.

    I use this scene in to anoter scene wat use de Day / Night value of burning a bulb in the house with the motion trigger. When its Day, it will do nothing and when its Night, the Bulp will burning when trigered by the motion sensor.

    Edited by rjansen
    Link to comment
    Share on other sites

    • 0
    45 minutes ago, rjansen said:

    by the way. It dousnt matter to me that one time the 1 minut trigger miss the sunset/sunrise time. It will activatet a minut later.

    I use this scene in to anoter scene wat use de Day / Night value of burning a bulb in the house with the motion trigger. When its Day, it will do nothing and when its Night, the Bulp will burning when trigered by the motion sensor.

    It will miss a sunrise or a sunset  for the whole day... not just a minute.

    If the sunset is at ex. 21:30, your checks, if you are unlucky, will test 21:29, 21:31 - i.e. skip the 20:30th minute. 

    Edited by jgab
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi Jgab,

     

    I don't know what you meaning.

    The scene checks every minute if it is sunset or sunrice.  This not a fixed time, but will automaticly changes each day the sunset and sunrise are later/earlyer. All the time it see its sunrise, its getting the variable "Dag" (day) and  my other scene wat must trigger te bulb knows, when its still day, it don't activate the bulb when te motion sensor is breached. Fot the value "nacht"(Night) it will al the night  activate the Bulb when the morion sensor is breached. So the combination of those scenes' are working realy fine with eachother. Ony i want that the motion sensor can be activate  the buld arround 30 minuts later of earlyer then the exact time of sunrise and sunset.

    Link to comment
    Share on other sites

    • 0

    Ok,

    your loop that waits 1minute

    59 minutes ago, rjansen said:

    I don't know what you meaning.

     

    Ok,

    You have a loop

    Please login or register to see this code.

    The problem is that the "Inside" of your loop takes a little time to execute, let's say 1ms.

    That means that one loop doesn't take 1min, it takes 1min and 1ms. That means that if you started the loop exactly 22:00:00, after 1000 minutes you have drifted 1000*1ms = 1s. and it is 22:00:01

    That may not seem so dangerous?

    Well, assume that you started the loop at 21:59:59:999 (21:59:59 + 999ms) - and assume that sunset is at "22:00"

    First turn in the loop you test "21:59" == "22:00", which is false (current time 21:59:59:999 is truncated to "21:59")

    Your loop takes 1ms, so we add that to the current time, which becomes (21:59:59:999 + 00:00:00:001) = 22:00:00:000

    Then you sleep 60*1000ms and the time becomes 22:01:00:000

    Next turn in the loop you test "22:01" == "22:00", which is also false

    --- You skipped a minute. And you will not get another match until next day.

     

    The problem is that this happens rarely, but it may bite you when you least expect it and you will be convinced that it is Fibaro's and the HC2's fault, because the scene has always worked in the past....

    Link to comment
    Share on other sites

    • 0
    38 minutes ago, jgab said:

    you will be convinced that it is Fibaro's and the HC2's fault, because the scene has always worked in the past....

    As we often saw here on forum in the past ?

    Link to comment
    Share on other sites

    • 0
    21 hours ago, rjansen said:

    But now i want to program 30 minuts. So the variable Dag (Day)  for sunrise must start 30 minutes later  and for Sunset 30 minuts earlyer.

    Dous anyone now where and how i must programm a 30 minutes code?

    Beside Scene Timer you can try All-in-One Scene as well

     

     

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