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
//declaration
val normalLightsList = newArrayList( GWPN6_32_sw,
Hue1_sw,
FGWP101_3_sw )
// in a rule
val itr = normalLightsList.iterator
while(itr.hasNext)
{
val str = itr.next
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
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?
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.