Problems with looping through Items within a group

Hello,

I have trouble finding the mistake in this script. I try to build a lighting control where the user can modify the light settings and then save them to one of many scenes (i.e. Night, TV, Game, Music …)

Every light fixture is within a specific group. Every fixture has one Item for every scene to store the Light (RGB) settings. Part of the SaveScene Function:

val org.eclipse.xtext.xbase.lib.Functions$Function2 SaveScene = [
	String Group, int Scene|

    LightColors.allMembers.filter(i | i.groupNames.contains("LightC_"+Group)).forEach(i |
    	logInfo("Light","Save Scene: "+Scene+" Color: " +i)
    	var String ItemName = i.name
    )
	true
]

This incomplete code loops through all Items inside the Group “LightC_X”, The log Info works as expected:

2016-05-07 10:03:35.058 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Light_1_1_C_0 (Type=ColorItem, State=103.799995,76.114639,43.000000)
2016-05-07 10:03:35.125 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Light_1_3_C_0 (Type=ColorItem, State=33.599998,82.006371,54.000004)
2016-05-07 10:03:35.139 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Light_1_2_C_0 (Type=ColorItem, State=142.800003,70.382164,65.333328)

But the next line give an error

2016-05-07 10:03:36.872 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Save scene’: The name ‘i’ cannot be resolved to an item or type.

The Designer do not like the line "logInfo(“Light”,“Save Scene: “+Scene+” Color: " +i)” = “incompatible types. Expected java.lang.String but was org.openhab.core.items.Item” but it work see log.

I Need the Name of the Item to find the coresponding ID, Group Number.

I always have trouble with type conversions … can somebody help, please?

Chris

Try with:

val org.eclipse.xtext.xbase.lib.Functions$Function2 SaveScene = [
	String Group, int Scene|

    LightColors.allMembers.filter(i | i.groupNames.contains("LightC_"+Group)).forEach(iii |
    	logInfo("Light","Save Scene: "+Scene+" Color: " +iii.state.toString)
    	var String ItemName = iii.name
    )
]

Thank you for your fast response. I copy pasted the code form the forum and was wondering why i was used 2 times but that isn’t the problem, same error still exists:

2016-05-08 10:02:16.201 [INFO ] [org.openhab.model.script.Light] - Save Scene: 3 Color: Light_1_1_C_0 (Type=ColorItem, State=103.799995,76.114639,43.000000)
2016-05-08 10:02:16.248 [INFO ] [org.openhab.model.script.Light] - Save Scene: 3 Color: Light_1_2_C_0 (Type=ColorItem, State=142.800003,70.382164,65.333328)
2016-05-08 10:02:16.257 [INFO ] [org.openhab.model.script.Light] - Save Scene: 3 Color: Light_1_3_C_0 (Type=ColorItem, State=33.599998,82.006371,0)
2016-05-08 10:02:17.898 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Save scene': The name 'iii' cannot be resolved to an item or type.

Chris

Ok, the problem is the brackets of foreach
Not Correct

.foreach(
//...
)

Correct

.foreach[
//...
]

I just tested this with success:

items

Group LightColors
Group LightC_1
Group LightC_2
Group LightC_3

Switch LightC_1_1_C_0	"Color 01"		(LightColors, LightC_1)
Switch LightC_1_2_C_0	"Color 02"		(LightColors, LightC_2)
Switch LightC_1_3_C_0	"Color 03"		(LightColors, LightC_3)


Switch TEST_TRIGGER	"TEST"

rule:

import org.openhab.core.library.types.*
import org.openhab.core.library.items.*
import org.openhab.core.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.core.items.*
import org.eclipse.xtext.xbase.lib.*



val org.eclipse.xtext.xbase.lib.Functions$Function2 SaveScene = [
	String GroupName,
	int Scene |
		LightColors?.allMembers.filter(i | i.groupNames.toString.contains("LightC_" + GroupName)).forEach[iii|
			logInfo("Light","Save Scene: " + Scene + " Color: " + iii.state.toString + "  --> " + iii.name.toString)
			var String ItemName = iii.name
			logInfo("Light","Item Name: " + ItemName)
		]
		true
]

rule "Group Test"
when
	Item TEST_TRIGGER received update
then
	Thread::sleep(500)
	logInfo("TEST","---> RULE STARTED <-----")
	
	SaveScene.apply("1",2)
	SaveScene.apply("2",2)
	SaveScene.apply("3",2)

	
	logInfo("TEST","---> RULE ENDED <-----")
	
end
1 Like

Perfect, you solved my problem, thank you! But as in real life, when one problem is solved the next rise immediately

val org.eclipse.xtext.xbase.lib.Functions$Function2 SaveScene = [
	String GroupName, int Scene|
	LightColors?.allMembers.filter(i | i.groupNames.toString.contains("LightC_" + GroupName)).forEach[iii|
			logInfo("Light","Save Scene: " + Scene + " Color: " + iii.state.toString + "  --> " + iii.name.toString)
			var String ItemName = iii.name
	    	var String mlID = ItemName.substring(7,1)
    		var String mlGroup = ItemName.substring(9,1)
			logInfo("Light","Item Name: " + ItemName + "Save ID:"+mlID+" Group:"+mlGroup)
		]
    true
]

The substring isn`t working as expected. The function stop without an error message, so no info in the log file. I think the lambda declaration could be the problem …

Chris

public String substring(int beginIndex, int endIndex)

This is working for me (inside lambda):

//..
var String mlID = ItemName.substring(1,7)
var String mlGroup = ItemName.substring(1,5)
logInfo("Light","Item Name: " + ItemName + "Save ID:" + mlID + " Group:" + mlGroup)
//..

And this is the output:

2016-05-08 12:14:09.387 [INFO ] [org.openhab.model.script.TEST ] - ---> RULE STARTED <-----
2016-05-08 12:14:09.483 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Uninitialized  --> LightC_1_1_C_0
2016-05-08 12:14:09.509 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_1_C_0
2016-05-08 12:14:09.553 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_1_C_0Save ID:ightC_ Group:ight
2016-05-08 12:14:09.570 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Uninitialized  --> LightC_1_2_C_0
2016-05-08 12:14:09.571 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_2_C_0
2016-05-08 12:14:09.574 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_2_C_0Save ID:ightC_ Group:ight
2016-05-08 12:14:09.586 [INFO ] [org.openhab.model.script.Light] - Save Scene: 2 Color: Uninitialized  --> LightC_1_3_C_0
2016-05-08 12:14:09.588 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_3_C_0
2016-05-08 12:14:09.590 [INFO ] [org.openhab.model.script.Light] - Item Name: LightC_1_3_C_0Save ID:ightC_ Group:ight
2016-05-08 12:14:09.594 [INFO ] [org.openhab.model.script.TEST ] - ---> RULE ENDED <-----

You have to adjust indexes to fit yor needs

Thank you! I know substring(index,characters), but here it is substring(start,end) beginning with zero. strg+space only gives me p1,p2 ??? you have to guess. And without error messages (is this normal?) I was lost.

Sorry, I’m new to openHAB and adding new features only from time to time.
So now I can go on doing usefull stuff :wink:

Chris

This maybe can be usefull…

Thank you. Funny that you have a similar approach for scene control.

Currently I’m not to enthusiastic doing it because I’m facing to many “workarounds” especially because of the limitation of the UI. (Not possible to use dialog boxes (to confirm save for example) or text input (to name scenes) …

Even if the app and the software is crappy the concept in the Logitech Harmony Hub isn’t too bad. Based in one room you can choose between different “activities”. Every Activity have a start and end sequence where you can pull in different devices and commands. The transition is generated automatically I.e switch off the Beamer when you switch between TV and Music, switch off the receiver, switch on Apple TV and change the input of the sound bar). Switching the Beamer off when changing from tv to Xbox would be a disaster because my Beamer always cool down first before it accepts an on command.

I’m not happy with the limitation on lights only, I like to control everything and configure it through the UI.

BTW: I use an different approach for the save button

I use a selection List. This gives the possibility to press cancel (Abbruch) after the first click. - Dialog Box workaround