4. Creating rules

previous step | start

step4: We create a rules for the new items

We do this in /etc/openhab2/rules by implementing a new rule in a file with the extension *.rules. If you have your openhab2 config folder mounted in windows create a new file and open it in your editor.

the rules folder

First we make a rule for checking the door/window contact HM-Sec-SCo  state. We use the item cEsszimmerTuerRechts created here for this.

rule "Rule_DiningRoomDoorRight"
    when Item cEsszimmerTuerRechts changed
    if (cBadWindow.state == OPEN) {
        //Window is open. Do something
    else {
        //Window is closed. Puh!

In the if statement you can add now your code. Good idea is to switch of the heating as long the window is open.

Now we add a second rule which checks the battery state. In my case I want to get an email if I have to rplace a battery at home. You can do this with the following rule based on the item B_Esszimmer_TuerRechts created here.

rule „B_Esszimmer_TuerRechts_rule“
    Item B_Esszimmer_TuerRechts changed
    if (B_Esszimmer_TuerRechts.state == ON) {
        sendMail("myMail@xxxx.de", "Battery empty!", "Replace the battery in the right door sensor of the dining room")

Please note that you can only send emails with sendMail if have this action configured correctly.  How you can do this for the popular German service gmx.de you can read here.

OK done! Now you can add homematic devices and work with them in openHAB.