[SOLVED] Conditional statement with a Lambda function variable

There are far better approaches than using global lambdas in the Rules DSL. Global lambdas have a lot of problems which really make them only usable as a last resort. They should never be the first thing you use to help make code better. See Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL for techniques which do work (using lambdas is among them).

We need more context to understand this code. Is it in a Rule? Is the lambda definition within a Rule too? Where do you define item? Sometimes attempts to “simplify” a Rule before posting destroys the very detail we need to help.

There is no such type as state. Do you mean State? Rules DSL is case sensitive. And usually it is more appropriate to be more specific, such as OnOffType or NumberType rather than the very generic State. You are unlikely to be writing lambdas for that diverse of Item types.

You haven’t passed item into the lambda.

As Markus pointed out, you are trying to assign OFF to s when what you mean to do is a comparison which requires ==".

That’s because it doesn’t know what to do with s. You’ve tried to make it of type state which doesn’t exist.

No, neither the variable s nor the OFF are Strings, they are OnOffTypes, which is the type of State carried by Switch Items.

That is actually an old syntax requirement. What OP did is perfectly acceptable since OH 2.3 (and before OH 2.0). It’s all the other stuff you identified that was causing problems. See Reusable Functions: A simple lambda example with copious notes for details. The tl;dr is you don’t have to specify the Functions$Function1 with a return type or Procedures$Procedure (no return). The Rules DSL is smart enough to figure out the number and type of the arguments and if the last line returns void it will use Procedures$X and if it returns something it will use Functions$X and the appropriate type for the return value.

That would work from a lambda declaration perspective but there are no state1 and state2 types.

Since you don’t care about the return value, eliminate the whole true stuff. Leave it out and the lambda won’t return anything which is perfectly OK.

I agree. Even as an expert, global lambdas should only be used as a last resort. They are not thread safe so their use will cause more problems than they solve.

This part is not required. It was for awhile but then I think the upstream Xtend/Xtext libraries fixed something and now we can do it the “nicer” way.

val test_lambda = [ String s1, String s2 |
    // LOGIC GOES HERE
]

would also work.