# \[SOLVED\] Conditional statement with a Lambda function variable

**URL:** <https://community.openhab.org/t/solved-conditional-statement-with-a-lambda-function-variable/79753>\
**Category:** Scripts & Rules\
**Tags:** rules\
**Created:** [August 11, 2019, 2:52pm UTC](https://community.openhab.org/t/solved-conditional-statement-with-a-lambda-function-variable/79753 "2019-08-11T14:52:24Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 11, 2019, 10:08pm UTC](https://community.openhab.org/t/solved-conditional-statement-with-a-lambda-function-variable/79753/7 "2019-08-11T22:08:33Z")

</div>

> [@funky28](#):
>
> I am trying to create/use Lambdas inside of my rules to avoid repeating code over and over

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](https://community.openhab.org/t/design-pattern-dry-how-not-to-repeat-yourself-in-rules-dsl/60105) for techniques which do work (using lambdas is among them).

> [@funky28](#):
>
> I am posting a simple example code that resembles what I want to do:

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.

> [@funky28](#):
>
> I get some errors

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

> [@funky28](#):
>
> but when the rule executes i get

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.

> [@funky28](#):
>
> I suspect the issue is around the fact that the variable is not recognized as a string and thus I get the error,

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

> [@mstormi](#):
>
> The last variable ALWAYS needs to be declared because it defines the return value. So if you want to pass one parameter you need to have two variable declared in the `val`

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](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888) 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.

> [@funky28](#):
>
> variables declared i came up with:

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.

> [@mstormi](#):
>
> Lambdas you shouldn’t use as a beginner as they’re the most advanced construct in rules DSL and the one to have the most pitfalls.

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.

> [@mstormi](#):
>
> you must declare the Function to have a specific number of parameters, see other thread

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.

> [@chriscolden](#):
>
> Where Function2 is the number of parameters were passing in. The types go in the \< \> plus the additional return type of Boolean, function name in this case is test\_lambda.
> 
> Always return true, unless your logic can support other states or return variables. Mine usually dont, so I just return true.

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

```

would also work.

---

_[View the full topic](https://community.openhab.org/t/solved-conditional-statement-with-a-lambda-function-variable/79753)._
