Reduce the size of a rule

Hey,

I have a security keypad in my sitemap which is mapped to 12 buttons.
I am looking at what buttons are pressed and adding them to a code string. when i press the tick button, if that code string matches a predetermined code, it unlocks everything.

This is a very simple form of security ive been playing around with for fun.

But how can i make this rule shorter. I know i can look at what button was pressed, and then send the last letter/number as string in that button name. But how would i word it.

sitemap

Switch item=AlarmKeypad label="Passcode" 	icon="keypad"	mappings=[KEY1="1", KEY2="2", KEY3="3"]			
		Switch item=AlarmKeypad label=""	icon="none"					mappings=[KEY4="4", KEY5="5", KEY6="6"]		
		Switch item=AlarmKeypad label=""	icon="none"					mappings=[KEY7="7", KEY8="8", KEY9="9"]		
		Switch item=AlarmKeypad label=""	icon="none"					mappings=[KEYN="X", KEY0="0", KEYY="✓"]		
		Text item=KeypadString 	label="Code is [%s]" icon="none"  valuecolor=[KeypadString=="Correct"="green",KeypadString=="Incorrect"="red"]

Items

String AlarmKeypad
Switch Unlocked
String KeypadString

Rule

rule "keypad"
when
	Item AlarmKeypad received update
then
	if (AlarmKeypad.state == "KEYN")
		{
		if (lockTimer !== null)
		{
		lockTimer.cancel
		lockTimer = null
		}
		KeypadString.postUpdate("")
		Unlocked.sendCommand(OFF)
		
		}
		
	if (AlarmKeypad.state == "KEY0")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "0")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY1")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "1")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY2")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "2")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY3")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "3")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY4")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "4")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY5")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "5")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY6")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "6")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY7")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "7")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY8")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "8")
	AlarmKeypad.postUpdate(NULL)
	}
	
	if (AlarmKeypad.state == "KEY9")
	{
	KeypadString.postUpdate(KeypadString.state.toString + "9")
	AlarmKeypad.postUpdate(NULL)
	}
	
	
end

rule "keypadstate"
when
	Item AlarmKeypad received command KEYY
then
	if (KeypadString.state == "XXXX")
	{
	KeypadString.postUpdate("Correct")
	Thread::sleep(750)
	Unlocked.sendCommand(ON)
	AlarmKeypad.postUpdate(NULL)
	
	if (lockTimer !== null)
		{
		lockTimer.cancel
		lockTimer = null
		}
		 
		
	lockTimer = createTimer(now.plusMinutes(3))[|
	Unlocked.sendCommand(OFF)
	]
	}
	else
	{
	KeypadString.postUpdate("Incorrect")
	Unlocked.sendCommand(OFF)
	AlarmKeypad.postUpdate(NULL)
	}
createTimer(now.plusSeconds(1)) [|
		KeypadString.postUpdate("")
		]

	
end
1 Like

Also,
has anyone noticed a noticeable lag when using the openhab Android app connecting via the cloud, vs using chrome on android phone connecting to public IP with port 8080 forwarded through router?

My kepad items above do not update properly on the app, whereas on chrome for android, its almost instant.
anyway to make this better?

I’m on my phone so can’t do a thorough job but the first thing that pops it is you should apply Design Pattern: How to Structure a Rule.

You might be able to apply something from Design Pattern: Encoding and Accessing Values in Rules but I’m not as sure.