Time with dynamic offset?

Hi, I’m currently evaluating sharptools as a replacement for webcore. I have a webcore rule for turning lights on and off around sunrise/sunset, but there is an offset which is calculated each day, so throughout the year the lights will come on longer before sunset in the winter, and shorter in the summer, and it works really well with how bright it is out. The logic is based on adding/subtracting a number of minutes from sunrise/sunset as follows:

if time is after addMinutes($sunset, -(2.5 * $month ** 2 - 30 * $month + 120)) OR time is before addMinutes($sunrise, (2.5 * $month ** 2 - 30 * $month + 120)) [[TURN ON LIGHTS]]

This evaluates every minute. Any ideas how I can replicate this logic/functionality in sharptools?

Math calculations can be performed with the Expressions feature, but there’s not currently an official way to use dynamic schedules.

There’s a feature request for being able to use variables in the Delay block which I believe would be needed for what you’re looking for. Feel free to cast a vote and bump the thread with your use-case.

The last piece would be how to schedule this and I suspect that just doing it once or twice a day would be sufficient as you could have the rule trigger every day at 12:01 and then have it calculate how long you need to wait until your desired time.

I would also add that the sunset / sunrise are not exposed as variables directly, but there’s several other ways you could get that value.

I’m doing something very similar to this currently, except in my use case I’m factoring in the current weather conditions (turns lights on earlier if its raining/snowing vs. sunny) and adjusting to todays sunset time.

The way I’m doing this is:

– grabbing todays sunset time from the openweather api each day and storing as a variable.
– when i grab the current weather throughout the day, i use an expression to determine what time for the lights to come on (basically setting an offset to todays sunset time) based on current weather conditions
– ive got an ‘every one minute’ rule that runs which turns a variable on/off ever minute
– i check the current time against the ‘offset time’ and if they equal, a variable turns to true, which triggers the rule to turn on evening lights

A few steps involved, but its been functioning for a couple of months with no issues.

1 Like

Thanks, this helps a lot! I may also try and factor in weather! Can I ask for a bit more info on the sunset time. I’ve just signed up for open weather, but waiting for my api key to activate. What format is the sunset/rise in? Do you need to convert it? And then how to you calculate the offsets?

Also, how do you compare the current time with the offset time?

If you haven’t read this post, I’d highly suggest doing so – tons of good stuff there.

The sunset/sunrise are returned as an epoch (learn more here)

So, you can easily store it as an epoch or convert it to serve it on a dashboard, etc., your choice.

Here’s how I store both the sunset as an epoch and as a ‘display’ time for a dashboard – put this after your open weather API call:

Calculating the offsets would be based on your logic, for mine I use these based on the weatherID at the time and the less sunny the weather, the greater the sunset offset (you can get the weatherIDs here):

You can convert your offset epoch by doing this:

In order to trigger it, I use an ‘Every One Minute’ rule – which has its pros and cons – and check to see if the current time is equal to the sunset offset time. If it is, I turn another variable true, which triggers my rule which adjusts various things (lights, thermostats, etc.) around the house:

Hope this helps!

1 Like

You hero, thank you so much for this. I think I’ve got my logic all implemented now, just need to see what happens around sunrise and sunset.

Thanks again!