I am not sure if a doorbell has state, but here is my state sketch:

So in my rule I somehow need to keep track of time and changes, I added questions as comments in the code. Also by using state i need two rules and two items instead of just 1 rule and 1 item. Maybe I am missing something here?
val Number MODE_NO_ACTIVITY = 0
val Number MODE_NOICE = 1
val Number MODE_CORRECT_CODE = 2
val Number changes = 0
val time timeFirstChange //how to define a time variable?
rule "Doorbell ringed"
when
Item DoorBell changed from OPEN to CLOSED
then
if (changes =0) {
changes = changes +1
timeFirstChange = now() // whats the correct syntax?
DoorBell_State.postUpdate(MODE_NOICE)
else if (changes = 3) and (now- timeFirstChange)>5 and (now- timeFirstChange)<6 { //does the time use seconds or ms?
DoorBell_State.postUpdate(MODE_CORRECT_CODE)
changes = changes +1
}
else if (changes=6) {
changes = 6
DoorBell_State.postUpdate(MODE_NO_ACTIVITY)
}
else{
changes = changes +1
DoorBell_State.postUpdate(MODE_NOICE)
}
end
rule "Doorbell state changed"
when
Item DoorBell_State changed to MODE_CORRECT_CODE //is this correct syntax?
then
DoorOpener.sendCommand(ON)
end