rule "counter"
when
Item alarm received update
then
var counter = 0
if (alarm.state == 3) {
counter = counter + 1
}
else if (counter = 2) {
//do something
}
end
Note “Variables” that need be referenced between subsequent (same) rule executions, must be declared in the rule-file outside the rule-definition code itself.
I didn’t look at the rest of you code but when one need to compare a “state”, you must convert (cast) its internal string-like-type to numeric.
Updated/enhanced variation:
var int counter = 0
rule "counter"
when
Item alarm received update
then
if ((alarm.state as DecimalType).intValue = 3) {
counter = counter + 1
}
else if ((alarm.state as DecimalType).intValue = 2) {
//do something
}
end
var int counter = 0
rule "counter"
when
Item alarm received update
then
if (alarm.state == 3) {
counter = counter + 1
}
if (counter == 2) {
//do something
counter=0 //Reset the counter
}
end
@opus Yep, you’re (may be?) right… a numeric item state is internally already defined “org.eclipse.smarthome.core.library.types.DecimalType”
I had quite some problems using the “smarthome” reference types and prefer (or better used to) explicitly cast them to native “java”.
In this case the your solution (which covers receiving Messages while “test” is ON) is better.
Thanks for spotting my failure in the If cases (was only using “=” instead of “==”, corrected my post)
var int counter = 0
rule "counter"
when
Item alarm received update
then
If ((alarm.state as DecimalType).intValue = 3 && counter <= 2) {
counter = counter +1
}
else if ((alarm.state as DecimalType).intValue = 2) {
test.sendCommand(ON)
createTimer(now.plusSeconds(15))
[|
test.sendCommand(OFF)
counter = 0
]
}
end
The logic might have a problem due to the use of “else if”.
Can’t test ATM, but will the “else if” part be run if the “if” part was triggered? IMHO the “else if” will only be run if the previous “if” was not triggered!
adding - also my missed - else brackets may help… and as @Opus said (my) casting is not required… so this leaves us with:
var int counter = 0
rule "counter"
when
Item alarm received update
then
if (alarm.state == 3 && counter <= 2) {
counter = counter +1
}
else { if (alarm.state == 2) {
test.sendCommand(ON)
createTimer(now.plusSeconds(15))
[|
test.sendCommand(OFF)
counter = 0
] }
}
end
Update single equal = must be exact == , and have a close look at what you expect of the if-then-else-if logic…
FYI: a single “=” is comparing the pointer (which will be never equal) and a double “==” is really the wanted “equal to” value comparison.
Offtopic: always nice to look into someone’s details and rethinking things.
Update: 21u10, the reason I love to use an IDE…that prevent silly If!=if and other lazy bracketed typo’s
There are typos in this rule. If is not the same as if
The else is wrong, I’m pretty sure of that.
// define global vars at the top of the rules file!
var int counter = 0
rule "counter"
when
Item alarm received update
then
if(!(alarm.state instanceof Number)) { // Type correct?
logWarn("alarm","alarm not of type Number!",alarm.state) // No
return; // stop rule
}
if((alarm.state as Number).intValue == 3 && counter <= 2) // alarm, so
counter = counter + 1 // count up
if(counter == 2) { // second alarm?
test.sendCommand(ON) // switch test ON
createTimer(now.plusSeconds(15), [ | // create a timer for 15 Seconds with code
test.sendCommand(OFF) // switch test OFF
counter = 0 // and reset counter
])
}
end
If the rule doesn’t show errors in the log, use some logInfo or logWarn lines. Using such, you can get feedback if the rule has run that line and what the state of an item or variable is at that line.
If the only thing that doesn’t work is the sendCommand, most probably the item “test” does not exist, is spelled differently or is of the wrong type.
Indeed try to step-down the rule.
It might be silly, but while developing rules, sometimes creating a ’ logInfo(“id.id2”, “blabla count=” + count ) ’ after every line, to see if and where it breaks down (to nothing).
Sometimes if undeclared variables are used, the wrong type of do not exist, the rule execution may disappear into the dark.
Also check if rules are triggered setting the appropiate KARAF console logging e.g.