Rule to check windows that have been openend recently when leaving the house

I am making a rule that reminds me to check possibly open windows. I have contact sensors that only send ON when windows open and not OFF when they close. A rule switches them to OFF after a specified amount of time instead. So, I thought I’d filter out all contact sensors that have been ON since sunrise.

This is what I came up with:

import org.joda.time.*
import java.lang.Math
import org.openhab.core.persistence.*
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.Timer
import java.util.StringBuilder

var Timer tOpenWindows = null

   rule "warn for possibly open windows"
when
	Item Living_OuterHallwayDoor changed to ON or
    Item DebugSwitch changed to ON
then
	//tOpenWindows = null
    val StringBuilder OpenWindows = new StringBuilder
	val DateTime WindowsStart = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)  
  if ( (new LocalTime().getLocalMillis()) <= (new LocalTime(8, 0, 0).getLocalMillis()) || (new LocalTime().getLocalMillis()) >= (new LocalTime(9, 30, 0).getLocalMillis()) ) {

	if (tOpenWindows == null) {
		logInfo("OpenWindows", "checking for windows opened after" + WindowsStart + " (sunrise = " + Sunrise_Time.state + ")")                   
        gWindows.members.forEach(Switch|
        	if (Switch != null) {
        	  var String LastUpdate = String::format( "%1$td %1$tb %1$tl:%1$tM %1$tp", Switch.lastUpdate("rrd4j") )
        	  logInfo("OpenWindows", "checking " + Switch.name as String + ", last opened " + LastUpdate)
              if (Switch.updatedSince(WindowsStart,"rrd4j")) {
              		if (OpenWindows.toString().equals("")) {
						OpenWindows.append(" the " + Switch.name as String) 
			        } else {
						OpenWindows.append(" and the " + Switch.name as String)        		
        	  		}
                    logInfo("OpenWindows", "added " + Switch.name as String)
                }
			}
        )
        if (!OpenWindows.toString().equals("")) {
                executeCommandLine("/home/pi/tts.sh attention. Did you close" + OpenWindows + "?")
                logInfo("OpenWindows", "Warned for"+ OpenWindows)
        } else {
        		logInfo("OpenWindows", "No opened windows found")
        }      
        tOpenWindows = createTimer(now.plusMinutes(60)) [|
        	logInfo("OpenWindows","Timer tOpenWindows ended") 
        ]
        logInfo("OpenWindows","Timer tOpenWindows initiated") 
    } else {	 
    	logInfo("OpenWindows","Timer tOpenWindows running, do nothing")   
    }
    
  }
end

This is the log:

2016-03-20 14:33:21.708 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'security.rules'
2016-03-20 14:33:33.611 [INFO ] [enhab.model.script.OpenWindows] - checking for windows opened after2016-03-20T06:40:00.000+01:00 (sunrise = 2016-03-20T06:40:00)
2016-03-20 14:33:34.607 [INFO ] [enhab.model.script.OpenWindows] - checking Attic_Window, last opened 19 Mar 8:40 am
2016-03-20 14:33:34.937 [INFO ] [enhab.model.script.OpenWindows] - checking Kitchen_Window, last opened 20 Mar 2:31 pm
2016-03-20 14:33:35.216 [INFO ] [enhab.model.script.OpenWindows] - No opened windows found
2016-03-20 14:33:35.707 [INFO ] [enhab.model.script.OpenWindows] - Timer tOpenWindows initiated

The code seems to pick up the sunrise time (WindowsStart) and the timestamps the windows were last openend (Switch.lastUpdate(“rrd4j”)), but somehow the calculation whether the one is after the other doesn’t work (if (Switch.updatedSince(WindowsStart,“rrd4j”))). Any ideas?

It works now. I don’t know what was the problem. For reference the final version:

import org.joda.time.*
import java.lang.Math
import org.openhab.core.persistence.*
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.Timer
import java.util.StringBuilder

var Timer tOpenWindows = null

rule "warn for possibly open windows"
when
    Item Living_OuterHallwayDoor changed to ON //or    Item DebugSwitch changed to ON
then
    //tOpenWindows = null
    val StringBuilder OpenWindows = new StringBuilder
    val DateTime WindowsStart = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)  
  if ( (new LocalTime().getLocalMillis()) <= (new LocalTime(8, 0, 0).getLocalMillis()) || (new LocalTime().getLocalMillis()) >= (new LocalTime(9, 30, 0).getLocalMillis()) ) {

    if (tOpenWindows == null) {
        logInfo("OpenWindows", "checking for windows opened after" + WindowsStart + " (sunrise = " + Sunrise_Time.state + ")")                   
        gWindows.members.forEach(Switch|
            if (Switch != null) {
              var String LastUpdate = String::format( "%1$td %1$tb %1$tl:%1$tM %1$tp", Switch.lastUpdate("rrd4j") )
              logInfo("OpenWindows", "checking " + Switch.name as String + ", last opened " + LastUpdate)
              if (Switch.updatedSince(WindowsStart,"rrd4j")) {
                    if (OpenWindows.toString().equals("")) {
                        OpenWindows.append(" the " + Switch.name as String) 
                    } else {
                        OpenWindows.append(" and the " + Switch.name as String)        		
                    }
                    logInfo("OpenWindows", "added " + Switch.name as String)
                }
            }
        )
        if (!OpenWindows.toString().equals("")) {
                executeCommandLine("/home/pi/tts.sh attention. Did you close" + OpenWindows + "?")
                logInfo("OpenWindows", "Warned for"+ OpenWindows)
        } else {
                logInfo("OpenWindows", "No opened windows found")
        }      
        tOpenWindows = createTimer(now.plusMinutes(60)) [|
            logInfo("OpenWindows","Timer tOpenWindows ended") 
        ]
        logInfo("OpenWindows","Timer tOpenWindows initiated") 
    } else {	 
        logInfo("OpenWindows","Timer tOpenWindows running, do nothing")   
    }

  }
    end

The log file says:

2016-04-09 20:19:43.726 [INFO ] [enhab.model.script.OpenWindows] - checking for windows opened after2016-04-09T06:56:00.000+02:00 (sunrise = 2016-04-09T06:56:00)
2016-04-09 20:19:45.061 [INFO ] [enhab.model.script.OpenWindows] - checking Attic_Window, last opened 09 Apr 7:59 am
2016-04-09 20:19:46.305 [INFO ] [enhab.model.script.OpenWindows] - added Attic_Window
2016-04-09 20:19:46.432 [INFO ] [enhab.model.script.OpenWindows] - checking Kitchen_Window, last opened 20 Mar 2:31 pm
2016-04-09 20:19:47.466 [INFO ] [enhab.model.script.OpenWindows] - Warned for the Attic_Window
2016-04-09 20:19:48.370 [INFO ] [enhab.model.script.OpenWindows] - Timer tOpenWindows initiated