[RELEASE] SunCalc Driver

I created a driver for calculating the position of the sun using the SunCalc.js library. I use this to automatically close my roller shades on the west side of the house when the sun starts to come through the windows.

The driver uses your hub's lat/long to calculate the position of the sun at the current time. You can choose how often you wish to update. I've found that the altitude doesn't change more than a degree in 5 minutes.

Hopefully this is helpful to someone!

Altitude is the angle up from the horizon. Zero degrees altitude means exactly on your local horizon, and 90 degrees is "straight up". Hence, "directly underfoot" is -90 degrees altitude.

Azimuth is the angle along the horizon, with zero degrees corresponding to North, and increasing in a clockwise fashion. Thus, 90 degrees is East, 180 degrees is South, and 270 degrees is West.

Using these two angles, one can describe the apparent position of an object (such as the Sun at a given time).

26 Likes

This is brilliant. I'm surprised you haven't received a bunch of comments. Perfect for managing shades and curtains. Props.

1 Like

@mluck not enough shade owners! We are all waiting on ikea!

1 Like

This is brilliant! I don't know how I missed this all this time. Thank you, this is going to simplify a bunch of rules I have and keep me from doing a bunch of variable math that just slows everything down.

You're @kings in my book!

Edit: The more I think about it, this should be rolled into the basic firmware of the hub. There are so many times when the position of the sun effects the climate control of a home, this seems so necessary in a smart home controller.

5 Likes

I don't know how I missed this too. I'm going to give this a shot. Auto adjusting my kitchen blinds will help in the summer.

Weird, Can't believe I missed this as well. Been using Lux for the last 5 years and it sucked for shades. Thank you.

Glad this is helpful!

I didn’t want to have to deal with seasonal calculations, so I thought I would go straight to the source of the issue: the sun’s position! The JS library has support for sun phases. Perhaps someday I’ll get around to adding that, but it is not strictly necessary and would be more of a display feature.

I agree, it would be nice if this was was built in.

2 Likes

@augoisms, I tried using the SunCalc driver in an action repeat loop to see if the sun was within an azimuth range before I took action to adjust a shade. Unfortunately, referencing The Sun (as you called it) within the action to pick up a changing azimuth didn’t update the value of that variable.

The only way I could get the azimuth variable to update was to mimic how you did it in the trigger event in your example by setting the preferences to auto update and setting the update interval to the frequency I was looking for.

That works, but I would rather address The Sun like any other device and get the variables updated at the time it was referenced. Am I doing something wrong or misunderstanding the intent of your work (which is really nice, BTW)

Don

@Donald There are two ways to get the value to recalculate: set an auto update interval or manually call the refresh command. Devices don’t generally refresh/update when you access their values. There is some external event that triggers an update.

For your case I think the easiest would be to use the auto update, otherwise you could call a refresh in your loop before evaluating .

However, I’m not sure that looping in an action until you reach a desired azimuth would be a good idea. If your other trigger is temporary, like a motion event, you could set a private Boolean and have your rule trigger on motion and azimuth change. You then evaluate the PB and the azimuth value.

Hope this helps!

Ahhh - that is the key to what I am not understanding - how do you call for a driver value refresh from a rule?

Run custom action, the driver is considered an actuator in this case.

No parameter type need be selected.

1 Like

Very interesting, @michael.l.nelson, and good to know for a lot of reasons. I always wondered how to get to all of the other variables and buttons in drivers. I’m certainly not complaining because the SunCalc driver @augoisms wrote is just what I needed for controlling our shades, but I still find it a little unusual that the driver doesn’t update when it is called, much like when I call on a rain sensor to tell me if the floor is wet or dry. If I call on a sun sensor, it seems it should update the sun’s position.

Thanks for the quick response and clear answer.

Don

1 Like

Just an update on my automation using the SunCalc driver. After a minor hack to turn the altitude variable in the driver to a shutter position variable that can be fed directly to the shutter driver with a value of between zero (closed) and 100 (open) so that the shutter casts a constant shadow throughout the arc of the sun from sunrise to sunset, the result was truly remarkable.

Here are the calculations I added to the driver and the way I determined the field of view for each shutter so that they can adjust individually every 15 minutes between sunrise and sunset.

Many thanks for @augoisms and the others who helped out!

5 Likes

Wow this sounds amazing, We have Serena Shades on the NW and SE sides of our house and Velux Skylight shades on the roof. I'd love to do (sort of) what you did.

Check : are shades open at all? (e.g. don't open them before we wake up in the morning and manually open them a little)
THEN
Start SunControl
Increment shades throughout the day as Sun moves
Else
End SunControl

The skylight shades might need their own rule that just says "close if Altitude >= X otherwise open"

Being able to accurately control the amount of sun coming into any room throughout the day is a nice lifestyle improvement that comes at the price of a little technology and some high school math. You can literally draw a line on the floor beyond which you don’t want any sun to come.

The SunCalc software was, of course, the key to success. By knowing the position of the sun and that line on the floor, you can position your shades to maintain that line throughout the day. I update the sun’s position every 15 minutes and adjust accordingly. That gives a variation on the sun line on the floor of a few inches without constantly adjusting the shades.

Having auto adjusting shades can be a little surprising to guests, so I used our Alexas to do a simple chime 5 seconds before each adjustment. The Alexas also pre announce any major shade movements - like when all go up 5 minutes before sunset so we can enjoy the view. Also if water is detected on the terrace before they all go down.

Fun stuff! Good luck with your project...

1 Like

@augoisms This is awesome! I just installed 3 Ikea shades on my west windows mainly for the purpose to auto close them when the sun starts beating down on those windows. This is going to help me immensely!

@Donald I'd love to see your RM set up for your shades. I made a protractor and was using my compass today and my wife was laughing at me. :slight_smile:

@jared.zimmerman Did you get your RM set up for your shades as well? I'd love to see that too.

Great work everyone!

1 Like

I'm going to give this a try today and see how it works. Any recommendations/suggestions?

I went https://www.suncalc.org/ to try and get a good approximation. We'll see what happens!

Nice!

Do your delays needs to increase (10,20,30, etc)? I’m not positive, but seems like they would all run in 10 min.

:man_facepalming:

You’re right. I’ll add a pause/wait in between as opposed to the delay. I wasn’t thinking clearly last night! Lol

Maybe move your private boolean first in case the pauses "delay" the setting of the PB until the very end.