Depreciated Constructor - What is a better way to do this

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

Can anyone suggest a better way to do this?

Most use the Astro binding and use the offset for the various positions of the sun; unless I’m missing something you are trying to accomplish.

1 Like

In the PaperUI you can set an offset to -15 Minutes to the Sunset#start to accomplish what you wish, I think.

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.

Ah, have you read @rlkoshak’s DP time of day series?

Wow, that is at least one unnecessary transform, probably two.

var Number sunsethourminus900 = new DateTime(Astro_Sunset_Time.state.toString).minusSeconds(900).millis

Why not? Just create a new Thing for each offset.

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”.

It looks a whole lot like the way to do it better is to create separate Astro Things with the offset.

I don’t want to do it that way. Thank you.

Then you don’t want to do it in a better way and this is really a case of The XY Problem.

1 Like

No, it’s me wanting to fix one line of code rather than completely rewrite the thing.

  • 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.

Sure looks like an XY Problem to me.

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:

maybe I’m forgetting. Though you are the only person who seems to have a problem with my posts. Hmmm, what’s the common denominator?

But as you wish. Consider yourself silenced. I’ll see no more posting from you.


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

e.g. with the following item:

DateTime Astro_Sunset_Time      "Sunset [%1$tH:%1$tM]"          <sunset>    { channel = "astro:sun:home:set#start" }

Thank you! I will make the change shortly.

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?