toDateMidnight() from type DateTime is deprecated - Validation issue in Rule

Hi everyone
I get the following error when starting openHAB:

2020-01-01 14:49:41.627 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'Strom.rules', using it anyway:
The method withDayOfMonth(int) from the type DateMidnight is deprecated
The method toDateMidnight() from the type DateTime is deprecated
The method plusMonths(int) from the type DateMidnight is deprecated
2020-01-01 14:49:41.631 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Strom.rules'

Here is my setup:

openhabian installed on Raspberry Pi 4 Model B
Raspbian GNU/Linux 10 (buster)
Linux 4.19.75-v7l+
openHAB 2.5.0-1 (Release Build)
InfluxDB v1.7.9

Strom.rules

rule "Stromverbrauch+Kosten aktueller Monat+Prognose"
    when
        Time cron "35 */10 * * * ?" //alle 10 Minuten zur 35. Sekunde
    then
    //Aktuell
    var Number vStromPreis180   = StromPreis180.state
    var Number vKWHumrechnung   = 3600000
    var Number vStrom = ZaehlerStromTarif0.state  //mit kWh
    vStrom = vStrom / vKWHumrechnung //ohne kWh
    var Number vStromHistory = (ZaehlerStromTarif0.historicState(now.toDateMidnight.withDayOfMonth1), "influxdb").state  as DecimalType) //History Wert Mitternacht zum Monatswechsel
    var Number vStromThisM = vStrom - vStromHistory
    var Number vStromKostenThisM = vStromThisM * vStromPreis180 / 100
        
    postUpdate(ZaehlerStromThisM, vStromThisM)
    postUpdate(ZaehlerStromKostenThisM, vStromKostenThisM)
    //Prognose
    var Number TimestampManfang          = now.toDateMidnight.withDayOfMonth(1).millis
    var Number TimestampMende            = now.toDateMidnight.withDayOfMonth(1).plusMonths(1).millis
    var Number SecondOfMonth             = (now.millis - TimestampManfang ) / 1000      //aktuelle Sekund des Monats
    var Number SekundenImM               = (TimestampMende - TimestampManfang) / 1000   //Anzahl derSekunden des Monats
    var Number vZaehlerStromThisM        = ZaehlerStromThisM.state  //mit kWh
    vZaehlerStromThisM = vZaehlerStromThisM / vKWHumrechnung //ohne kWh
    
    var Number vZaehlerStromThisMp       =   vZaehlerStromThisM * SekundenImM / SecondOfMonth
    var Number vZaehlerStromKostenThisMp =   vZaehlerStromThisMp * vStromPreis180 / 100
    postUpdate(ZaehlerStromThisMp, vZaehlerStromThisMp)
    postUpdate(ZaehlerStromKostenThisMp, vZaehlerStromKostenThisMp)
end

The rule works great. What do I have to change so that the error message no longer comes?

thx

This method was deprecated a long time ago. You need to use withTimeAtStartOfDay to get midnight. According to the docs, withDayOfMonth and plusMonths are not deprecated so it is not clear why it’s complaining about those.

But realize that this is just a warning. That’s why the Rule still works. “Deprecated” means “no longer supported, likely to go away at some point unannounced.”

In OH, you don’t really have to worry about it going away. The current version of Joda isn’t going to be updated in OH 2.5. In OH 3 Joda is going to be completely replaced with the built in Java DateTime which works slightly differently.

Just a guess… As those methods (or functions?) are part of toDateMidnight
They are both recognized as type DateMidnight instead of DateTime.

1 Like

thx! I just had to replace toDateMidnight with withTimeAtStartOfDay:

OLD:
var Number vStromHistory = (ZaehlerStromTarif0.historicState(now.toDateMidnight.withDayOfMonth(1), "influxdb").state  as DecimalType) //History Wert Mitternacht zum Monatswechsel

NEW:
var Number vStromHistory = (ZaehlerStromTarif0.historicState(now.withTimeAtStartOfDay.withDayOfMonth(1), "influxdb").state  as DecimalType) //History Wert Mitternacht zum Monatswechsel

delivers exactly the same result :partying_face:

The other warnings(withDayOfMonth(), plusMonths()) came, as expected, just because they were part of the function.