[SOLVED] DateTimeType is deprecated

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
  1. It is easiest to just create a new DateTime with the String from your DateTimeType.

if(now.isAfter(BedTime) && now.isBefore(new DateTime(Sunrise_Start.state.toString)))
But you problem is as follows.

Sunrise_Start gets calculated around midnight every day. You don’t show when this Rule triggers but lets assume it triggers sometime after BedTime but before midnight. At that time, Sunrise_Start is still set for TODAY and sunrise today happened before BedTime so the if statement will never evaluate to true because it will never be both after BedTime and before Sunrise_Start during that time period.

You need to break the if statement up and trigger your rule accordingly to:

  • test if now is between BedTime and midnight
  • test if now is between midnight and Sunrise_Start

Note that Sunrise_Start doesn’t get updated exactly at midnight. Instead it usually gets updated 30 seconds or so after midnight.

Thank you for the advice and the clear explanation of why my original design was going to run into problems owing to sunrise being calculated after midnight by the Astro binding. It is much clearer now.

So I re-designed my logic accordingly and borrowed pretty heavily from your design pattern for Time of Day.

The rule was intended to calculate the correct time of day after OH had been offline during which time an event may have ocurred, so the system would need to recalculate it on startup.

So based on what I stole from your design pattern, it seems to be working so far.

I just have a question about the case now.isAfter(bed_start) which does not have an && condition. Is this because of the ‘midnight’ phenomenon and a second condition is not needed?

    // Convert the Astro Items to Joda DateTime
    val day_start = new DateTime(Sunrise_Time.state.toString) 
    val night_start = new DateTime(Sunset_Time.state.toString)
   
    var curr = 'Unknown'

    switch now {
  	    case now.isAfter(morning_start)         && now.isBefore(day_start):       curr = 'Morning'
  	    case now.isAfter(day_start)             && now.isBefore(evening_start):   curr = 'Day'
  	    case now.isAfter(evening_start)         && now.isBefore(night_start):     curr = 'Evening'
  	    case now.isAfter(night_start)           && now.isBefore(bed_start):       curr = 'Night'
  	    case now.isAfter(bed_start):                                              curr = 'Bed Time'
        case now.isAfter(after_Midnight_start)  && now.isBefore(morning_start):   curr = 'After Midnight'
    }

Do you think I would need the extra condition as follows? (I’ve tried to understand it per the example but I’m not sure I’ve got it right.

    switch now {
  	    case now.isAfter(morning_start)         && now.isBefore(day_start):       curr = 'Morning'
  	    case now.isAfter(day_start)             && now.isBefore(evening_start):   curr = 'Day'
  	    case now.isAfter(evening_start)         && now.isBefore(night_start):     curr = 'Evening'
  	    case now.isAfter(night_start)           && now.isBefore(bed_start):       curr = 'Night'
  	    case now.isAfter(bed_start)             && now.isBefore(midnight_start):  curr = 'Bed Time'
        case now.isAfter(after_Midnight_start)  && now.isBefore(morning_start):   curr = 'After Midnight'
    }

So far it worked for Night Time and for Bed Time, as it is 22:40. I won’t last until midnight so I’ll check the logs in the AM to see if the midnight condition worked

image

Thanks again for your feedback.

1 Like

Correct.

That would work but it isn’t necessary. Nothing changes until after midnight. And once you get to midnight bed_start and all the rest of the times will have moved to the new day and the last clause in the switch will execute.