Determine time between sunset/sunrise

Been working on creating my own apps recent...moving away from RM. Got a few working and I'm pretty happy about that...now I'm trying in code to determine if the time is between sunset and sunrise. Of course I mean today's sunset and tomorrows sunrise. Any help on that?

Take a look at the webCoRE sources (piston file).

There are several tricky parts

  • dealing with DST
  • not having to wakeup just to figure on the next sunrise or sunset

I have found it pretty reliable.

start with getSunsetTime, getSunriseTime, getNextSunsetTime, getNextSunriseTime

Convert to epoch and do 'ordinary math' on those values.

Let's just say both of those answers are still a little bit above my paygrade. I've done some research into Date.parse and the format I'm seeing from location.sunset isn't even close to the formats I've found documentation for. I'm able to get the dates/times in this format.

Thu May 07 20:27:00 EDT 2020
Thu May 07 06:08:00 EDT 2020

I'm not overly concerned about what tomorrows "exact" sunrise time is so theoretically I could use today's time for tomorrow. But basically I want to know if NOW is between 20:27 and 06:08.

The format you're seeing is just a string representation of a java.util.Date, which is the type that location.sunrise and location.sunset are. For any work with these, I'd use the Java/Groovy methods for date/time whenever possible, with no need to parse() them into one since they already are one.

As an alternative to now(), which will give a Long in epoch time, you can use new Date(), which will return the current date/time as a Date object. This makes it easy to compare to the location sunrise/sunsets times since they are the same type.

Given that, I think something like this would work for the simplest case:

def currTime = new Date()
if (currTime > location.sunset || currTime < location.sunrise) {
    // it's between sunset and sunrise
}

I thought of things that were a lot more complicated before I thought of this (using timeOfDayIsBetween() to check whether the current time is between sunset and the next midnight or the previous midnight and sunrise) but then realized this should be basically the same thing; the hub calculates sunrise/sunset times at (or shortly after) midnight every day.

That being said, I'm sure the webCoRE solution is far more robust, and keeping everything in epoch time is convenient if you want to do arithmetic (though Groovy also overloads operators like + to make this easy: location.sunrise + 1 is the time of today's sunrise but with tomorrow's date, not really tomorrow's sunrise but may do in a pinch). Either would probably be better if you want to schedule something, and you'd need some more complicated code if you want to do offsets (though Hubitat has some SmartThings-esque methods to help you here). However, for a simple check at runtime (as you might do to emulate rules with IF (Time is between sunset and sunrise)... or built-in apps with similar restrictions), this seems like it would work. Someone please let me know if I'm missing anything obvious. :slight_smile:

1 Like

Fantastic, thank you! I've made great progress with the app!