Script for calculating solar angle of incidence on a wall

Posted on
Tue Apr 06, 2021 10:01 pm
jheddings offline
User avatar
Posts: 149
Joined: Dec 01, 2013
Location: Denver, CO

Script for calculating solar angle of incidence on a wall

Just passing along a script I've been using for calculating the solar angle of incidence on a wall... I'm currently triggering when the sun hits a particular wall to lower certain blinds. Typically, this is whenever the sun drops below 45 deg on a given wall.

The script requires the `ephem` Python module. You need to install this where Indigo will have access to it.

I run this once a minute to update several variables in Indigo:
  • solar_azimuth - current sun azimuth
  • solar_altitude - current sun altitude
  • solar_aoi_north - current AOI on north wall
  • solar_aoi_south - current AOI on south wall
  • solar_aoi_east - current AOI on east wall
  • solar_aoi_west - current AOI on west wall

Near the bottom of the script, you'll notice a few hard-coded values (330, 60, 150, 240). These are the directions (in degrees) that each surface is facing. You'll want to update them for your application.

When checking this value in an action or trigger, you'll need to make sure that the sun is above the horizon for the AOI to make sense. Even though the trigonometry works out correctly, an angle below horizon isn't very useful for most applications. Also, if the AOI is greater than 90, it means that the sun is behind the intended surface.

Last point for now - you can also run this outside of Indigo and just pass your latitude / longitude as command line arguments. This may be useful for checking values before assigning to variables in Indigo.

Code: Select all
import math
import ephem
import sys

try:
    import indigo
    (lat, lng) = indigo.server.getLatitudeAndLongitude()
    has_indigo_host = True

except ImportError:
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('lat', type=float)
    parser.add_argument('lng', type=float)
    args = parser.parse_args()

    lat = args.lat
    lng = args.lng
    has_indigo_host = False

################################################################################
def set_attr(name, value):
    if has_indigo_host:
        indigo_var = indigo.variables.get(name, None)
        if indigo_var is not None:
            indigo.variable.updateValue(indigo_var, str(value))
    else:
        print(name + ' = ' + str(value))

################################################################################
# pysolar has a built-in method for AOI, however it doesn't run on Python 2
# so we kind of hijack it here for use in this script with mods for our needs...
# https://github.com/pingswept/pysolar/blob/master/pysolar/solar.py

def get_incident_angle(obj_azm, obj_alt=90.0):
    tza_rad = math.radians(90.0 - solar_alt)
    slope_rad = math.radians(obj_alt)
    so_rad = math.radians((180 + obj_azm) % 360)
    taa_rad = math.radians(solar_azm)
    return math.degrees(math.acos(math.cos(tza_rad) * math.cos(slope_rad) + math.sin(slope_rad) * math.sin(tza_rad) * math.cos(taa_rad - math.pi - so_rad)))

################################################################################

obs = ephem.Observer()
obs.lat = str(lat)
obs.long = str(lng)

sun = ephem.Sun(obs)
solar_azm = math.degrees(sun.az)
solar_alt = math.degrees(sun.alt)

aoi_north = get_incident_angle(330)
aoi_east = get_incident_angle(60)
aoi_south = get_incident_angle(150)
aoi_west = get_incident_angle(240)

################################################################################

set_attr('solar_azimuth', solar_azm)
set_attr('solar_altitude', solar_alt)
set_attr('solar_aoi_north', aoi_north)
set_attr('solar_aoi_south', aoi_south)
set_attr('solar_aoi_east', aoi_east)
set_attr('solar_aoi_west', aoi_west)

Attachments
Screen Shot 2021-04-06 at 10.01.01 PM.png
Screen Shot 2021-04-06 at 10.01.01 PM.png (30.13 KiB) Viewed 1341 times

Posted on
Wed Apr 07, 2021 5:34 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Script for calculating solar angle of incidence on a wal

Nice. Thanks for sharing this.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 07, 2021 8:38 am
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Script for calculating solar angle of incidence on a wal

Nice!

I believe you could do something similar to this with Perry's Weather plugin.

"Sun tracking and orientation"

Although I am unsure if it requires the use of dark sky.. :?:

Thanks for the scripts!

--
Korey

Posted on
Wed Apr 07, 2021 1:06 pm
jheddings offline
User avatar
Posts: 149
Joined: Dec 01, 2013
Location: Denver, CO

Re: Script for calculating solar angle of incidence on a wal

Korey wrote:
I believe you could do something similar to this with Perry's Weather plugin.


Definitely! Perry's plugin has a nice "index" feature for providing a relative sun exposure on a surface. It does not use the Dark Sky data for sun position (from what I can tell). It would be a good alternative if you want to represent your walls as devices in Indigo.

Posted on
Tue Dec 21, 2021 11:47 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Script for calculating solar angle of incidence on a wal

I'm curious. In practice, how well has this worked for you? If it's a cloudy day and there's no sun getting through, I'm guessing the blinds are still lowered? Are there ways you mitigate that or is that not a bother to you?

Posted on
Thu Mar 31, 2022 1:49 pm
jheddings offline
User avatar
Posts: 149
Joined: Dec 01, 2013
Location: Denver, CO

Re: Script for calculating solar angle of incidence on a wal

Glad you asked (though I am late in replying)...

This has been working quite well for my use case. In general, it has worked just as expected. There are still certain times of the year where the sun moves in such a way that I have ad to manually close blinds, but for the vast majority of the time it "just works."

You are correct that even in cloud cover the blinds are lowered... In several rooms, we have multiple windows on different walls, so closing one wall doesn't cause too many issues on those days. I did try using Fantastic Weather inputs as trigger conditions to determine cloud cover, but it never caused enough of an issue for me to complete the work.

I should note that it took some time (using the Prometheus plugin) to tweak the right angles to use in triggers. Once they were set, it has been running for over a year without any changes.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests