Announcement

Collapse
No announcement yet.

sunrise/sunset script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sunrise/sunset script

    As part of the script that I am writting to use with my alarm system, I want to compare whether the current time is before or after sunrise/sunset.

    Here is what I came up with, but it does not work correct.

    sub main()

    dim currenttime
    dim sunset
    dim sunrise


    sunset = hs.sunset
    sunrise = hs.sunrise
    currenttime = formatdatetime (time, vbshorttime)


    msgbox currenttime & " sr: " & sunrise & " ss: " & sunset
    if (curerenttime <= sunrise) or (currenttime >= sunset) then
    hs.speak "It is dark outside"
    else
    hs.speak "It is light outside."
    end if


    end sub


    Can anybody help me out on this.

    Regards Willem-bram

  • #2
    Try this out, I threw it together in about 15 min. so did not have much time to debug it.

    Code:
    sub main()
    
    dim currenttime
    dim sunset
    dim sunrise
    dim sunrisediff
    dim sensetdiff
    
    sunset = hs.sunset
    sunrise = hs.sunrise
    currenttime = formatdatetime (time, vbshorttime)
    
    sunrisediff = datediff("n", currenttime, sunrise)
    sunsetdiff = datediff("n", currenttime, sunset)
    if (sunrisediff <= 0) and (sunsetdiff >= 0) then
    hs.speak "It is light outside"
    else
    hs.speak "It is dark outside."
    end if
    
    
    end sub

    Comment


    • #3
      That works!


      thanks a lot.

      Comment


      • #4
        thank you for that script! it's the only one anywhere in the forum that actually works. There are all kinds of peculiarities with sunset/sunrise that cause problems but this script really works.

        Comment


        • #5
          A script is not required to do this. You could set up and event that triggers on Daytime or Nighttime.
          -Rupp
          sigpic

          Comment


          • #6
            He did ask for it as part of larger (alarm) script, though

            Anyway, here is my VB.NET flavour tuppence-worth:

            Code:
            Private Function IsSun(Optional ByVal TimeToCheck As Date = Nothing) As Boolean
                If TimeToCheck = Nothing Then
                    TimeToCheck = Now
                End If
                Return TimeToCheck >= hs.SunriseDt And TimeToCheck < hs.SunsetDt
            End Function

            Comment

            Working...
            X