Multiple if not working

Hi everyone,

I’m new into OpenHab (and dev). I’m trying to set up an alarm with a rule but its not working fully.
I’m trying to count each wrong code to run the alarm but it stay at one.

rule “Alarm test”
Item Keypad_Alarm received command
var Number CountWrongCode = 0

if (Keypad_Alarm.state == 1234 && State_Alarm.state == ARMED){
else if (Keypad_Alarm.state == 1234 && State_Alarm.state == DISARMED){
else if (Keypad_Alarm.state != 1234 && CountWrongCode != 3) {
    CountWrongCode = CountWrongCode + 1
else {
    CountWrongCode = 0


On HabPannel :
I have a string who display the state (armed/disarmed/Alarm) and another one who is the count of wrong code typed.
The count stay at one and it’s updated only once as i can see in the log.

I believe you are struggling with the scope of your var statement.
If you put in the body of the rule (as you did), the variable will only exist when your rule is running, but not in the time when it is not running. Every time your rule starts it will be set to zero and then either stay (correct code) or be changed to 1 (wrong code). And you rule will start from scratch every time you Item Keypad_alarm received command.
You can try to put the var statement at the top of your rule file (before the line rule "Alarm test"), this would make it a global variable (accessible to all rules within this specific rule file and it will exist until you reload this rule file or restart OH2); or you may want to look into persistence features, but that will make your rule more difficult and likely not necessary here.