# Lambda example from Wiki doesn't work?

**URL:** <https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861>\
**Category:** Runtime\
**Created:** [October 28, 2016, 11:06am UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861 "2016-10-28T11:06:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [October 28, 2016, 11:06am UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/1 "2016-10-28T11:06:56Z")

</div>

Hello Guys.  
when pasting the Lambda example from the GitHub WIKI the Smarthome Designer doesn’t accept theese… It doesn’t like:

```auto
val java.util.Map<String, org.openhab.model.script.actions.Timer> rolloTimers = newHashMap

// lambda expression that can be used as a function (here: with 5 parameters)
val org.eclipse.xtext.xbase.lib.Functions$Function5 rolloLogic = [
    org.openhab.core.library.items.SwitchItem relayItem,
    org.openhab.core.library.items.SwitchItem relayItemOpposite,
    java.util.Map<String, org.openhab.model.script.actions.Timer> timers,

```

Is there anything special needs to be done when using this in OH2?

---

<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:** [October 28, 2016, 6:42pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/2 "2016-10-28T18:42:37Z")

</div>

First of all, Designer is currently experiencing problems and is not recognizing things it should.

Second of all, you need to tell us what the errors Designer is telling you.

Finally, without knowing what the errors it is complaining about, I’m going to guess that it cannot find `org.openhab.core.*` because all of those classes have moved in OH 2. Get rid of the fully qualified class names for all the org.openhab classes and the code should run. Though because of the aforementioned bug in Designer, it may still be marked as an error.

I wrote up a simpler example of how lambdas work here:

> [@Reusable Functions: A simple lambda example with copious notes](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888):
>
> Major Edit: Rewrote with the cleaner OH 2.3 syntax. The example lambda on the OH 1 wiki is overly complex and as written will not work in OH 2. This quick example will show a super simple lambda example with copious notes. Here is the code without comments. It takes in an Item, logs it’s state and returns a slightly modified version of the String logged. val log= [GenericItem s | logInfo("lambda", s.state.toString) s.state.toString + " logged"] rule "Call Lambda" when // some t…

---

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [October 28, 2016, 7:20pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/3 "2016-10-28T19:20:20Z")

</div>

Hello Rick…  
Yes it’s the Org.openhab things it can’t find…  
yes I saw your example, but you don’t use any switches of numbers as inputs in your lambda… I need to use a couple of switches, 2 numbers and a dimmer (%)

---

<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:** [October 28, 2016, 8:03pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/4 "2016-10-28T20:03:55Z")

</div>

Then just add them as arguments and adjust the $Function to the number of arguments you do need.

---

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [October 29, 2016, 6:54pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/5 "2016-10-29T18:54:30Z")

</div>

I don’t understand what to write when using switch and number, can you please come with an example? 🙂 just about the defining items 🙂

---

<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:** [October 29, 2016, 10:10pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/6 "2016-10-29T22:10:09Z")

</div>

```auto
val Functions$Function2 myLambda = [ SwitchItem switch, NumberItem num |
    Yada yada yada
]

```

---

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [October 30, 2016, 6:10pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/7 "2016-10-30T18:10:19Z")

</div>

Thanks - exactly what I needed 🙂

---

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [October 31, 2016, 12:34pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/8 "2016-10-31T12:34:38Z")

</div>

Now i tested the Lambda - seems like the Designer doesn’t like the

```javascript
logInfo("lambda", "Setting valve to 0")

```

why doesn’t it accept that?

Also do i understand correct, that the output should also be assigned to variables inside the lambda, so that the last defined function si the output, like the one below:?

```javascript
import org.eclipse.xtext.xbase.lib.*
val Functions$Function5 heating= [ SwitchItem Heating_Stop_bit, NumberItem Temp_SP, NumberItem Temp_Act, DimmerItem OFF_Value, DimmerItem Valve |  
	
	if (Heating_Stop_bit == OFF && Temp_Act.state < (Temp_SP.state as DecimalType + 3.0))
		{
			if (Temp_Act.state < Temp_SP.state as DecimalType)
			{
			sendCommand(Valve, 100)
// logInfo("lambda", Valve.state.toString)	
			}
			else {
				sendCommand(Valve, OFF_Value.state.toString)
// logInfo("Køkken Alrum", "Sætter varme ventil til OFF værdi")
			}
		}
	else {
	sendCommand(Valve, 0)
//	logInfo("Køkken Alrum", "Sætter varme ventil til 0")
	} 
]

```

How to deal with loginfo within a lambda, so i can see what it has changed from and to? Would it be possible to something like:

```javascript
logInfo(Lambda, output.state.toString) 

```

the Designer wouldn’t like this…

---

<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:** [October 31, 2016, 4:45pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/9 "2016-10-31T16:45:33Z")

</div>

> [@Rasmus7700](#):
>
> seems like the Designer doesn’t like

Designer has a bug right now where it is isn’t recognizing Items and Actions (logInfo is an Action).

If you hover your mouse over the red underline it will pop-up specifically what the problem is.

For now, we cannot accept Designer errors as being real until we prove that it is a problem when it runs. This bug has been a real problem for me too.

There also might be a problem when logInfo is the last line of the lambda. If Designer is complaining that there are no side effect this is the problem. You can solve it by making the last line of the lambda be `true`. This will give the lambda something to return. I do talk about this in the comments of the example at the link I provided above.

---

<div class="post-metadata">

**Author:** ![Rasmus7700](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@Rasmus7700](https://community.openhab.org/u/Rasmus7700)\
**Post date:** [November 1, 2016, 7:06am UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/10 "2016-11-01T07:06:35Z")

</div>

Hello Rick.  
It worked, just switched the statement so the logInfo was the first thing, and then after that the sendCommand statement - now the designer is happy - meaning me also happy 🙂

But i get an error when executing the lambda:

```javascript
import org.eclipse.xtext.xbase.lib.*
val Functions$Function6 heating= [ SwitchItem Heating_Stop, NumberItem Temp_SP, NumberItem Temp_Act, DimmerItem OFF_Value, NumberItem Setpoint_Correction, DimmerItem Valve |

var Number setpoint_calc
setpoint_calc = (Temp_SP.state as DecimalType - Temp_Act.state as DecimalType) * Setpoint_Correction.state as DecimalType

if (Heating_Stop == OFF)
{
	logInfo("Varme", Valve.state.toString)
	sendCommand(Valve, setpoint_calc.toString)
}	
else
{
	sendCommand(Valve, 0)

```

Rule:

```php
rule "Varme Alrum"
when
	Item Temp_AVG_Living_Room received update
	then
		heating.apply(Heating_Stop_bit, Temp_Living_SP, Temp_AVG_Living_Room, OFF_Heating_Living, Setpoint_Correction, Heating_Living_Room)			
end

```

Debugger message:

```javascript
[ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Varme Alrum': org.eclipse.smarthome.core.library.types.DecimalType

```

---

<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:** [November 1, 2016, 2:38pm UTC](https://community.openhab.org/t/lambda-example-from-wiki-doesnt-work/15861/11 "2016-11-01T14:38:38Z")

</div>

Do all of the Items you are passing have a value (i.e. not Undefined)?
