Openhab rule: Burglar alarm

Hi guys,

I’ve been trying to set up a set of rules to control my z-wave security devices in an event of unauthorized access to my home. My rule is however not working as I intended. I’ve been setting up OH2 since december and first now I’m starting with more complex rules. It’s taken me a long time to get all the way here. :slight_smile:

I gueess I should outline my stuff first:
I have 2 door sensors, 1 movement sensor and 2 sirens. These I’m trying to get to work in harmony.

  • Dorr_Dorrsensor_Hall_null = Door sensor main door
  • Dorr_Dorrsensor_Vardagsrum_null = door sensor back door
  • Rorelse_Rorelsesensor_Vardagsrum_null = motion sensor living room

Then I also have two virtual items:

  • Number Switch_Virtual_Armering “Alarm arming status”
  • Switch Switch_Virtual_AlarmState “Alarm state”

The Number Switch_Virtual_Armering item has 4 states.
1 = Off, Nothing will ever trigger
2 = Day, only the back door can trigger the alarm
3 = Night, back and front door can trigger the alarm
4 = Full security, all sensors can trigger alarm. Back door, front door and motion sensor in living room.
Shown in my sitemap it looks like this:

Now to the rule. Just an example of what goes wrong. If I try to trigger the back door in “Day” mode. Then the timer activates as it should. But everything in the rule “alarm trigger” gets executed even the part about reseting the timer.
You see I have a tablet inside my door which is password protected. I have a 25 second timer so that I can deactivate the alarm before it triggers. If I do deactivate before the timer reaches 0 it it still activates the parts of the code for alarm activation.

I’ve followed different examples that I have found in the OH documentation to build the Rules.

import org.openhab.model.script.actions.Timer
var Timer timer
var String alarm_trigger1 = null

rule main_door_open
when
	Item Dorr_Dorrsensor_Hall_null received update
then
	if (Switch_Virtual_Armering.state >= 3 && Dorr_Dorrsensor_Hall_null.state == OPEN) {
		alarm_trigger1 = "front door"
		sendCommand(Switch_Virtual_AlarmState, "ON")
	}
end

rule back_door_open
when
	Item Dorr_Dorrsensor_Vardagsrum_null received update
then
	if (Switch_Virtual_Armering.state != 1 && Dorr_Dorrsensor_Vardagsrum_null.state == OPEN) {
		alarm_trigger1 = "back door"
		sendCommand(Switch_Virtual_AlarmState, "ON")
	}
end

rule movement_vardagsrum
when
	Item Rorelse_Rorelsesensor_Vardagsrum_null received update
then
	if (Switch_Virtual_Armering.state == 4 && Rorelse_Rorelsesensor_Vardagsrum_null.state == ON) {
		alarm_trigger1 = "movement living room"
		sendCommand(Switch_Virtual_AlarmState, "ON")
	}
end

rule alarm_trigger
when
	Item Switch_Virtual_AlarmState changed from OFF to ON
then
	if (Switch_Virtual_AlarmState.state == ON) {
		sendNotification("emailaddressofmytablet@gmail.com", "Alarm at home!!")
		timer = createTimer(now.plusSeconds(25)) [|
			sendCommand(Switch_Siren_Hall_null, "ON")
			sendCommand(Switch_Siren_Vardagsrum_null, "ON")
			sendBroadcastNotification("Alarm! Alarm trigger is: " + alarm_trigger1)
			]
        } else {
            if(timer!=null) {
                timer.cancel
                timer = null
                alarm_trigger1 = null
                sendBroadcastNotification("Alarm aborted")
            }
	}
end

rule reset_alarm
when
	Item Switch_Virtual_Armering received update
then
	if (Switch_Virtual_Armering.state == 1 && Switch_Virtual_AlarmState.state == ON) {
		sendCommand(Switch_Siren_Hall_null, "OFF")
		sendCommand(Switch_Siren_Vardagsrum_null, "OFF")
		sendCommand(Switch_Virtual_AlarmState, "OFF")
	}
end

not sure your rule reset_alarm ever triggers:

maybe I am not getting it, you do poll the state of Switch_Virtual_Armering frequently, but where do you define it?

I trigger the item “Switch_Virtual_Armering” in my sitemap. It has 4 states.
1 for alarm off
2 for day time
3 for night time
4 for full security

Is that what you mean?
I also have an items file for it where I have defined it as a number.

You could simplify here, as the rule only triggers for ON you don’t need to test for ON as well.
Then see if the rule logic is what you intended

Ah I see, I didn’t relize that the part after “then” didn’t need to start with an if clause. Most rule examples I have looked at all have this.

And after some more testing I think that the part of my rules which is not functioning as intended is that rule called “alarm_trigger”.
I based it off one of the examples in the OH docuentation here. I tried to adapt the example for my needs, but maybe I am missunderstanding the function of the rule in the documentation.

The description says “How to create a rule, which only executes some code, if a value does not change for a certain period of time”. And that is what I want. :slight_smile:

I’m not sure how to adjust the rule to simplify it as you suggest when it has an else clause.

Cut and pasted just that part of my rules that is the "alarm_trigger currently looks like this:

rule alarm_trigger
when
	Item Switch_Virtual_AlarmState changed from OFF to ON
then
	if (Switch_Virtual_AlarmState.state == ON) {
		sendNotification("emailaddressofmytablet@gmail.com", "Alarm at home!!")
		timer = createTimer(now.plusSeconds(25)) [|
			sendCommand(Switch_Siren_Hall_null, "ON")
			sendCommand(Switch_Siren_Vardagsrum_null, "ON")
			sendBroadcastNotification("Alarm! Alarm trigger is: " + alarm_trigger1)
			]
        } else {
            if(timer!=null) {
                timer.cancel
                timer = null
                alarm_trigger1 = null
                sendBroadcastNotification("Alarm aborted")
            }
	}
end

Is it correct to change it to this?

rule alarm_trigger
when
	Item Switch_Virtual_AlarmState changed from OFF to ON
then
		sendNotification("emailaddressofmytablet@gmail.com", "Alarm at home!!")
		timer = createTimer(now.plusSeconds(25)) [|
			sendCommand(Switch_Siren_Hall_null, "ON")
			sendCommand(Switch_Siren_Vardagsrum_null, "ON")
			sendBroadcastNotification("Alarm! Alarm trigger is: " + alarm_trigger1)
			]
			if(timer!=null) {
				timer.cancel
				timer = null
				alarm_trigger1 = null
				sendBroadcastNotification("Alarm aborted")
			}
end

In your original rule, the ‘else’ clause would only be executed when the ‘if’’ check for ON was not true.
Since the rule was triggered by the same ON, the ‘else’ clause could never execute.

I don’t really understand what you want this rule to do, perhaps text explanation? You want to turn on the sirens 25 seconds after the door opening event? Then what?

1 Like

Hmm I’m trying to understand what your saying about else clause being not able to execute. I will think some more after this post.
I thought I just explain quickly what I’m trying to do in this rule.

If the alarm trigger item “Switch_Virtual_AlarmState” is ON then I want a timer to start. And if the timer goes all the way to the end it will trigger my sirens Switch_Siren_Hall_null & Switch_Siren_Vardagsrum_null.

However, if the state of Switch_Virtual_AlarmState changes to OFF during the timer then the timer should cancel and no alarms should go off. (basically an abort of the alarm, in case I enter the house and have forgotten to disarm).

Hmm maybe I should use Switch_Virtual_AlarmState received update instead of changed from OFF to ON to avoid the problem you state above?

1 Like

I think I got the rule working now. Just tested it from different angles live and it seems to be doing what I want now.

I went with having when the items “changes” and nothing else not from this to that.
I also removed the import at the very top of my rules file. It was saying this: import org.openhab.model.script.actions.Timer since I copied the code directly from the OH2 documentation rule examples.
But I found this thread where the op had problems with the same type of rule as me. It seems it was maybe the same issue in both cases! :slight_smile: yay!! :slight_smile: