[SOLVED] Get state with a calculated item name

From the docs here:

We can set a state value like this:

val index = 5
sendCommand(“My_Lamp_” + index, ON)

But, how do I read the current value?
I’m thinking something like

val index = 5
logInfo (“test”, “MyLamp_” + index + " = " + getState(“MyLamp_” + index).toString)

When generating item names in rules you need a little workaround:

//At the TOP of the rules file
import org.eclipse.smarthome.model.script.ScriptServiceUtil

val index = 5
val testItem = ScriptServiceUtil.getItemRegistry.getItem("Virtual_Switch_1")
logInfo (“test”, “MyLamp_” + index + " = " testItem.state.toString)

I think your question is covered in my solution Option 2
in below post:

so either filter groups, or use the ScriptServiceUtil

1 Like

Solved.

I ended up using the ScriptService. Worked perfectly! Thanks :slight_smile:

1 Like