How to get an item object from its name ?

The idea is to search an item when you have its name dynamically built in a variable.

I used the following code, it is working but it requires a loop amongst all members of a group:

var String itemName = "TestItem" GVoice?.members.forEach[item| if (item.name == itemName) { // do something with item like item.sendCommand... } ]

Could we avoid the loop and get the object with a simple API call ?

Note that the code is doing a one level depth search only.

[tt]TestItem[/tt] (no quotes) is the Item.

So you can use:

[code] …
var item = TestItem

logInfo(“test”, item.name)

[/code]

Ok, few more precisions. I am in the treatmean of a voice command. Depending on the voice message received, I build the name of the item in a string variable. My string variable is named itemName. So itemName is not the name of one of my items, but just the name of a variable containing the name of one of my items.
The next step is how to get the item ?

I have suddenly a doubt. Could I simply use this code: sendCommand(itemName, ON) ?

It might work, but I’ve not tried the sendCommand variant where the first parameter is a String instead of the [more usual] Item.

Also note that this variant appears to require the second parameter to be a String also, not just a Command (like ON, OFF, INCREASE, etc)

Oh yes, it is working 8) Very simple finally: sendCommand(itemName, “ON”)

I will verify if I can use the same tip for the first argument of sendMiosAction ?

That is not possible with sendMiosAction.

Correct, I don’t have that overload.

If you add an issue to GitHubs issue tracker for openhab then I’ll add one when I get some cycles.

[quote=“guessed, post:7, topic:186642”]Correct, I don’t have that overload.

If you add an issue to GitHubs issue tracker for openhab then I’ll add one when I get some cycles.[/quote]

Not necessary. I can easily initialize my item variable before calling sendMiosAction.

I have now something clean in my rules. I am really surprised how simple is my rule to handle voice control. 8)

Cool, do you mind posting them as examples? I’m sure others would like to see them also.