Howto find out if a var is an even number

I try increase a varable every incomming update of an ITEM
That is no problem in rules:

for example:

var Number counter = 1
rule “counter is an even Numer”" then do something"
when
Item YYYY changed from OPEN to CLOSED
then
counter = counter + 1

Now I would like to send a command to an item every two events or in other words only when the variable is an even number (or uneven number - but is the same …)

What is the correct syntax for the arithmetic if advise:

if ( counter == even)

didn´t work - but can explain what i’m looking for.

Thanks for helping me.
I´m using at the moment OH2.5.8-1 on a tinkerboard with “buster”

Cheers
Stef

if (counter % 2 == 0)
2 Likes

Did you try the modulo ( % ) operator ? It can be used to detect if a number is odd or even.

if( counter % 2 == 0) {
       logInfo("Test", "EVEN")
} else {
       logInfo("Test","ODD")
}
1 Like

you want a mod operator, normally used as 5 mod 2 = 1, 6 mod 2 = 0 it gives you the remainder after a division operation not the answer. In C# it’s a percent symbol maybe the same in java - yes looks like it might be.

http://www.cafeaulait.org/course/week2/15.html#:~:text=Java%20has%20one%20important%20arithmetical,leaves%20a%20remainder%20of%201.

So this might work.

If it’s only to run on even or uneven you can also use a boolean. Instead of + 1 you inverse the value and when testing if the rule should run just test the boolean if (value) {

2 Likes

That’s a really good approach. The code would look like

var run = True

rule ...

    run = !run
    if(run){ ...

The not operator ! will flip it between True and False every time it runs.

1 Like

moin min,

“!run” does not solve the problem!
I need ’ “every other” and not “every” ’

means:
1 run “true” 2nd run “true”
third run “true” 4th run “true”

Thanks
Cheers
Stef

Good approach but:

Error:
Rule ‘Badezimmer Tuer auf Radio an’: Unknown variable or command ‘%’; line 21,

so Modulo ‘%’; does not work

On which syntax is the rule system based on?

Thanks
Cheers
Stef

Hi all,
Correction of ‘%’ and solution of the Top

The operator ’ % ’ (Modulo) work!
Syntax of DSL rules can be found in
https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html

The problem was the variable declaration!
“var Number” does not work with modulo operator!
You have to use int instead of Number in declaration if you want to use the modulo_operator with the Integer 2.

Thanks all for helping and pointing me to the solution

Summary: Rules solution is:

var int counter = 1
rule EVEN // “counter is EVEN ” then do something"
when
Item YYYY changed from OPEN to CLOSED
then
counter = counter + 1
if (counter % 2 == 0) {      
       logInfo("Test", "EVEN")
       sendcommand ..... //*do something*
} else {
       logInfo("Test","ODD")
       // *do nothing*
}

Cheers
Stef

1 Like

Rich’s example used Jython syntax for the boolean. Use true in lower case, not True

var run = true

The better option is to not cast unless you absolutely have to!

The better option is to not cast unless you absolutely have to!

absolutely true: “better is not to cast”: where is “var run = true” in the quotation or the solution??? anywhere!
Stef

var run = true// global variables need to go above all rules in the file

rule "EVEN"
when
    Item YYYY changed from OPEN to CLOSED
then
    run = !run
    if (run) {
       logInfo("Test", "true")
       sendcommand ..... //*do something*
    } else {
       logInfo("Test", "false")
       // *do nothing*
    }
end

Okay couldn’t resist sharing this :smirk:
Just make sure you don’t follow any of these :wink:

(some of the replies on twitter are hilarious though)

2 Likes

'I have no twitter

twitter is good for Donald T… the first king of “great again”… therefore I’m so glad that I’m to stupid to use (and have) it :slight_smile:

1 Like

@sjheinz hope you didn’t find this offensive (apologies if you did!), this is a legitimate question, just happened that this tweet was popular just a few days ago, and it was just sarcasm :slight_smile:

This might be happened so.

But as i said: not everybody have twitter!

That´s why i use the communitiy web and not twitter!

I thing in generell The comunnity should focus in one support szenario and not in every! Same as the system, they work on every stage and nowhere in a good stable shape! that is not sarcasm - unfortunately from simple user point of view