Sunrise/Sunset time variable math

I was thinking about some "what if I could do this..." ideas and I came up with something beyond my basic programming capabilities... I'm stumped on how to pull this off so I'm tossing out to the brainiacs out there.

Basically, take each days sunrise time minus 45min and sunset time plus 45min, then do some math - sunset minus sunrise, to get a duration time. I need that duration time before the sunrise of each day.

I've tried various things in rule machine but nothing works out using the criteria mentioned earlier. But it's not like I'm some sort of power user either.

I noticed that Hubitat already computes those times if I enable "Display current times" in rule machine. So in the screenshot below, I would take 8:44pm minus 6:16am to come up with a total duration the pool pump was on. So that's pretty much it. I'd like the duration time, and in 00:00:00 format.

This is mission critical or anything, just an idea I had. Thanks.

You don't specify a trigger that will make those actions even start. Assuming that the trigger is Sunrise-45 minutes, the first IF action might turn on the pump, but for sure all of the other IF actions will register as false and the rule will end.

What you might do is look for one of the scheduling apps on the forum that let you turn things (switches, pumps, etc.) ON or OFF as specified times (absolute or relative to sun set/rise).

Example schedulers:

This isn't the rule I'm trying to create. It's being used as an example to show that Hubitat already calculates sunset/sunrise +/- times. Disregard the IF statements and just look at the times being shown. If I could grab those times instead of trying to calculate them with a rule, things would be simpler. I don't think I can though.

OK, got that part. But I'm still not clear on what you are trying to do, just figure out total time duration from your start and stop times, or calculate a set of absolute times based on a relative time plus/minus an offset, or something else.

Note that you can do something like this, where vTimeStop is a DateTime variable, and vTimeStopOffset is a number variable, if you just want to get the absolute time based on an offset to sun set/rise:

Set vTimeStop to sunset time(8:04 PM) plus vTimeStopOffset(30) minutes

1 Like

4 Likes

Holy cow, this is amazing. There was a lot more to this then I realized. Let me work on this later this evening.

Thanks @John_Land !

Success.

What started out as an idea a week ago has turned into a completed product. A countdown timer based on the changing difference between sunrise and sunset times instead of a static time period.

image

The remaining time is counted down on the right side and a colored bar moves across as the countdown progresses.

I had to copy one of the local variables to a hub variable and create a connector so it would be attached to a device. I could then send that to Home Assistant via Maker API which then could be put on a timer bar card on my dashboard. The rule runs every morning at 6am.

Could I have just done everything in Home Assistant? Maybe with more effort, but IMHO, Hubitat and rule machine are very powerful, easier to use and to understand. Hubitat is the workhorse of my automations and HA is just used for dashboards and data graphing, or in other words, visual stuff that Hubitat isn't as good at - yet.

The idea came about when I was wondering if a light that I had on a timed rule should still be on. Did it miss the off command, did the rule trigger properly, etc? Instead of digging in with troubleshooting, I can now see if a timer is or should be running by a visual representation of it instead of an on or off icon. If it is running it shows the remaining time, otherwise it shows idle.

image

There is probably a simpler way of doing this but anytime I can get an idea to actually work, I'll take that as a win and that's the fun I have with all of this automation stuff.

Thanks again @John_Land for your assistance with this!

2 Likes