Turn things ON/OFF at Sunset or Sunrise

Turning tings ON or OFF at Sunset or Sunrise using an IOT is very simple to accomplish. I am using the Particle Photon to do that.

Timelord, and Dusk2dawn both libraries work, but I decided to use this library maintained by Peter Buelow listed here:
https://github.com/buelowp/sunset
This library is not available in the Particle Web IDE, so you will need to install it. Just click the plus (+) sign top-right corner of the IDE, two files will be added, just change their name to sunset, paste their respective code from the library listed above, that’s it.

Now convert hours to minute, so 24 hours is equal to 1440 minutes. This library provides sunrise and sunset in minutes, let’s do the work using minutes.

int eMinutes = Time.hour() * 60 + Time.minute(); // Current hours plus minutes to minutes

This variable, eMinutes, will track the elapsed time since midnight in minutes. Two if(s) like so, and you are done:

if (eMinutes >= Sunrise && eMinutes <=  Sunset)       //  Turn things ON at sunrise 
else if (eMinutes >= Sunset || eMinutes  <=  Sunrise) // Turn things OFF at  sunset 

If you are interested to find out the meridian or mid-point, just add Sunrise and Sunset and divided by 2.

int meridian = Sunrise + Sunset / 2;

Just in case you want to convert eMinutes back to hours and minutes, do the following:

int cHours   = eMinutes/60;
int cMinutes = eMinutes%60;
4 Likes

Thanks for the shoutout. It should be available for the web and VSCode, but I’ll look into that. It might be a few releases behind.

Latest revision is uploaded and should be visible to both the Web and VSCode library systems.

Nice!
But I am using since about 6 years a very simple system to perform this task:
In IFTTT you can find a function sunset/sunrise, based on your location.
Then you can make a connection to your Particle device and call a Particle.function when the sun rises or sets.

Works great without the need for another library.

:wave: :mask:

Yes, the library is available now in the Particle Web IDE, thank you very much for your quick update. Your library is well maintained and well documented. I just updated my project, and it works just fine. By the way, I am using this library on my Personal Weather Station (PWS) that is feeding data to wunderground.com:

https://www.wunderground.com/dashboard/pws/KCAPALMD170

The two like upside down cups, one is the Davis 6450 Solar Radiation Sensor, the other one is the Davis 6490 UV Index Level Sensor, now the Solar Radiation sensor generates some random spikes at night that I could not figure out how to control, after the sunset it should be zero(0) until the sunrises. Using this library I just turn both sensors OFF at sunset, and ON at sunrise.

Here are some pics how the PWS looks like:




3 Likes

Nice to know you can use IFTT to turn things OFF/ON at sunset and sunrise, I guess there is more than way to skin a cat. The more we know how to do something, the better.

I love it. I have a PWS as well, though I’m not feeding anyone just yet. It’s just an MQTT broadcast to the house. I also have a bunch of colorful LED setups around the backyard that use that library to know when to turn on and off. IFTT works as well, but it’s a matter of preference.

Little off topic but care to share some info about your build?

At this moment I am working on a web page where I will post code, instructions, etc., how to create a Personal Weather Stations. I will post a diagram as well, tips and trips, etc. So stay tuned.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.