[Solved] Stuck on calculating average in lambda

I’m trying to create a basic function (lambda) to figure the average from values passed to it, but am stuck. I’m running OH1.8 on an RPi.

Here’s the lambda I’m attempting:

val Functions$Function3 testAverage = [ 
	int numOne, int numTwo, int numThree |
		
	logInfo("lambda", "numOne = " + numOne)
	logInfo("lambda", "numTwo = " + numTwo)
	logInfo("lambda", "numThr = " + numThree)
	
	var int exAve = ((numOne) + (numTwo) + (numThree) / 3)   //THIS IS WHERE I'M STUCK
	
	logInfo("lambda", "exAve = " + exAve)
		
	true
	
	]

and the call:

// to call the function in a rule, use:
rule "feed values to function"
when
	Time cron "15 04 15 * * ?"
then
	logInfo("Testing", "Sending values to function...")
	testAverage.apply ("5","10","15")  
end

I get the following error:

  • Error during the execution of rule feed standard deviation sample
    java.lang.IllegalStateException: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_plus(int,int) on instance: null
    at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:738) ~[na:na]

I’m not really sure where to turn from here, and I haven’t found anything in the forums that was able to get me past this point. Can anyone give me some guidance on how to get past this?

Thanks for your time,

Brian

I’m relatively new to openHAB, so YMMV, but I see two main problems:

  • you’re passing strings to the lambda, and trying to operate on them as numbers. Someone more experienced with openHAB will have to tell you how to convert them; there a couple of ways I can see, but I don’t know which one is “correct”.

  • Your math probably won’t be correct. You have (a+b+c/3); this is going to divide c by 3, and then add a and b, in that order. You probably want ((a+b+c)/3). The extra parens will cause the additions to happen first, followed by the division.

hth.

Thanks for your reply. Nice catch on the misplaced parenthesis. I’ve never even made it to that point in this calculation, so I hadn’t seen its misplacement. Thanks!

I understand what you’re saying about sending a string to the lambda, but I guess I was (incorrectly) thinking that it would be converted on the fly since I setup “int” variables in the second line of the lambda (int numOne and int numTwo).

So, does anyone have any tips on converting these strings to numbers on which I can perform calculations? I have tried all sorts of things I’ve seen in the forum, but none work.

Thanks, @floating.io, for the nudge in the right direction. Finally figured this out. For those that may run across this and are looking for a resolution, I set my variables in the second line to “String” instead of “int”. Then I converted the Strings to Numbers. Sample below:

val Functions$Function2 testAverage = [ 
	String numOne, String numTwo |

	var Number intOne = new Integer(numOne)
	var Number intTwo = new Integer(numTwo)
	var Number exAve = (((intOne) + (intTwo)) / 2 ) 
	
	logInfo("lambda", "exAve = " + (exAve))
		
	true
	
	]