How do I set a flag and test it

I need to set a flag to the value of 1 in one rule, and in different rule test the flag for a value of 1 && (and) something else. This format does not work. Help please.
I chose a numerical flag, but would also like to have an example of a logical text flag such as true/false

rule "set flag"
when // trigger condition
then var flag=1
end

rule "take action if flag=1"
when  //different trigger condition
then logInfo("RULES", "the value of flag is", flag)  //show value of flag in log
if (flag=1 && // something else)  { //do this }
end

Two things I see wrong.

= is assignment operator and should not be in your if statement. Use == which compares and determine if true or false.

Second thing you declare the var inside a rule so it can only be used inside that one rule. Declare it at the top outside of all rules to make it what is called a global variable that all rules can then use.

try…

var flag=0

rule "set flag"
when // trigger condition
then flag=1
end

rule "take action if flag=1"
when  //different trigger condition
then logInfo("RULES", "the value of flag is", flag)  //show value of flag in log
if (flag==1 && // something else)  { //do this }
end

That is a variable that all rules within that one .rules file can use. You cannot use a var or val across .rules files.

2 Likes