Announcement

Collapse
No announcement yet.

Solar Position Script

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

  • Guest
    Guest replied
    Originally posted by pcgirl65 View Post
    I am just seeing this solution and it is exactly what I have been looking for. However, I am not very familiar with how to use scripts in events. Can someone help me with this and also, where exactly in the script to I have to enter my coordinates? Sorry for the dumb questions but I would like to use this idea but I have no knowledge of VB.
    I took the liberty of modifying the script to better suit my needs, and my changes might be helpfull to you.

    Code:
    ' Version: 30 September 2021 B
    'From c code posted here: http://www.psa.es/sdg/sunpos.htm
    'VB.NET Conversion posted here: http://www.vbforums.com/showthread.php?832645-Solar-position-calculator
    'converted for HomeSeer use by Sparkman v1.0
    'Modified to allow for determining Sunrise and Sunset Azimuths by aa6vh
    'Requires two Virtual Devices to hold the info: Night and WxSolar
    Imports System.Math
    Private Function BearConvert(ByVal sBear As String) As String
      If IsNumeric(sBear) = False Then Return "-"
      Dim iBear As Integer = CInt(sBear)
      Do While iBear >= 360
       iBear = iBear - 360
      Loop
      Do While iBear < 0
        iBear = iBear + 360
      Loop
      iBear = CInt(Math.Round(CSng((iBear * 2) / 45)))
      Dim sDeg As String = "N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNE,N"
      Dim buff(18) As String
      buff = sDeg.Split(",")
      Return buff(iBear)
    End Function
    Private Sub SolarPosition(ByRef dt As DateTime, ByRef sAzm As String, ByRef sAlt As String)
      Dim Debug As Boolean = False
      Dim logName As String = "Solar"
      Dim dLatitude As Double = 34.2220281
      Dim dLongitude As Double = -119.1724294
      Dim pi As Double = 3.14159265358979
      Dim rad As Double = pi / 180
      Dim dEarthMeanRadius As Double = 6371.01
      Dim dAstronomicalUnit As Double = 149597890
      Dim iYear As Integer = dt.Year
      Dim iMonth As Integer = dt.Month
      Dim iDay As Integer = dt.Day
      Dim dHours As Double = dt.Hour
      Dim dMinutes As Double = dt.Minute
      Dim dSeconds As Double = dt.Second
      Dim dZenithAngle As Double
      Dim dZenithAngleParallax As Double
      Dim dAzimuth As Double
      Dim dAltitudeAngle As Double
      Dim dElapsedJulianDays As Double
      Dim dDecimalHours As Double
      Dim dEclipticLongitude As Double
      Dim dEclipticObliquity As Double
      Dim dRightAscension As Double
      Dim dDeclination As Double
      Dim dY As Double
      Dim dX As Double
      Dim dJulianDate As Double
      Dim liAux1 As Integer
      Dim liAux2 As Integer
      Dim dMeanLongitude As Double
      Dim dMeanAnomaly As Double
      Dim dOmega As Double
      Dim dSin_EclipticLongitude As Double
      Dim dGreenwichMeanSiderealTime As Double
      Dim dLocalMeanSiderealTime As Double
      Dim dLatitudeInRadians As Double
      Dim dHourAngle As Double
      Dim dCos_Latitude As Double
      Dim dSin_Latitude As Double
      Dim dCos_HourAngle As Double
      Dim dParallax As Double
      Try
        If Debug Then hs.WriteLog(logName, CStr(iMonth) + "-" + CStr(iDay) + "-" + CStr(iYear) + " " + CStr(dHours) + ":" + CStr(dMinutes) + ":" + CStr(dSeconds))
    ' Calculate difference in days between the current Julian Day and JD 2451545.0, which is noon 1 January 2000 Universal Time
    ' Calculate time of the day in UT decimal hours
        dDecimalHours = dHours + (dMinutes + dSeconds / 60.0) / 60.0
    ' Calculate current Julian Day
        liAux1 = (iMonth - 14) \ 12
        liAux2 = (1461 * (iYear + 4800 + liAux1)) \ 4 + (367 * (iMonth - 2 - 12 * liAux1)) \ 12 - (3 * ((iYear + 4900 + liAux1) \ 100)) \ 4 + iDay - 32075
        dJulianDate = CDbl(liAux2) - 0.5 + dDecimalHours / 24.0
    ' Calculate difference between current Julian Day and JD 2451545.0
        dElapsedJulianDays = dJulianDate - 2451545.0
        If Debug Then hs.writelog(logName,"Elapsed Julian Days Since 2000/01/01: " & CStr(dElapsedJulianDays))
    ' Calculate ecliptic coordinates (ecliptic longitude and obliquity of the ecliptic in radians but without limiting the angle to be less than 2*Pi
    ' (i.e., the result may be greater than 2*Pi)
        dOmega = 2.1429 - 0.0010394594 * dElapsedJulianDays
        dMeanLongitude = 4.895063 + 0.017202791698 * dElapsedJulianDays ' Radians
        dMeanAnomaly = 6.24006 + 0.0172019699 * dElapsedJulianDays
        dEclipticLongitude = dMeanLongitude + 0.03341607 * Math.Sin(dMeanAnomaly) + 0.00034894 * Math.Sin(2 * dMeanAnomaly) - 0.0001134 - 0.0000203 * Math.Sin(dOmega)
        dEclipticObliquity = 0.4090928 - 0.000000006214 * dElapsedJulianDays + 0.0000396 * Math.Cos(dOmega)
        If Debug Then hs.writelog(logName,"Ecliptic Longitude: " & CStr(dEclipticLongitude))
        If Debug Then hs.writelog(logName,"Ecliptic Obliquity: " & CStr(dEclipticObliquity))
    ' Calculate celestial coordinates ( right ascension and declination ) in radians but without limiting the angle to be less than 2*Pi (i.e., the result may be greater than 2*Pi)
        dSin_EclipticLongitude = Math.Sin(dEclipticLongitude)
        dY = Math.Cos(dEclipticObliquity) * dSin_EclipticLongitude
        dX = Math.Cos(dEclipticLongitude)
        dRightAscension = Math.Atan2(dY, dX)
        If dRightAscension < 0.0 Then
          dRightAscension = dRightAscension + (2 * pi)
        End If
        dDeclination = Math.Asin(Math.Sin(dEclipticObliquity) * dSin_EclipticLongitude)
        If Debug Then hs.writelog(logName,"Declination: " & CStr(dDeclination))
    ' Calculate local coordinates ( azimuth and zenith angle ) in degrees
        dGreenwichMeanSiderealTime = 6.6974243242 + 0.0657098283 * dElapsedJulianDays + dDecimalHours
        dLocalMeanSiderealTime = (dGreenwichMeanSiderealTime * 15 + dLongitude) * rad
        dHourAngle = dLocalMeanSiderealTime - dRightAscension
        If Debug Then hs.writelog(logName,"Hour Angle: " & CStr(dHourAngle))
        dLatitudeInRadians = dLatitude * rad
        dCos_Latitude = Math.Cos(dLatitudeInRadians)
        dSin_Latitude = Math.Sin(dLatitudeInRadians)
        dCos_HourAngle = Math.Cos(dHourAngle)
        dZenithAngle = (Math.Acos(dCos_Latitude * dCos_HourAngle * Math.Cos(dDeclination) + Math.Sin(dDeclination) * dSin_Latitude))
        dY = -Math.Sin(dHourAngle)
        dX = Math.Tan(dDeclination) * dCos_Latitude - dSin_Latitude * dCos_HourAngle
        dAzimuth = Math.Atan2(dY, dX)
        If dAzimuth < 0.0 Then
          dAzimuth = dAzimuth + (2 * pi)
        End If
        dAzimuth = dAzimuth / rad
        If Debug Then hs.writelog(logName,"Azimuth: " & CStr(dAzimuth))
    ' Parallax Correction
        dParallax = (dEarthMeanRadius / dAstronomicalUnit) * Math.Sin(dZenithAngle)
        dZenithAngleParallax = (dZenithAngle + dParallax) / rad
        dAltitudeAngle = (dZenithAngleParallax * -1) + 90
        If Debug Then hs.writelog(logName,"Altitude Angle: " & Math.Round(dAltitudeAngle).ToString)
        sAzm = Math.Round(dAzimuth).ToString
        sAlt = Math.Round(dAltitudeAngle).ToString
      Catch ex As Exception
        hs.WriteLog(logName, "Exception " & ex.ToString)
      End Try
    End Sub
    Public Sub SunRiseSet(parms As Object)
    ' Determines the azimuth of the sun for sunrise and sunset times
      Dim buff As String
      Dim sAzm As String = "", sAlt As String = "", sAzms As String = "", sAlts As String = ""
      buff = hs.Sunrise()
      Dim dt As DateTime = DateTime.Parse(buff)
      dt = dt.ToUniversalTime()
      SolarPosition(dt, sAzm, sAlt)
      buff = hs.Sunset()
      dt = DateTime.Parse(buff)
      dt = dt.ToUniversalTime()
      SolarPosition(dt, sAzms, sAlts)
      buff = LCase(hs.Sunrise() + " " + sAzm + "°/" + hs.Sunset() + " " + sAzms + "°")
      hs.SetDeviceStringByName("Night", buff, True)
    End Sub
    Sub Main(parms As Object)
    ' Determines the current azimuth and altitude angle of the sun
      Dim dt As DateTime = DateTime.UtcNow()
      Dim sAzm As String = "", sAlt As String = "", sBearing As String
      SolarPosition(dt, sAzm, sAlt)
      Dim buff As String = Format(Now(), "ddd h:mmtt")
      sBearing = BearConvert(sAzm)
      If CInt(sAlt) < 0 Then
        hs.SetDeviceStringByName("WxSolar", buff + " Nighttime", True)
      Else
        hs.SetDeviceStringByName("WxSolar", buff + " Azmth: " + _
          sAzm + "° (" + sBearing + "), Alt: " + sAlt + "°", True)
      End If
    End Sub
    Since my house does not move, I supply the Lat and Long in the dLatatitiude and dLongitude variables. I also converted this into Javascript for running on my phone, but I supply the Lat and Long as an input parameter there since the phone does move.

    To use, simply create an event action of "Run script", supplying the function name you want to run (Main or SunRiseSet)

    Leave a comment:


  • pcgirl65
    replied
    Actually, it looks like the scripts create the virtual devices, which is interesting but he doesn't elaborate on if the corordinates need to be changed or where to do that.

    Leave a comment:


  • zwolfpack
    replied
    A similar project, with setup instructions: https://forums.homeseer.com/forum/de...d-moon-project

    Leave a comment:


  • pcgirl65
    replied
    I am just seeing this solution and it is exactly what I have been looking for. However, I am not very familiar with how to use scripts in events. Can someone help me with this and also, where exactly in the script to I have to enter my coordinates? Sorry for the dumb questions but I would like to use this idea but I have no knowledge of VB.

    Leave a comment:


  • Richel
    replied
    Originally posted by jon00 View Post

    I expect you are using Task Scheduler to start Homeseer? In the Task/Actions/Edit Action, make sure you place the full path to Homeseer in the Start in field i.e. "C:\Program Files (x86)\HomeSeer HS4"

    If that fails, check that it works when you start Homeseer from the Desktop Icon.
    Brilliant! You are correct! Thank you, Jon00. Elliott

    Leave a comment:


  • jon00
    replied
    Originally posted by Richel View Post
    I just started seeing this today:
    5/20/2021 10:52:02

    HomeSeer
    Error
    Compiling script Astronomical.vb: compiler initialization failed unexpectedly: The system cannot find the file specified.

    5/20/2021 10:52:02

    HomeSeer
    Error
    Compiling script Astronomical.vb: could not find library 'Scheduler.dll'

    I am running HS4 Pro, which I upgraded to last week. Scheduler.dll is present in the root directory. Any help to fix would be appreciated. Thanks.
    I expect you are using Task Scheduler to start Homeseer? In the Task/Actions/Edit Action, make sure you place the full path to Homeseer in the Start in field i.e. "C:\Program Files (x86)\HomeSeer HS4"

    If that fails, check that it works when you start Homeseer from the Desktop Icon.

    Leave a comment:


  • Richel
    replied
    I just started seeing this today:
    5/20/2021 10:52:02

    HomeSeer
    Error
    Compiling script Astronomical.vb: compiler initialization failed unexpectedly: The system cannot find the file specified.

    5/20/2021 10:52:02

    HomeSeer
    Error
    Compiling script Astronomical.vb: could not find library 'Scheduler.dll'

    I am running HS4 Pro, which I upgraded to last week. Scheduler.dll is present in the root directory. Any help to fix would be appreciated. Thanks.

    Leave a comment:


  • sparkman
    replied
    Originally posted by dmurphy View Post
    I have a luimnance detector in room with shades....

    run script after sunrise and before sunset, every 15 mins.....and luminance is greater than certain value.

    Took some adjusting to get it right, but it works a charm
    I use luminance sensors with the scripts as well. They only send value updates on a relatively large interval so no issue with the shades opening and closing. I close the shades when it is bright and open them when it is not. I have some exceptions as well when it is cold outside and sunny, but no one is home or in the room then I open them to get the sun to help with the heating.

    Leave a comment:


  • dbrunt
    replied
    Originally posted by dmurphy View Post
    I have a luimnance detector in room with shades....

    run script after sunrise and before sunset, every 15 mins.....and luminance is greater than certain value.

    Took some adjusting to get it right, but it works a charm
    Just make sure that the detected luminance when the shades are closed does not drop below the threshold trigger otherwise the shades will close-open-close-open every 15 minutes! You could possibly disable the event after triggering and then re-enable it some time later. That's where the adjusting part comes into play!

    I put my luminance detector outside the shades so that could not happen. That way it detects the outside luminance, not the luminance in the room. Make sure the outside luminance detector can register a high enough value. Everspring ST-815 can not...it maxes at 3000 lux which a cloudy day can exceed. I'm using Mi (aka Lumi, aka Aqara) Zigbee sensors which register up to 83,000 lux...

    Leave a comment:


  • dmurphy
    replied
    I have a luimnance detector in room with shades....

    run script after sunrise and before sunset, every 15 mins.....and luminance is greater than certain value.

    Took some adjusting to get it right, but it works a charm

    Leave a comment:


  • wkearney99
    replied
    And now I'm stumbling in, finding this. Nice. Seems to be calculating things right for me here in MD.

    What're some thoughts on tying this with live luminance data?

    I have some shades in the home office that are battery operated. Hunter Douglas, with bi-directional opening (top/down & bottom/up). I have them on scheduled open/close cycles and that's good for average days. But on overcast days it'd be nice to let in more light. The problem is during winter months, the low angle is bad for opening the bottom (shines in the eyes). But for summer it's the opposite, the sun being high in the sky means letting the top open is a problem.

    I want to avoid twitching the shades too often, as it'd be annoying and waste battery life.

    Any thoughts on the 'logic tree' to use for this?

    I have the UltraRachio plug-in, and that has several weather-related values. Their current cloud cover value is largely unsuitable for shade management. I don't know how they determine it, but what it thinks is 'cloud cover' is nothing like what we have here on-site. Not unexpected though as that's not why they provide the data. They do have some 'weather conditions' fields that could be useful. An integer for various conditions and I could use that in conjunction with a live luminance value from an outdoor sensor.

    I'm thinking, is the room occupied, is it daytime, is the outside light below a certain value, and is it forecast for "not sunny".

    That would cover me for the baseline, the next branch would be to factor the solar data. Low/high winter/summer, and then use that to trigger a shade scene.

    Sound about right? Any gotchas to consider?

    Leave a comment:


  • langenet
    replied
    I am planning on using it with this device https://www.switch-bot.com for curtains that I have on a pole rode in my living room. I have a baby grand piano fairly close to the south facing window which we have to close the curtains to keep the heat of the sun off. Been thinking for quite some time how to automate but just found this device lately which may be my solution. There's an api apparently available... looking into it now...

    Robert

    Leave a comment:


  • sparkman
    replied
    Originally posted by jlikonen View Post
    Hi Al,

    This is really great script. Many thanks for sharing it.
    You're welcome Jari. Hope all is well.

    Cheers
    Al

    Leave a comment:


  • jlikonen
    replied
    Hi Al,

    This is really great script. Many thanks for sharing it.

    Leave a comment:


  • sparkman
    replied
    Originally posted by Moskus View Post
    Thanks for this, sparkman ! I've been meaning to write this script for ages. I found the page you found, and started the cleanup.

    Luckily I found your script when I was looking for som e of those constants, so it saved me the work!
    You're welcome!

    Leave a comment:

Working...
X