Sunrise sunset script using http://sunrise-sunset.org/

13 replies [Last post]
mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012

I've come to the conclusion that earthtools is gone, so I've modified the sunforcast script to use sunrise-sunset.org.

It's not as pretty as Brett would write but it's functional, however if you feel you can improve on it, feel free

AttachmentSize
sunForcastApplet.lua1.91 KB
mike6
Offline
United Kingdom
Joined: 1 Mar 2011
I think Earthtools have moved

I think Earthtools have moved to here:  new.earthtools.org

Will this still work?

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
new.earhtools

Dunno, not tried it. I am however waiting for Brett to chip in with a cleaner version :)

 

 

kema01
Offline
MAIDSTONE, United Kingdom
Joined: 8 Jul 2013
Fudge factor

Mark I never had a use for the sun rise/set app so far, but thought it would be a good idea for turning on and off our outside Christmas lights. Well I am not a programmer but got your applet working second attempt in conjunction with Brett’s automatic lights applet. What surprised me was how light it was when the lights turned on and subsequently when they turned off. To get round this I am going to  enter a latitude further south than I really am (fudge factor). Obviously this will not work for all the year.

What would be great if there was a way of having a plus or minus time padding factor. As an example if the sun sets at 16:00 a 30min delay could be added so the lights turn on at 16:30 or sun rise at 08:00 but lights go off at 07:30.

 

Kema...

brett
Offline
Providence, United States
Joined: 9 Jan 2010
To do a better job we need a

To do a better job we need a JSON parser for LUA to be bundled as part of the HAH build.
There are some options.  One written in pure LUA

Or a fast C based one with LUA bindings.

I'm thinking the C one would be a nice addition.  Its a been a while since I have done anything HAH so I'll dust off my trustly beaglebone that has been gathering my solar array feed and spewing xAP without a fault all year and give it a little upgrade as a xmas present.

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I added lua-cjson to the

I added lua-cjson to the portable distribution - however on my debian BeagleBone the packge is already setup and available for usage. COOL.

root@beaglebone:~# apt-get install lua-cjson

Making it use the new json decode is easy however as the reply is in UTC i need to the times back into localtime so they are meaningful and this is where things break down for me.  The penlight library just isn't handling this correctly - after burning way too many hours debugging somebody elses code i threw in the towel for today.

The fetching side changes are easy enough ....

local cjson=require("cjson")

function lookupSun()
   local now=os.date("*t")
   local url = string.format("http://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0",latitude,longitude )
   local response,statuscode = http.request(url)
   if response == nil or statuscode ~= 200 then return end
   t = cjson.decode(response)
   if t.status ~= 'OK' then return end
   sunrise = t.results.sunrise
   sunset = t.results.sunset
end

The only change on the PARSE side it remove the format mask and it will deal natively with the ISO8601 format being returned.

   local df = Date.Format(nil)

The UTC->localtime conversion that's another story....

Brett

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Hi Mike,I had a look at

Hi Mike,

I had a look at new.earthtools.org that doesn't seem to be functioning either

Brett,

I knew you'd have a dabble, nice work as ever

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Kema,would a cron job not do

Kema,

would a cron job not do for the couple of weeks you'll have the lights up?

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
I agree Mark a few lines of

I agree Mark a few lines of cron script seem to be the easiest way for me too.

kema01
Offline
MAIDSTONE, United Kingdom
Joined: 8 Jul 2013
You are both absolutly correct.

You are both absolutly correct, a couple of lines added to cron would do.

I was trying to justify to myself a reason to use the sun rise/set apps and then discovering it was turning on the lights about half an hour before it was dark.

kema...

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
I used the earthtools version

I used the earthtools version for a while when I was out of the country for an extended period. But as you say I had to tell porkies about my Lat/long in order to make the lights work at realistic times. Then, one year, while I was away the whole site went belly up and I had to remote in and set the whole thing up again using cron. As someone said cron is more reliable because it doesn't rely on an external data source.

In the end I decided that it was just too much bother.

 

I now use something like this:

#01 20 1-7 4 * aliasmsg "rf 4 on"
#15 20 8-16 4 * aliasmsg "rf 4 on"
#29 20 17-22 4 * aliasmsg "rf 4 on"
#45 20 23-30 4 * aliasmsg "rf 4 on"
#47 20 23-30 4 * aliasmsg "rf 5 on"
#00 21 1-8 5 * aliasmsg "rf 4 on"
#02 21 1-8 5 * aliasmsg "rf 5 on"
#15 21 9-16 5 * aliasmsg "rf 4 on"
#17 21 9-16 5 * aliasmsg "rf 5 on"
#30 21 17-25 5 * aliasmsg "rf 4 on"
#32 21 17-25 5 * aliasmsg "rf 5 on"

and just un rem the lines as and when I need them.

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
I don't see any rf x off in

I don't see any rfx off in that cron script, then again I don't se any REM statements either :)

As for the sunrise/set script. It works for me but I am controlling inside lights, it gets dark inside around sunset. I do have the advantage of living in UTC 0  though

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Ah, yes that's just a sample

Ah, yes that's just a sample from the cron to show the section used to change the timings as the evenings get lighter. The off commands go every night irrespective of whether the lights are being controlled by the HAH (good hint to the kids to go to bed). The # is in effect a rem because those lines are not run until I delete the # from the beginning of the line.

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Ah yes, sorry Alan, forgot #

Ah yes, sorry Alan, forgot # was for a comment

Hardware Info