Using other times for sunset.

Posted on
Sun Jan 05, 2020 12:49 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Using other times for sunset.

I recently replaced a stand-alone chicken-coop door operator with a custom system I designed. The old system had n o external interfaces and could only set open and close times based either on clock-time, and./or light level.

With my new system, since I control the door operator from Indigo, I have a lot more leeway. Initially,I am basing the open and close times on sunrise & sunset plus a small wait factor. Mornings are not really an issue. But in the evening, the chick come back to roost when they think it is dark. And, depending on weather and season may be well before or after Indigo's idea of sunset. In the caveat hand, I am only worried about closing too early, not too late - that is the case where the chicks get locked out for the night.

So... I hacked a small script I found online to calculate various astronomical times (see below) and seem to be stumped as how to proceed to use these times in a schedule. (Yes, I guess I could run the script externally and just have it trigger the close action. But, I'd rather try to stay in a more Indigo mind.)

Anyway, the issue is simply, how to I get Indigo to execute my action at, say astronomical twilight (true dark). I have the time, and can convert it to a number of formats, and can place it in a variable. I just can't wrap my mid-winter hibernating brain around how to make the final connection.

Ideas, suggestions and pointers most welcome.

Here is the code
Code: Select all
#!/usr/bin/python2.7

import ephem

# Make an observer
loc      = ephem.Observer()

# PyEphem takes and returns only UTC times. 15:00 is noon in locericton
loc.date = str(datetime.datetime.utcnow())  # "2020-01-05 16:39:00"

# Location
loc.lat = str(xx.xxxx)     # Note that lon should be in string format
loc.lon = str(xx.xxxx)      # Note that lat should be in string format

# Elevation in metres
loc.elev = 134

# To get U.S. Naval Astronomical Almanac values, use these settings
loc.pressure = 0
loc.horizon = '-0:34'

sunrise=loc.previous_rising(ephem.Sun())  # Sunrise
noon   =loc.next_transit   (ephem.Sun(), start=sunrise)  # Solar noon
sunset =loc.next_setting   (ephem.Sun())  # Sunset

# We relocate the horizon to get twilight times
loc.horizon = '-6' # -6=civil twilight, -12=nautical, -18=astronomical
beg_twilight=loc.previous_rising(ephem.Sun(), use_center=True)  # Begin twilight
end_twilight=loc.next_setting   (ephem.Sun(), use_center=True)  # End twilight

print("Current time is:       %12s" % ephem.localtime(loc.date))
print("begin civil twilight:  %12s" % ephem.localtime(beg_twilight))
print("sunrise:               %12s" % ephem.localtime(sunrise))
print("noon:                  %12s" % ephem.localtime(noon))
print("sunset:                %12s" % ephem.localtime(sunset))
print("end civil twilight:    %12s" % ephem.localtime(end_twilight))

Posted on
Sun Jan 05, 2020 12:59 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Using other times for sunset.

How about an alternate way to do it?

Here's a script I wrote to set the sun azimuth and elevation into variables. It runs every 10 minutes. I have a trigger that closes a shade when those numbers meet a specific criteria. Maybe you could just have a trigger on the elevation getting to -6?

Code: Select all
import ephem

varAzimuth    = indigo.variables[717359973] # "Azimuth"
varElevation  = indigo.variables[719196174] # "Elevation"

(latitude, longitude) = indigo.server.getLatitudeAndLongitude()

obs=ephem.Observer()
obs.lat=str(latitude)
obs.long=str(longitude)
sun = ephem.Sun(obs)

azimuth = float(sun.az) * 57.2957795      # convert radians to degrees
elevation = float(sun.alt) * 57.2957795

indigo.variable.updateValue(varAzimuth, str(azimuth))
indigo.variable.updateValue(varElevation, str(elevation))

#indigo.server.log(u"Latitude: {}, Longitude: {}".format(latitude, longitude))
#indigo.server.log(u"Sun Azimuth: {}, elevation: {}".format(azimuth, elevation))

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Jan 06, 2020 10:06 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Using other times for sunset.

FlyingDiver wrote:
How about an alternate way to do it?

Thanks for the response and the suggestion. Though, on reflection, as long as I am running the script every 5 min, or so, I could just as well have the script trigger the action. The problem with that, and the advantage to your approach, is that the way you suggest, everything is visible in Indigo, no background script magic. I could simplify your approach and just have a variable isReallyDark and have the script toggle it true at sunrise and false at astronomical sunset. In other words just compare the real time to the sunrise/sunset times and make the change when local time is greater than the last event time. Then have a trigger fire when isReallyDark becomes true.

I could also just run the script once each day at midnight and place the astronomical sunset time into a variable, then have a trigger that fires when local time is greater than sunset time. But, I am not sure how to do the comparison. I could use minutes past midnight in the script, but how do I get Indigo to calculate that to make the comparison?

I'm thinking, I'm thinking....

Posted on
Mon Jan 06, 2020 10:10 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Using other times for sunset.

Seems to me that having useful data in a variable (like the azimuth and elevation) could also be used for other automations (like my window shade). Then the trigger is specific to the task to perform, not the data script. Just a thought...

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Jan 06, 2020 12:53 pm
Swancoat offline
Posts: 503
Joined: Nov 20, 2009
Location: Houston

Re: Using other times for sunset.

Just FYI, there already exists a plugin that provides solar angle and azimuth. (I use this to set the shade height in my office in the evenings).

http://www.indigodomo.com/pluginstore/110/

http://nerdhome.jimdo.com

Posted on
Tue Jan 07, 2020 2:53 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Using other times for sunset.

Swancoat wrote:
Just FYI, there already exists a plugin that provides solar angle and azimuth. (I use this to set the shade height in my office in the evenings).
http://www.indigodomo.com/pluginstore/110/

Thanks, That looks like it will do the trick. I'll give it a try.

BTW, I have, more-or-less, figured out how to do my own time scheduling, at least for recurring daily events. Once a minute run a script that calculates the current number of minutes since mid-night (0 to 1440) and place that in variable A. Separately, at 00:00:00 every day , calculate the time, in terms on minutes-past-midnight, of whatever event you want to trigger on and place that in variable B. Then trigger on A becomes greater than B.

You could do the same thing for non-recurring events by using epoch time instead of minutes-past-midnight.

Posted on
Wed Jan 08, 2020 9:06 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Using other times for sunset.

berkinet wrote:
Swancoat wrote:
Just FYI, there already exists a plugin that provides solar angle and azimuth....
...Thanks, That looks like it will do the trick. I'll give it a try.....

Works perfectly for what I wanted. Thanks for the tip.

Posted on
Wed Jan 08, 2020 11:28 am
Swancoat offline
Posts: 503
Joined: Nov 20, 2009
Location: Houston

Re: Using other times for sunset.

berkinet wrote:
berkinet wrote:
Swancoat wrote:
Just FYI, there already exists a plugin that provides solar angle and azimuth....
...Thanks, That looks like it will do the trick. I'll give it a try.....

Works perfectly for what I wanted. Thanks for the tip.


np, I found it on some random browse through the plugin store literally the day before I planned to try and write the same plugin. It was like finding $100 in my pocket.

I didn't follow the rest of the thread super closely, but as for some timer after dark to get to true dark etc..., what did you finally trigger on? (I was thinking some negative level of solar angle (like -5 deg or something) would represent a 'true dark' and would automatically correct for seasonal variations).

http://nerdhome.jimdo.com

Posted on
Wed Jan 08, 2020 12:18 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Using other times for sunset.

Swancoat wrote:
... as for some timer after dark to get to true dark etc..., what did you finally trigger on? (I was thinking some negative level of solar angle (like -5 deg or something) would represent a 'true dark' and would automatically correct for seasonal variations).

According to what I found online, true dark, I.e. after the end of twilight, is with the sun at -18º - But even that depends on what is to the west of you. Twilight lasts far longer on the IUS east coast than on the west coast.

Anyway, after a couple of days testing, I'm going with a value of -4. But, consider that my case is a bit unusual. I am trying to determine the correct time to close the chicken coop. Too early and I lock some of the chicks out - and the fox has dinner, too late and the fox has a really fine meal. So, I have to predict that level of darkness when my chicks return to the roost. I am no chicken expert, so it may well be a social as well as genetically determined event. But, whatever determines it, I need to figure out how to predict it. BTW, I have noticed that weather, dark overcast, and temperature have an effect as well. So, in the end, I might add a light sensor to the equation and see what the light level is with the sun at -4º. Then I can trigger an early closing if it gets really dark before the sun is -4º below the horizon.

If I were trying to manage curtains, shades or awnings, things would be very different and azimuth would play a factor. But, for me it is just light level.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 19 guests