Cannot refer to the non-final variable i inside a lambda expression

Tags: #<Tag:0x00007f2faff0d3c8> #<Tag:0x00007f2faff0d238>

Hi All,

I’m trying to read a variable from inside a loop (Lambda) but getting an error about “non-final variable”. I understand why i’m getting this error but I don’t know how to break up my rule so I can read the variable.

Can any one assist?

Thanks
Glenn

rule "Test"
	when
		Item Testa received update
	then

		if (Testa.state == ON){
			
			var i = 1
    		while((i=i+1) < 360) {
    			
    			GF_Hall?.allMembers.forEach(item|
					if( item.name.toString.startsWith("Light_") && item instanceof ColorItem){

				   		item.sendCommand(i + ",100,100")
					}
				)
	        	Thread::sleep(500)
	        	if (Testa.state == OFF){
	        		i=360
	        	}
		    }
		}
end

The workaround is to define i as a global variable.

1 Like

Actually you can just ignore that error; it’s just Designer acting naughty. I have several rules that run fine even though Designer complains.

Both answers 100% worked.

Thanks for the help!

Thanks :smile: