Rules sometimes don't execute correctly

  • Platform information:
    • Hardware: Raspberry Pi 3
    • OS: OpenHabian
    • openHAB version: 2.2.0-1 (release build)
  • Issue of the topic:

I am integrating wall-mounted Fire tablets into my OpenHAB system to control my 4-zone security system in my home. I have attached a screenshot below but basically, the screen consists of a keypad along with several labels to show the status of each zone and the status of the system (armed, disarmed, tripped, etc.)

I have attached my .rules below for reference but I have noticed that sometimes the disarmed sound is played twice when I enter the PIN. In addition, sometimes when I enter the correct PIN it’s not registered (system stays armed and nothing happens) or registers as wrong (incorrect noise played and system not disarmed). I was wondering if anyone had any suggestions as to how I could optimize/change my rules to lessen errors like these. Although the system is usable, I don’t feel comfortable with my untechnical family using it with the occasional bugs that there are right now.

  • Rules configuration:
    For reference:
    0=Armed
    1=Chime
    2=Triggered
    3=Disarmed
rule "Motion detected"
when
	Item alarmZone1 changed to 1 or Item alarmZone2 changed to 1 or Item alarmZone3 changed to 1 or Item alarmZone4 changed to 1
then
	if(alarmStatus.state.toString == "0") {		//If motion is detected and system is armed
		playSound("alarm triggered.mp3")
		alarmControllerCommand.sendCommand("Triggered")
        alarmStatus.sendCommand("2")
	}
	if(alarmStatus.state.toString == "1") {		//If motion is detected and system is on chime
	}
end
rule "Keypad input"
when
	Item alarmKeypadInput received command
then
	if(alarmKeypadInput.state.toString == "****") {		//If correct PIN is entered
		if(alarmStatus.state.toString != "3") {		//And system is not disarmed
			alarmControllerCommand.sendCommand("Disarmed")
			alarmStatus.sendCommand("3")
			playSound("welcome.mp3")
		}
	}
	if(alarmKeypadInput.state.toString != "****") {		//If incorrect PIN is entered
		playSound("incorrect.mp3")
	}
end
rule "Alarm given command"
when
	Item alarmStatus received update
then
	if(alarmStatus.state.toString == "0") {		//System will be armed
		playSound("armed.mp3")
	}
	if(alarmStatus.state.toString == "1") {		//System will be on chime
		playSound("chime.mp3")
	}
	if(alarmStatus.state.toString == "2") {		//System will be triggered
		playSound("alarm triggered.mp3")
	}
end

First I would recommend just using the Strings in your code rather than Numbers. You are treating them as Strings anyway and then your code is much more self-documenting.

Then you don’t need a table or a comments because it is apparent what

if(alarmStatus.state.toString == "Armed") {

means.

In the “Alarm given command” use a switch statement or else if instead of if statements.

    switch(alarmStatus.state.toString) {
        case "0": playSound("armed.mp3")
        case "1": playSound("chime.mp3")
        case "2": playSound("alarm triggered.mp3")
    }

This isn’t just to make the code shorter, this prevents the state of alarmStatus changing while the Rule is executing and causing the other conditions to execute. For example, if while playSound(“armed.mp3”) is playing alarmStatus changes to “1” then chime.mp3 will play twice, once in the rule that triggered when alarmStatus changed to “0” because the if statement will execute, then again when the rule triggers again for the change to “1”. By using a switch or else if statements you make sure that only one sound can be played for one trigger of that rule.

I don’t have any other significant suggestions without more information. You need to add lots of logging to your rules so you can find out some sort of pattern. Perhaps the HABPanel widget isn’t working consistently. Perhaps multiple instances of your rules are running at the same time. Perhaps your rules are not triggering as expected.

1 Like

Great suggestions. Switching out numbers for the actual String was a good idea, I just wasn’t sure if I could compare strings with ==. I think that the sound playing twice could have been a result of having if statements that were triggering twice. I changed those to switch statements like you suggested.

A flaw that I have come across while using the system. Although it’s not a huge deal to my Dad, I’ve noticed that if the system is triggered, then I could simply change it to either “armed” or “chime” where the external alarm controller (an Arduino) would receive the command and basically disarm the system.

So a scenario: I arm my alarm tomorrow morning when my sister and I leave for school and my parents leave for work like usual. A burglar enters my home and sets off the system. The Arduino would receive a command to then turn on the sirens in our home and my family and I would get an OpenHAB alert via push notification. The burglar then proceeds to the alarm panel and set the system to chime. The Arduino then turns off the sirens. Now when motion is detected, a friendly little chime is played through HabPanel.

Does anyone have any suggestions as to how I could require a password to disarm the system first before it’s switched to “chime” or “armed” again?