Door window rule

Hi.
I am not so good with programming but I am learning. I have a fibaro window contact and max cube thermostat. Can someone give me an example how to create a rule when window is open for more than 5 minutes to close the thermostat and if window is closed before the 5 minutes to cancel the rule.also if the window is closed after the 5 minutes to send again a command on the thermostat.

Thanks in advanced
Nikos

Your rule will need to do a few things.
Since you are using a timer, your rule header has to reference some libraries. These are my “standard stack” which will support your need, possibly not all needed though.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.* 
import org.joda.time.*
import org.joda.time.DateTime
import org.joda.time.DateTime.plusSeconds
import org.openhab.model.script.actions.Timer
var Timer WIN_OPEN_TIMER = null

You’ll start a timer when the window opens. The timer will be for 5 minutes. At the end of the five minutes the timer will trigger the closing of the thermostat. Let’s call the timer WIN_OPEN_TIMER (in rule header example above) and the thermostat control sw_THERMOSTAT (it will also be in your items file).

First, trigger when the window is opened. Let’s say your window contact is named co_WINDOW in your items file. So your rule starts with:

RULE "window open rule"
WHEN
    co_WINDOW changed to OPEN

THEN
    WIN_OPEN_TIMER=createTimer(now.plusSeconds(300)) [ |
                   sendCommand(sw_THERMOSTAT, OFF)  ]
END

That will set up the window open too long, shut off thermostat logic.

Now let’s deal with the window closing under either condition (before or after the 5 minutes expires)

RULE: "Window closing"
WHEN
    co_WINDOW changed to CLOSED
THEN
    if WIN_OPEN_TIMER.hasTerminated() {
         //window open for longer than five minutes so thermostat is OFF
         WIN_OPEN_TIMER=null
         sendCommand(sw_THERMOSTAT,ON)
    }
    if WIN_OPEN_TIMER !=null {
        //window open less than five minutes, just cancel and clean up the timer
        WIN_OPEN_TIMER.cancel()
        WIN_OPEN_TIMER=null
    }
END

Hope this helps.

Hi bob_dickenson,

Thanks for your example it helps me a lot. here is the code and its working very good.
I am trying to make it more clever. For example the max thermostats have automatic and manual modes, on automatic mode the thermostats working on the weekly programm that the user have build form the maxportal. on the manual mode the thermostat stay all the time on the same temperature that the user set it.
I see the command previousState can help to restore the thermost on the state that was before .
For example if my thermostat is set up on the manual mode 19 celcius and i open the window then the code will send the command manual and 4.5 celcius so hermost is off. Now if i close the window the code will sent the command automatic that will bring the thermostat on the weekly programm for example 22 celcius. The optimall will be to restore the thermostat on the last sate (manual 19 celcius). but i dont know how to do it with the persistence and previousState.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.joda.time.*
import org.joda.time.DateTime
import org.joda.time.DateTime.plusSeconds
import org.openhab.model.script.actions.Timer

var Timer windopen = null
var Timer delay = null

rule “Window/door open close thermostat”

when
Item Living_Room_Door changed
then
if(Living_Room_Door.state==OPEN) {
if(windopen==null) {
windopen = createTimer(now.plusSeconds(240)) [|
sendCommand(Heating_Mode_Living, “MANUAL”)
]
delay = createTimer(now.plusSeconds(300)) [|
sendCommand(Heating_Living, 4.5)
]
}
}
end

rule “Window/door close restore thermostat”

when
Item Living_Room_Door changed
then
if(Living_Room_Door.state==CLOSED) {
if windopen.hasTerminated() {
//window open for longer than five minutes so thermostat is OFF
windopen=null
sendCommand(Heating_Mode_Living, “AUTOMATIC”)
}
}
if(Living_Room_Door.state==CLOSED) {
if windopen !=null {
//window open less than five minutes, just cancel and clean up the timer
windopen.cancel()
windopen=null
delay.cancel()
delay=null
}
}
end

Sorry for the big post and for my bad english

To restore previous state you need a persistence setup (use rrd4j ) – so that jar needs to be in your addons directory. You also need a persistence file in your configurations/persistence subdirectory called rrd4j.persist (AFAIR…not near my system). That file contains the strategy for persisting data. Wiki doc on this is pretty accurate so go there. Essentially you’ll probably want to persist every temperature setting (as a value from Heating_Living_Setting in your items file) , then in your sendCommand do something like :

sendCommand(Heating_Living, Heating_Living_Setting.previousState() )

BTW, if you are posting code blocks, bracket them with “pre” and “/pre” (both with unshown left-and-right angle brackets). Your code will be more readable.