[SOLVED] Help script number counter

Hi guys, maybe u cant help me write corect script?
I need count the item “alarm” meanings.
If 2 time i have 3 to alarm do something. (3+3)

Number alarm "Fire alarm" {mqtt="<[broker:/Alarm-esp-katilinei/bell_input_status/bell_input_status:state:default]"}

P.s Sorry for my english skills :blush:

Mhmm, maybe work this?

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

One have to precede the rule with “counter” definition in order to make it referable in subsequent rule execution(s).

e.g.

var int counter = 0
rule "counter"
when
....

Thanks friend, i try, but my rule dosn’t work :smiley: . This was more of a guess

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

I think the OP meant something like this:

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

@PtrO IMHO the casting you used is an “Overkill”.

I try this, and I think it’s work, but don’t know or right

var int counter = 0
rule "counter"
when
Item alarm received update
then
If (alarm.state == 3 && counter <= 2) {
    counter = counter +1
}
else if (counter == 2) {
test.sendCommand(ON)
createTimer(now.plusSeconds(15))
[|
test.sendCommand(OFF)
counter = 0
]
}
end

now I search bugs. Maybe have observations?
P.s this script for fire alarm notification :smiley:

:ok_hand: @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)

1 Like

@PtrO you mean this?

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

1 Like

There are typos in this rule. If is not the same as if

The else is wrong, I’m pretty sure of that. :wink:

// 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
2 Likes

@Udo_Hartmann Configuration model ‘.rules’ has errors, therefore ignoring it: [13,22]: missing ‘)’ at ‘{’

sorry, one bracket left. It’S this line:

    if((counter = 2) {                                           // second alarm?
//     ^ one bracket too much

I changed the code above.

@Udo_Hartmann

Rule 'counter': Unknown variable or command '&amp;&amp;'; line 13, column 51, length 17

Jep, another typo.

    if((alarm.state as Number).intValue = 3 && counter <= 2)     // alarm, so 
//                                      ^ one = missing, should be ==

and another…

   if(counter = 2) {                                            // second alarm?
//            ^ should also be ==

@Udo_Hartmann the Rule looks amazing, but dosen’t work.:disappointed_relieved:

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.

1 Like

:+1: 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.

 log:set DEBUG org.eclipse.smarthome.model.rule.runtime
1 Like