[SOLVED] DateTimeType is deprecated

The D in getID needs to be capitalized…

MyDateTimeItem.postUpdate(
	new DateTimeType(
		java.time.ZonedDateTime.ofInstant(
			java.time.Instant.ofEpochMilli(now.getMillis),
			java.time.ZoneId.of(now.getZone.getID, java.time.ZoneId.SHORT_IDS)
		)
	)
)

I also experienced the “deprecated-notification” today and wanted to code according to the new standards.
I however didn’t find how to convert the milliseconds back to a DateTimeType, as the wiki is also deprecated.
I want to post an update to a DateTime item.
How to do this?
My goal is to store the time when the wasing mashine starts and update another item when it finishes with the washing duration (starttime - now). Maybe you got a better idea of how to do this, if so, please let me know :slight_smile:

rule "test time difference"
when
	Time cron "*/10 * * * * ?"
then
	val calendar = java.util.Calendar::getInstance
	calendar.timeInMillis = now.millis - (Waschkueche_Waschmaschine_startZeit.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
	Waschkueche_Waschmaschine_letzteZykluszeit.postUpdate(new DateTimeType(calendar))
end

This works but I want to solve it according to the new standard.
Thanks for your help :slight_smile:

Maybe it is unusual to store time differences in a DateTime item.
I should maybe better use a Number item.
Can anyone show me how to format the Number item from milliseconds to hours and minutes in the sitemap?

Staying with a DateTime item will work for this. It makes the formatting easier, but you need to be mindful of the timezone…

Waschkueche_Waschmaschine_letzteZykluszeit.postUpdate(new DateTime(now.millis - (Waschkueche_Waschmaschine_startZeit.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli, DateTimeZone.UTC).toString)

If you setup your item label to be formatted like this, it should display in HH:MM:SS…

DateTime Waschkueche_Waschmaschine_letzteZykluszeit "Dish Washing Time [%1$tT]"

More formatting options can be found in here though… https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax

1 Like

Thank you very much, I got it working as I wanted it :slight_smile:

1 Like

I’m also having trouble with the deprecation of DateTime in the latest build using JSR223,
Can you give me a hint what the new coding standards would be?

I will give you an example and hope it helps:

Items:

DateTime Waschkueche_Waschmaschine_startZeit "Waschmaschine Startzeit [%1$tH:%1$tM Uhr]" (Waschkueche, Zeit)
DateTime Waschkueche_Waschmaschine_letzteZykluszeit "Letzte Waschdauer [%1$tH:%1$tM h]" (Waschkueche, Zeit)

Update item with current time (no change):

Waschkueche_Waschmaschine_startZeit.postUpdate(now.toString)

Get millis from item:

(Waschkueche_Waschmaschine_startZeit.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

Calculate time difference between stored time and now and update item with result:

Waschkueche_Waschmaschine_letzteZykluszeit.postUpdate(new DateTime(now.millis - (Waschkueche_Waschmaschine_startZeit.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli, DateTimeZone.UTC).toString)
1 Like

Thank you for your hint.
I came up with following solution to replace the deprecated DateTime.

from java.time import OffsetDateTime

events.postUpdate(_uitem, str(OffsetDateTime.now()))

Wouldn’t it be a good idea to update the documentation’s sample code? The sample code on the Rules page of the documentation is itself now deprecated… hardly a good start for those trying to fight their way through the absurd DateTimeType vs Joda DateTime wrangle for the first time.

1 Like

Yes, it would
You can do that yourself by following the link at the bottom of the docs page:

oh, thank you! I shall!

ETA: I have!

But I did not get this error !

Because OH has been updated since

1 Like

So, I need to update my system. but I afraid if I update it then I will got errors

What version of OH are you running now?
Yes, you may get errors (unlikely) and then we fix them, that’s part of the fun!

1 Like

Hello, I think I’m having some problems about the DataTimeType and I have to say that the manipulation of DataTimeType items it’s something new for me. I opened a new discussion because I didn’t want to go too off-topic in this one but this is certainly related.
My problem about extracting data from a DataTimeType item is described here


if anybody could help me about that it would be very appreciated!
Bye.

I have same issue with:

The method getCalendar() from the type DateTimeType is deprecated

this is after I upgrade OH from 1.8 to 2.3.

I have tried to changed

from

Time= (Sorted_Date.state as DateTimeType).calendar.timeInMillis

to:

Time= (Sorted_Date.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

but designer tell me that The method or field zonedDateTime is undefined for the type DateTimeType

Does anybody know why ? Do I need to import some library (which I removed after upgrade) ?

(Sorted_Date.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli

Please don’t user Designer any more. It is about two years out of data at this point and never worked correctly for OH 2. Please use VSCode with the openHAB extension.

Personally, I find it easier to convert DateTimeTypes to Joda DateTimes and work with that:

val jdt = new DateTime(Sorted_Date.state.toString)

Thank Rich !

I used “new” designer, suggested in migration tutorial:

### Eclipse SmartHome Designer
As mentioned above, there is a new Integrated Development Environment (IDE) for openHAB 2 configurations, Eclipse SmartHome Designer. The old openHAB Designer is not compatible with openHAB 2.

That needs to change. I thought all of those references were removed. I guess that one got missed.

I created a PR to switch that over to VSCode in the tutorial along with some additional updates I’ve been meaning to make.

Hi all,

I’ve also been trying to update the time check in my rule since DateType was deprecated.

Now I can’t get the if statement right.

    val BedTime = now.withTimeAtStartOfDay.plusDays(1).minusHours(7)
    if (now.isAfter(BedTime) && now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
        logInfo('Its after bedtime and before sunrise', '--> Bedtime is ' + BedTime + ' PM')
        postUpdate(LateNightState, ON)
    } else {
        logInfo('NOT after bedtime and before sunrise', '--> Bedtime is ' + BedTime + ' PM')
    } 

Event though the condition is true that it is after 5pm and before Sunrise tomorrow, how come the ‘if’ statement is not executed (i.e. to set LateNightState to ON)?

I’ve been wrestling with this all afternoon and I’m out of ideas.

If anyone can spot the problem it would be greatly appreciated.

Edit: Just as a long shot I changed the && to || and it did execute the if condition.

   if (now.isAfter(BedTime) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {

Now that does not make much sense since it is after 5 AND before sunrise, not OR

In case it helps anyone, here is the full rule (seems to be working)…

rule 'OpenHAB system started'
when
    System started
then
    postUpdate(LateNightState, ON) // Default this so thatthe lights come on
    postUpdate(NightState, OFF)
    postUpdate(DayTimeState, OFF)
    Thread::sleep(30000) // Give thesystem enough time to load
    logInfo('System started rule trigerred', '--> System started')
    val BedTime = now.withTimeAtStartOfDay.plusDays(1).minusHours(2)
    if (now.isAfter(BedTime) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
        logInfo('LateNightState', '--> Its after bedtime and before sunrise')
        postUpdate(LateNightState, ON)
        postUpdate(NightState, OFF)
        postUpdate(DayTimeState, OFF)
    } else {
         if (now.isAfter((Sunset_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli) || now.isBefore((Sunrise_Start.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)) {
        logInfo('NightState', '--> It is evening, switch on outside and inside lights')
        postUpdate(LateNightState, OFF)
        postUpdate(NightState, ON)
        postUpdate(DayTimeState, OFF)
        } else {
            logInfo('Daytime', '--> Daytime, switch all lights off')
            postUpdate(LateNightState, OFF)
            postUpdate(NightState, OFF)
            postUpdate(DayTimeState, ON)
        }
    }
 end