Problems by comparing DateTimeType's in a rule

Hi to all.
I have a question:
I have the now time (currentDate) and i have a CalDav_Date Event out of the Caldav Binding.

DateTime CurrentDate  "Datum [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]" <calendar> { channel="ntp:ntp:local:dateTime" }
DateTime    CalDav_Date "Datum der Abholung [%1$td.%1$tm.%1$tY]"    <calendar>    { caldavPersonal="calendar:Muellkalender type:EVENT eventNr:'1'value:START" }

I want to compare both, but with minus 15 hours by the CalDav_Date

Something like that

	if ((CurrentDate.state as DateTimeType).getCalendar().getTime() == (CalDav_Date.state as DateTimeType).getCalendar().getTime()).minusHours(15)) 
do something 
		

Is this the right way?
Thanks for help and greetings,
Markus

I suggest you peruse this article for type conversions.
There are examples of time comparisons in it:

The most up to date version is actually pay of the docs now.

Thanks I knew that, I just couldn’t find it!

Okay. Thanks to all.

This is my solution:

rule "Muellabfuhr Benachrichtigung Termin1"

when
  Item CurrentDate changed
then
 
  	if (now.isAfter(new DateTime((CalDav_Date.state as DateTimeType).getCalendar().getTime()).minusMinutes(870)) &&      // 870 Minuten ist 16:30 Uhr am Tag zuvor zur Startzeit
		now.isBefore(new DateTime((CalDav_DateE.state as DateTimeType).getCalendar().getTime()).minusMinutes(870))) {    // 870 Minuten ist 16:45 Uhr am Tag zuvor zur Endzeit
  		
        if (CalDav_Muelltonne.state == "Leichtverpackungen" && Muellswitch.state == OFF) {
            green.sendCommand(ON)                                                                                        // Spezielles Licht leutet grün 
            Muellswitch.sendCommand(ON)                                                                                  // Verriegelung, damit die Regel nicht stänig innerhalb des Zeitintervalls auslöst
            Wohnzimmer_TTS.sendCommand('Hallo zusammen! Morgen wird der gelbe Sack und der Hausmüll abgeholt. Könnt Ihr diese bitte rausstellen! Dank euch!')
        }
        
        if (CalDav_Muelltonne.state == "Restabfall" && Muellswitch.state == OFF) {
            green.sendCommand(ON)
            Muellswitch.sendCommand(ON)
            Wohnzimmer_TTS.sendCommand('Hallo zusammen! Morgen wird der Hausmüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!')
        }
        
        if (CalDav_Muelltonne.state == "Bioabfall" && Muellswitch.state == OFF) {
            green.sendCommand(ON)
            Muellswitch.sendCommand(ON)
            Wohnzimmer_TTS.sendCommand('Hallo zusammen! Morgen wird der Biomüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!')
        }
        
        if (CalDav_Muelltonne.state == "Papier" && Muellswitch.state == OFF) {
            green.sendCommand(ON)
            Muellswitch.sendCommand(ON)
            Wohnzimmer_TTS.sendCommand('Hallo zusammen! Morgen wird der Papiermüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!')
        }
    }
    else
         if (Muellswitch.state == (ON))
            Muellswitch.sendCommand(OFF)                                                                                  // Nach Zeitintervall Verriegelung wieder aufgehoben                                                                                                        

end

I hope it will run. With test Dates and Times it works.
But one problem is already this:

The method getCalendar() from the type DateTimeType is deprecated

I don’t understand how i have to change it.

And i get two events with the same start and end time. Here Leichtstoffverpackung und Restmüll.
Is it right that the rule only recognise the first event and deletes both after ending?

Greetings,
Markus

Replace it with .getZonedDateTime.toInstant.toEpochMilli

The docs need to updated.

If you apply Design Pattern: How to Structure a Rule you can greatly reduce the lines of code and number of indents which will make the rule easier to expand or update in the future.

rule "Muellabfuhr Benachrichtigung Termin1"
when
    Item CurrentDate changed
then
    var newMuellswitch = OFF
    var newGreen = OFF
    var tts = ""

    if(now.isAfter(new DateTime((CalDav_Date.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli).minusMinutes(870) &&
       now.isBefore(new DateTime((CalDav_DateE.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli).minusMinutes(870) &&
       Muellswitch.state == OFF) {

        newGreen = ON
        newMuellswitch = ON

        switch CalDav_Muelltonne.state {
            case "Leichtverpackungen": tts = 'Hallo zusammen! Morgen wird der gelbe Sack und der Hausmüll abgeholt. Könnt Ihr diese bitte rausstellen! Dank euch!'
            case "Restabfall": tts = 'Hallo zusammen! Morgen wird der Hausmüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!'
            case "Bioabfall": tts = 'Hallo zusammen! Morgen wird der Biomüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!'
            case "Papier": tts = 'Hallo zusammen! Morgen wird der Papiermüll abgeholt. Könnt Ihr diesen bitte rausstellen! Dank euch!'
        }
    }

    if(Muellswitch.state != newMuellswitch) Muellswitch.sendCommand(newMuellswitch)
    if(newGreen == ON) {
        green.sendCommand(newGreen)
        Wohnzimmer_TTS.sendCommand(tts)
    }
end
2 Likes

Hi Rich.
That’s perfect. Very nice rule.
Thank you very much.
I think i need a lot more time until i can write such a rule by myself.
Greetings,
Markus