Issue with rule coming from openhab 1 -> 2

Hello,

I’ve been using openhab1 for a couple years now and really love it. Recently I decided it was finally time to upgrade to openhab2 and I’m running into an issue with changing toDateMidnight() to toDateTimeAtStartOfDay()

I had the following rule in OH1 to give me some daily min/max temps:

rule "Update Nest Temperature Min- and Max values"
    when
        Item  living_room_ambient_temperature_f received update
    then
        var Number Min
        var Number Max
        var String tmp
        var SimpleDateFormat df = new SimpleDateFormat( "hh:mm a" )

        if (living_room_ambient_temperature_f.state instanceof DecimalType) {
            Min = (living_room_ambient_temperature_f.minimumSince(now.toDateMidnight, "rrd4j").state as DecimalType)
            tmp = (Math::round(Min.floatValue*10.0)/10.0) + " °F (" + df.format(living_room_ambient_temperature_f.minimumSince(now.toDateMidnight, "rrd4j").timestamp) + ")"
            postUpdate(living_room_ambient_temperature_f_Min, tmp)
            Max = living_room_ambient_temperature_f.maximumSince(now.toDateMidnight, "rrd4j").state as DecimalType
            df = new SimpleDateFormat( "hh:mm a" )
            tmp = (Math::round(Max.floatValue*10.0)/10.0) + " °F (" + df.format(living_room_ambient_temperature_f.maximumSince(now.toDateMidnight, "rrd4j").timestamp) + ")"
            postUpdate(living_room_ambient_temperature_f_Max, tmp)
        }
    end

When I moved it to OH2 I received a warning about “toDateMidnight()” being depreciated in my log. Google lead me to the following page where they say to replace it with “toDateTimeAtStartOfDay()”:

http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDate.html#toDateTimeAtStartOfDay(org.joda.time.DateTimeZone)

I did so, and now the rule read like this (included line numbers so you can match to the line provided by the error to come):

 821 rule "Update Nest Temperature Min- and Max values"
 822     when
 823         Item  living_room_ambient_temperature_f received update
 824     then
 825         var Number Min
 826         var Number Max
 827         var String tmp
 828         var SimpleDateFormat df = new SimpleDateFormat( "hh:mm a" )
 829 
 830         
 831 
 832         if (living_room_ambient_temperature_f.state instanceof DecimalType) {
 833             Min = (living_room_ambient_temperature_f.minimumSince(now.toDateTimeAtStartOfDay, "rrd4j").state as DecimalType)
 834             tmp = (Math::round(Min.floatValue*10.0)/10.0) + " °F (" + df.format(living_room_ambient_temperature_f.minimumSince(now.to
 835             postUpdate(living_room_ambient_temperature_f_Min, tmp)
 836             Max = living_room_ambient_temperature_f.maximumSince(now.toDateTimeAtStartOfDay, "rrd4j").state as DecimalType
 837             df = new SimpleDateFormat( "hh:mm a" )
 838             tmp = (Math::round(Max.floatValue*10.0)/10.0) + " °F (" + df.format(living_room_ambient_temperature_f.maximumSince(now.to
 839             postUpdate(living_room_ambient_temperature_f_Max, tmp)
 840         }
 841     end

However I now get the following error in my openhab.log:

2018-01-04 18:38:54.307 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Update Nest Temperature Min- and Max values': 'toDateTimeAtStartOfDay' is not a member of 'org.joda.time.DateTime'; line 833, column 67, length 26

Does anyone have any idea how to fix this?

The method toDateTimeAtStartOfDay isn’t in the org.joda.time.DateTime class, but instead is in the org.joda.time.LocalDate class.

The method you want is withTimetStartOfDay. Or something like that. On my phone so it isn’t easy to look up.

Thanks for the quick reply!

I tried adding:

import org.joda.time.LocalDate

to my rules file and I still get the error. Is there something I should be importing or otherwise doing to fix this error?

Found it:

Thanks!