I use the following to run cron jobs for my lighting based on sunrise/sunset. In the example below, this would set the two variables to the hour and minute 15 minutes before sunset. This seems way over complicated to do something so easy. It is just “the way I figured it out originally” and I’ve left it alone since.
var Number sunsethourminus900 = Integer::parseInt(new DateTimeType((new DateTime((Astro_Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)).minusSeconds(900).toCalendar(null)).format("%1$tk"))
var Number sunsetminuteminus900 = Integer::parseInt(new DateTimeType((new DateTime((Astro_Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)).minusSeconds(900).toCalendar(null)).format("%1$tM"))
I now get the following error with the rules when they process:
The constructor DateTimeType(Calendar) is deprecated
Sorry I wasn’t being more clear. I have over a dozen different ones for all variety of triggers. I don’t want to adjust the binding itself. I need to do the math after the fact. More or less, I have a cron that runs everyvminutr which says if hour = hour and minute = minute do xyz.
Yes. I don’t disagree with it either, just not looking to do a rewrite right now. All I’m looking for is a better/newer way of doing those lines for now that doesn’t use depreciated code.
So the problem arises in getting the actual hour or minute value out of it once the math is done. I end up having to take the adjusted DateTime, fire it through toCalendar and put it into a new DateTimeType so that I can run the format() on it at the end. Putting the now Calendar type into DateTimeType is what is causing the barking. I know it seems very convoluted, it was the only way I could find (through trial and error) to get the results I wanted. Hence the post now of “how could I do this better”.
User wants to do X. // get a timestamp for sunset minus an offset
User doesn’t know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y. // deal with the calendar method being deprecated
User doesn’t know how to do Y either.
User asks for help with Y. // this post
Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve. // see above
After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn’t even a suitable solution for X.
If you insist on being confrontational to every thread I post, please just don’t reply if you see anything with my name on it. I asked a question, I said thank you for your answer but it wasn’t what I was looking for, you are persisting at this point only to cause confrontation. It’s not appreciated. Thank you.
Sometimes answers are not about you. They are about future readers of this thread.
The proper solution to this problem is to use the Astro binding as it was intended. And when you do that you can even get events from these offset time stamps.
I don’t care what you do. If you want to reject good advice that’s your business. I do care what future readers of this thread read and learn and make sure they understand the better way to solve this problem.
I also don’t recall being confrontational to any other posting you’ve made. But I make so many posts:
There’s a ZonedDateTime constructor nowadays that should be used (see deprecation). But as Rich pointed out you don’t need that constructor at all.
This should also work:
var Number sunsethourminus900 = (Astro_Sunset_Time.state as DateTimeType).zonedDateTime.minusSeconds(900).hour
var Number sunsetminuteminus900 = (Astro_Sunset_Time.state as DateTimeType).zonedDateTime.minusSeconds(900).minute
Are you really that passive/aggressive that you had to post your stats to some how show that you are superior? Do you really not realize that your reply in itself was confrontational?