I’m configuring an alarm. I need to count the times a wrong code is entered into the panel and save that. Everytime a wrong code is entered it needs to +1. I want to show the times a wrong code is entered into Str_Secur_CodeCount
rule "Deactivate Alarm"
when
Item Str_Secur_Panel1 changed
then
val Num_Secur_CodeCount = 0
if( Str_Secur_Panel1.state == "777" ) {
Str_Secur_SoundAlarm.sendCommand("No sound")
Str_Secur_AlarmState.sendCommand("OFF")
say("Alarm deactivated")
}
else {
Num_Secur_CodeCount = Num_Secur_CodeCount +1
}
str_Secur_CodeCount.sendCommand(Num_Secur_CodeCount.state.toString)
end
Looking back at first post I noticed Str_Secur_CodeCount with a cap S and in rule it is lower case s. That may be you issue if not you can try the other examples and post what you see in the logs.
Another example but notice the item Str_Secur_CodeCount on last line. I do not know how the item is defined so you may need to add “.state.toString” on num_Secur_CodeCount in last line. See bottom for 2 examples to try.
var num_Secur_CodeCount = 0
rule "Deactivate Alarm"
when
Item Str_Secur_Panel1 changed
then
if( Str_Secur_Panel1.state == "777" ) {
Str_Secur_SoundAlarm.sendCommand("No sound")
Str_Secur_AlarmState.sendCommand("OFF")
say("Alarm deactivated")
}
else (Str_Secur_Panel1.state != NULL) {
num_Secur_CodeCount += 1
Str_Secur_CodeCount.sendCommand(num_Secur_CodeCount)
}
end
If the above does not work use:
Str_Secur_CodeCount.sendCommand(num_Secur_CodeCount.state.toString)
This one should work fine b/c ALL items accept a string as commands.
If no luck with string try:
Str_Secur_CodeCount.sendCommand(num_Secur_CodeCount.state as Number)
did the trick for displaying the Str_Secur_CodeCount
Now i just cant +1 to the Num_Secur_CodeCount. I see what you mean by: “But of course you would like to count up. But as the var is defined in the rule, it’s always 0 before counting up, so the result is either 0 or 1”. But is there any way i can count up a variable in this loop?
Yes, sure. Simply define the var at the very beginning of the file.
But maybe consider another way. Instead of using a string Item for the counter, use a Number Item (or is there a reason to use a string instead of a number?)
Then simply count up the Item itself.
Maybe you want to reset the counter if the code is correct.