Lambda example from Wiki doesn't work?

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

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?

1 Like

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:

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 (%)

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

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

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

Thanks - exactly what I needed :slight_smile:

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

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:?

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:

logInfo(Lambda, output.state.toString) 

the Designer wouldn’t like this…

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.

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 :slight_smile:

But i get an error when executing the lambda:

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:

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:

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

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