LUA Syntax Question

Sorry, still getting my feet wet in Edge Driver and LUA.

Trying to figure the right syntax for adding a variable object reference.

I am trying to write code to vary the switch number (1 through 5) in this line.

device:emit_component_event(device.profile.components.switch1, capabilities.switch.switch.on())

i.e. I would like to modify this based on switch number.

device:emit_component_event(device.profile.components.switch1, capabilities.switch.switch.on())
device:emit_component_event(device.profile.components.switch2, capabilities.switch.switch.on())

device:emit_component_event(device.profile.components.switch3, capabilities.switch.switch.on())

Trying to add construct to vary the components switch number.

Thanks for your help.

You can build the element dynamically. I assume you mean you want to pass in the button number or its in another variable.

local switchNum = 3
local switchName = "switch" .. switchNum
device:emit_component_event(device.profile.components[switchName], capabilities.switch.switch.on())

-- or you could just do it in line
device:emit_component_event(device.profile.components["switch"..switchNum], capabilities.switch.switch.on())

Or you could just pass in “switch3” as the argument to whatever function you’re using, so the full name is already created. Many ways to construct the element name.