We have moved at community.getvera.com

Author Topic: How to get an item object from its name ?  (Read 1803 times)

Offline lolodomo

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3484
  • Karma: +74/-10
How to get an item object from its name ?
« on: March 27, 2015, 01:36:16 pm »
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:

Code: [Select]
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.
« Last Edit: March 27, 2015, 01:39:14 pm by lolodomo »

Offline guessed

  • Moderator
  • Master Member
  • *****
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to get an item object from its name ?
« Reply #1 on: March 27, 2015, 01:41:15 pm »
TestItem (no quotes) is the Item.

So you can use:

Code: [Select]
   ...
   var item = TestItem

   logInfo("test", item.name)
   ...

Offline lolodomo

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3484
  • Karma: +74/-10
Re: How to get an item object from its name ?
« Reply #2 on: March 27, 2015, 01:52:38 pm »
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) ?

Offline guessed

  • Moderator
  • Master Member
  • *****
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to get an item object from its name ?
« Reply #3 on: March 27, 2015, 02:12:49 pm »
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)

Offline lolodomo

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3484
  • Karma: +74/-10
Re: How to get an item object from its name ?
« Reply #4 on: March 27, 2015, 02:27:05 pm »
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 ?
« Last Edit: March 27, 2015, 02:38:02 pm by lolodomo »

Offline lolodomo

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3484
  • Karma: +74/-10
Re: How to get an item object from its name ?
« Reply #5 on: March 27, 2015, 02:39:25 pm »
I will verify if I can use the same tip for the first argument of sendMiosAction ?

That is not possible with sendMiosAction.

Offline guessed

  • Moderator
  • Master Member
  • *****
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to get an item object from its name ?
« Reply #6 on: March 27, 2015, 02:49:49 pm »
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.

Offline lolodomo

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3484
  • Karma: +74/-10
Re: How to get an item object from its name ?
« Reply #7 on: March 27, 2015, 03:31:12 pm »
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.

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)

Offline guessed

  • Moderator
  • Master Member
  • *****
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to get an item object from its name ?
« Reply #8 on: March 27, 2015, 05:01:16 pm »
Cool, do you mind posting them as examples?  I'm sure others would like to see them also.