Groovy Section statement, "Unexpected Error"

Placing a Groovy Section statement inside an iteration, such as each, always generates an error. Seems to destroy the iteration.

Source code

definition(
		name: "testSection",
		namespace: "hubitat",
		author: "AAB",
		description: "test section",
		category: "Convenience",
		iconUrl: "",
		iconX2Url: ""
)
preferences {
    page(name: "mainPage")
	}

def mainPage()
	{
	dynamicPage(name: "mainPage", title: "Test Section", install: true, uninstall: true)
		{
		section
			{
			input "globalLights", "capability.switch", required: true, multiple: true, submitOnChange: true,
				title: "One or more Bulbs, Leds or Switches"
			}
		if (globalLights)
			{
			globalLights.each
				{
				section ("${it.label}", hideable: true, hidden: true)
					{
					input "global${it.id}Lux", "number", required: false, multiple: false, range: "1..8000",submitOnChange: true,
						title: "${it.label}<br />Lux On/Off point 1 to 8000. Leave blank to use Standard Lux settings (Optional)"
					}
				}
			}
		}
	}
def installed() {
    initialize()
}

def updated() {
    initialize()
}

def initialize()
	{
}

This will work if you explicitly name the closure parameter, so instead of:

globalLights.each { ... }

try:

globalLights.each { globalLight -> ... }

...using globalLight instead of it inside your closure.

My guess is that section itself is implemented as a closure, making the implicit it be null in that inner context, but I don't know enough about Hubitat's implementation or Groovy nuts-and-bolts to give you a good explanation here. :slight_smile:

2 Likes

Thank You!

That works.