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


Hours between Sunset and Sunrise the next day


tampiss

Recommended Posts

Hello guys,

would be anyone so kind and help me calculate hours and minutes between current sunset hour and the next days sunrise?

 

local sunset = fibaro:getValue(1, "sunsetHour")
local sunrise = fibaro:getValue(1, "sunriseHour")
 
I would very appriciate having the information, that the night will last xy hours and xy minutes.
 
Thank you very much in an advance,
tampiss
Link to comment
Share on other sites

Guest FredrikKarlsson

Hi,

Need more information. Are you fine with assuming that sunrise tomorrow will occur approximately at the same time as it did today )mad disregard the difference)?

Link to comment
Share on other sites

Put this code on a VD button , edit your longitude & latitude ,then , from then mai loop, activate it every 8 hours. 

 

 

local NET = Net.FHttp("api.sunrise-sunset.org",80);
    fibaro:debug("Connecting....");
    fibaro:log("Connecting....");
    local My_lat = 45.8258910496758 ;
    local My_lng = 8.79590749740601 ;
    local response ,status, err = NET:GET("/json?My_lat=" .. My_lat .. "&My_lng=" ..My_lng .. "&formatted=0");
    if (tonumber(status) == 200 and tonumber(err)==0) then
        fibaro:debug("Collegamento OK")
          if (response ~= nil) then
            rem_jsonT = json.decode(response); -- converto i dati ricevuti da "api.sunrise-sunset.org"
            HC2_jsonT = json.decode(fibaro:getGlobalValue("Twilight")) -- apro la tabella memorizzata nelle variabili globali
                      
            if rem_jsonT.status ~= "OK" then
                fibaro:debug("Dati non disponibili")
                fibaro:log("Error-> ".. rem_jsonT.status);
                fibaro:debug("Error-> ".. rem_jsonT.status);
                HC2_jsonT.Status = rem_jsonT.status
                HC2_jsonT.conn_Time = os.date("%d-%m-%Y  %H:%M");
                fibaro:setGlobal("Twilight", json.encode(HC2_jsonT))
                return
            end
            
            local Start_T = rem_jsonT.results.civil_twilight_begin
            local TimeOffset = 0
            
            mytab = os.date("*t") -- defaults to current date and time
            -- controllo se siamo in presenza dell'ora legale
            if mytab.isdst  then
               TimeOffset = 7200 -- Adjust the Timezone (+1) + Ora legale
            else      
               TimeOffst = 3600 -- Adjust the Timezone (+1)
            end
            
            mytab.year = string.sub(Start_T, 1,4)
            mytab.month = string.sub(Start_T, 6,7)
            mytab.day = string.sub(Start_T, 9,10)
            mytab.hour = string.sub(Start_T, 12,13)
            mytab.min = string.sub(Start_T, 15,16)
            mytab.sec =string.sub(Start_T, 18,19)
            local timestamp
            timestamp = os.time ( mytab )
            timestamp = timestamp + TimeOffset
            
    
            fibaro:debug(os.date("%H:%M", timestamp))  
            HC2_jsonT.Start_T = os.date("%H:%M", timestamp)
            
            local End_T = rem_jsonT.results.civil_twilight_end
            
            mytab.year = string.sub(End_T, 1,4)
            mytab.month = string.sub(End_T, 6,7)
            mytab.day = string.sub(End_T, 9,10)
            mytab.hour = string.sub(End_T, 12,13)
            mytab.min = string.sub(End_T, 15,16)
            mytab.sec =string.sub(End_T, 18,19)
            timestamp = os.time ( mytab )
            timestamp = timestamp + TimeOffset
            fibaro:debug(os.date("%H:%M", timestamp))
            fibaro:log(" Twilight Updated");
            HC2_jsonT.End_T = os.date("%H:%M", timestamp)
            HC2_jsonT.Status = rem_jsonT.status
            HC2_jsonT.conn_Time = os.date("%d-%m-%Y  %H:%M");
            fibaro:setGlobal("Twilight", json.encode(HC2_jsonT))
          else
            fibaro:debug("status:" .. status .. ", errorCode:" .. errorCode);
    
        end
  end

 

 

 

 

------------------------------------------------------------------------------------------------------------------------

 

 

--  MAIN LOOP

 

 

 

if (not FirstExec) then
    fibaro:debug("Script Started")
    fibaro:debug("Checking JSON Table on StartUp")
    FirstExec=1
    n = string.len (fibaro:getGlobalValue("Twilight"))
    if ( n < 10) then
      fibaro:debug("JSON Table ->Twilight<- created")
      mytable = {Inizio = "07:00" , Fine = "19:00" , conn_Time = "" , Status = "????"}
      fibaro:debug(json.encode(mytable))
      fibaro:setGlobal("Twilight", json.encode(mytable))
    
    end
    next_time = os.time();
    fibaro:sleep(5000)
   
end  
 

-- apro la tabella memorizzata nelle variabili globali
HC2_jsonT = json.decode(fibaro:getGlobalValue("Twilight"))


selfID = fibaro:getSelfId()
c_time = os.time();

-- Check if it is time to activate the VD update button
if c_time > next_time then
-- Occhio per indirizzare il pulsante vanno contate sia label che buttons.
   fibaro:call(selfID, "pressButton", "5");
   next_time = c_time + (3600 * 8)  -- Next Update after 8 hours
   fibaro:debug("Loop Request Update at -> " .. os.date("%d-%m-%Y  %H:%M"))
end  

if HC2_jsonT.Status ~= "OK" then
   next_time = c_time + 60
   fibaro:debug("Polling Time reduced due Error")
end  

fibaro:call(selfID, "setProperty", "ui.lblInizio.value", HC2_jsonT.Inizio)
fibaro:call(selfID, "setProperty", "ui.lblFine.value",  HC2_jsonT.Fine)
fibaro:call(selfID, "setProperty", "ui.lblStatus.value", HC2_jsonT.Status)
fibaro:call(selfID, "setProperty", "ui.lblAttempt.value", HC2_jsonT.conn_Time)

fibaro:sleep(60 *1000)

Link to comment
Share on other sites

anonymous solution is very interesting but after checking API response from sunrise.sunset.org it gives back day duration and then from that you still have to calculate night duration or time between sunset and sunrise. 

 

My solution is simple and still not really calculating time duration between today's sunset and tomorrow's sunrise but if you enter time correction for tomorrow's sunrise then it can be accurate as it gets. Since difference is almost always same day by day you can always add correction in minutes. This code can be used in both scene and VD and function to calculate time duration between two different times is universal and is always calculating difference between later and earlier time. Also one example of nesting function subroutines to make code more simple and easier to debug

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" />

 

So here is that simple code snippet:

 

Please login or register to see this code.

I hope you find it useful. Happy coding!

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" />

Link to comment
Share on other sites

  • 2 weeks later...
  • Topic Author
  •  

    anonymous solution is very interesting but after checking API response from sunrise.sunset.org it gives back day duration and then from that you still have to calculate night duration or time between sunset and sunrise. 

     

    My solution is simple and still not really calculating time duration between today's sunset and tomorrow's sunrise but if you enter time correction for tomorrow's sunrise then it can be accurate as it gets. Since difference is almost always same day by day you can always add correction in minutes. This code can be used in both scene and VD and function to calculate time duration between two different times is universal and is always calculating difference between later and earlier time. Also one example of nesting function subroutines to make code more simple and easier to debug

    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" />

     

    So here is that simple code snippet:

     

    Please login or register to see this code.

    I hope you find it useful. Happy coding!

    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" />

     

     

    cool

    Please login or register to see this image.

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

    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
    Reply to this topic...

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