Create Light Timer & lambda

Hi there,

i have this code and I don´t know how to pass the actual lightItem (variable) into the createTimer

r ule "LIGHT-TIMER"
when
Item group____LIGHT received update
then
var Timer timer

	Thread::sleep(200)
	
	group____LIGHT?.allMembers.filter(lightItem|lightItem.changedSince(now.minusSeconds(1))).forEach[ lightItem |
		logInfo("TIMER", lightItem.name)
		if(lightItem.state == ON)
		{
			var timeValue = 30
			if(now.getHourOfDay > 7 && now.getHourOfDay < 18)
			{
				timeValue = 2
			}
			logInfo("TIMER", "activate timer "+timeValue+" Minutes: "+lightItem.name)
			var timer = createTimer(now.plusMinutes(timeValue), [
				//some code which should interact with lightItem from outside this lambda....
                           //when I reference it directly I get an null in log
			])
		}
	        // Save the date/time for openings
	]
end

Anybody can help me ?

I’m not too good at lamdas…but i think you will need to pass the actual name of the item and use another group function to act on it. Something like so(but it doubt it will be 100% right; though the idea itself should be right):

//save name in a variable and pass it on..
val TheItem =lightItem.name.toString
//don't know how to return a variable..
//then "outside", something like:
group____LIGHT?.members.filter(s|s.name == TheItem).forEach[s | s.sendCommand(YOUR-COMMAND-HERE)]	

HTH,
-OLI

Depending on how the “code” is processed and converted into the Java of the class that is defined to implement your rule file, it may be that the inner lambda expression is binding to the value of lightItem from the outer loop, which is null at the time the code is converted to the Java class.

You might try creating a poor-man’s function by binding the lambda to a val in the definition section and then using Procedure.apply(arg0, arg1,...) within the loop.

An example from my rules of this idea is:

val dimmer_group_send_stop = [ GroupItem dimmer_group |
        dimmer_group.allMembers.forEach [ dimmer |
                sendCommand(dimmer, STOP)
        ]
]

and as a rule action

then
        dimmer_group_send_stop.apply(group_Bedroom_dimmers)

I´ll try this when I´m at home tonight.
Somehow lambdas make my head ache. Do you know that when your synapses in your head start to cramp ? :slight_smile:

Yeah 100% with you there. It’s one of those things where you really need that “AHA!! I FINALLY GOT IT!!” moment until it just flows :wink:
Did not have my moment as of now though :frowning:

Hi,

wasn´t able to accomplish anything, yet.
Found something that there was a function called createTimerWithArgument … is that still available … ?
Do I have to import anything to be able to use that ?