Is there a way to create functions/routines in openhab?

I have found that if you have more than four arguments to a lambda it is a code smell. There is almost surely a better way to do what you want using fewer arguments or without using lambdas by taking advantage of Groups, storing state in Items, and or using other tricks to combine the rules that use the lambdas rather than having the rules call the same lambdas.

i eventually ended up writing the whole rule, then search and replace for other objects


i wanted to calculate tank volume , percentage, etc using ultrasonic distance, dimensions of the tank (WxHxD) and tapping point of the pipe and tank type (in case if it was cylindric) . that is total, the 7th is the result (item.postUpdate)

i know i could made it smaller, i just wanted to made it all in one shot

Thanks for the info! Where can I read more about the new rules engine?

Probably on the ESH forum or github page. It is experimental so there really isn’t any docs yet.

Just found this! http://docs.openhab.org/configuration/rules-ng.html

If you REALLY want to pass more than 6 arguments, you can use a simple trick of passing a hashmap to the lambda function, where the hashmap contains all the parameters you want to pass to the lambda. This overcomes the number of arguments restriction.

Just wanted to add something: A Function that does not return anything is a Procedure. So instead of using a Function one can use a Procedure. This is done in exactly the same way (only it does not return anything and consequently does not force you to return anything). Example using a Procedure that takes two arguments:

import org.eclipse.xtext.xbase.lib.Procedures

val Procedures.Procedure2<Integer, String> myProcedureWithTwoArguments = [someIntArg1, someStringArg2 |
	// do something and return nothing
]
3 Likes