Testing time in custom app

Thanks to the help from forum members I’ve my first knowledge hurdle and laid out the framework for a custom app.

Now I’m curious what is the best way to add a time constraint. In Rule Machine it is easy to add sunrise/sunset to the triggers. Without using RM how would you add similar functionality to a custom app?

I can do something like this:

def currentTime = new Date()
def isNight = (currentTime < location.sunrise || currentTime > location.sunset)

Is there a better way in HE? Is there an input type I can use to add a configuration item or would that need to be made from scratch?

For my current app it isn’t difficult to add the above code but I don’t want to get into a bad habit of there is something better.

Found out you can also subscribe to sunrise/set.

If location.sunrise and location.sunset work, they're probably OK, but I haven't used them. This the way (see the second post, not the first, but you'll have to read both to see the full context) I would do it:

https://community.hubitat.com/t/getsunriseandsunset/4595?u=bertabcd1234

I would not subscribe to the location's sunrise and sunset events unless you need that to "trigger" something in your app. If your app is being caused to run by something else and you just need to check the time whenever that happens to be, checking the time like above would be sufficient. If you provide a way for users to run your app at sunrise or sunset (or that but with offsets), then you'd need the subscription.

1 Like

I ended up subscribing and setting a global variable. There are also events that run at those times. It has evolved quite a bit in the last day or so.