How do I "correctly" create a list of switches in the *.rules file?

How do I “correctly” create a list of switches in the *.rules file?
This works if I refresh the *.rules file after a system startup.
If I don’t refresh it then I just get (which isn’t all that strange):

Rule 'allLightsOff button': cannot invoke method public java.util.Iterator java.util.ArrayList.iterator() on null

*.items file

Switch    GWPN6_32_sw             "GWPN6_32 - On/Off Switch"                                      (sw)                  { channel="zwave:device:532a6761:node9:switch_binary2" }
Switch    Hue1_sw                 "HUE1 - On/Off Switch"                                          (sw)                  { channel="hue:0210:001788408fa5:1:color" }
Switch    FGWP101_3_sw            "FGWP101_3 - On/Off Switch"                                     (sw)                  { channel="zwave:device:532a6761:node18:switch_binary" }

*.rules file

val normalLightsList = newArrayList( GWPN6_32_sw, 
                                     FGWP101_3_sw ) 

// in a rule
    val itr = normalLightsList.iterator
      val str =
      sendCommand(str, ON)

During loading of the *.rules file I get

Cannot reference the field 'Hue1_sw' before it is defined
Cannot reference the field 'GWPN6_32_sw' before it is defined
Cannot reference the field 'FGWP101_3_sw' before it is defined

OpenHab uses Groups to allow you to work with multiple items of a type. Here are a couple of references to get you started:

From the Doc’s:

A good tutorial to get you started: Design Pattern: Working with Groups in Rules

Reviving this topic, since I have the exact same problem as the OP.

The reply above offers an alternative means of achieving the desired result, but it doesn’t address the original problem: references to items in the initialization block of the rule cause an error.

Are items not expected to be defined outside of a rule? I’m trying to set a val to point to the group my rules should use, so I can easily retarget the rules to a smaller group of things during testing. (I’m working on lighting automation, and it’s annoying to have the whole room plunged into darkness every time I want to test the “automatic lights out” rules…)

So: is it (or should it be) possible to reference items in the variable declarations at the start of a rule file?


1 Like

I do the same in my rule:

val HashMap<String, GenericItem> ThingsMap = newHashMap (
										"zwave:device:86910632:node21" -> Plug_Garden_TerraceRight_Ping, 				//Plug_Garden_TerraceRight
										"zwave:device:86910632:node5"  -> Illumination_Garden_Blind_Ping,				//Illumination_Garden_Blind
										"zwave:device:86910632:node11" -> Illumination_GroundFloor_LivingRoom_Ping,		//illumination_GroundFloor_LivingRoom
										"zwave:device:86910632:node10" -> Plug_Garden_TerraceLeft_Ping,					//Plug_Garden_TerraceLeft
										"zwave:device:86910632:node6"  -> Pump_Basement_Circulation_Ping,				//Pump_Basement_Circulation
										"zwave:device:86910632:node19" -> SmokeDetector_2ndFloor_Entrance_Ping,			//SmokeDetector_2ndFloor_Entrance
										"zwave:device:86910632:node12" -> SmokeDetector_2ndFloor_KidsRoom_Ping,			//SmokeDetector_2ndFloor_KidsRoom
										"zwave:device:86910632:node18" -> SmokeDetector_2ndFloor_SleepingRoom_Ping,		//SmokeDetector_2ndFloor_SleepingRoom
										"zwave:device:86910632:node3"  -> SmokeDetector_GroundFloor_Entrance_Ping,		//SmokeDetector_GroundFloor_Entrance
										"zwave:device:86910632:node2"  -> SmokeDetector_GroundFloor_LivingRoom_Ping,	//SmokeDetector_GroundFloor_LivingRoom
										"zwave:device:86910632:node9"  -> Valve_Garden_Left_Ping,						//Valve_Garden_Left
										"zwave:device:86910632:node4"  -> Valve_Garden_Right_Ping, 						//Valve_Garden_Right
										"zwave:device:86910632:node8"  -> Door_GroundFloor_Terrace_Ping	 				//Door_GroundFloor_Terrace

In the log this will cause a warning as mentioned in this topic, but in the end everything is working.
If it is not working, you maybe should define that the array list contains GenericItems.

But in your case it still would be easier to put all your Items in a group and “foreach” through this group.