Item name as variable?

Hi all,

I tried to search the forum about storing item names as variables in rules, some seems to get it working and others do not. Is this possible?

For example how can I make this work without getting "An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(java.lang.String,java.lang.String) on instance: null
"?


rule "Please make it work"
when
        Member of gTest changed
then
          var triggerItem = triggeringItem.name
          var itemName = triggerItem.substring(0, triggerItem.length - 4).toString
          sendCommand(itemName, ON) //edit :)
end

My dilemma is that I have two sets of variables: “theGoodStuff” and “theGoodStuff_tmp”.
And something is happening in “theGoodStuff_tmp” and I want to process that and store it in the “theGoodStuff”. So I strip the “_tmp” part and hope to send it :slight_smile:

On is not a string
Try “On” , or better “ON” for a Switch type Item.

Funny that you should say that. You helped someone with the exact same problem where the error was that he tried to get SUM from a switch. But please ignore the faults caused by me trying to simplify :slight_smile:

For more discussion on this sort of approach, see Design Pattern: Associated Items. Though I’m not sure that I emphasize the fact that both arguments to the sendCommand and postUpdate Actions must be Strings which is what tripped you up here.

1 Like

It’s not really clear what you state you are at now,

ON is not a string either. Try “ON”

In this example we could imagine that I try to send to a switch item. The focus should be at using the variable as item name. :slight_smile:

You should stop showing us examples with errors in if you don’t want them corrected.

I begin to think you are not showing us ypur real rule for some reason. I’m not playing guessing games - sendCommand() is not complicated, just give it two strings.
If you get an error, you need to look at the supposed strings you are supplying.

Did some more searching and reading and found the solution

Please share, so others can learn from it. ;o)

Dont know if I dare :slight_smile:

1 Like

We all make mistakes (and often the same “stupid” ones)… :smiley:

True :100:

This worked for me:
I created a second group called gTest2 for the “theGoodStuff” items (the target for the processed value).
Then I used the following lines to:

  1. Get the item that triggered the rule (theGoodStuff_tmp) and remove the “_tmp” part
  2. Get the target item from the string “itemNameStr”
  3. Send it to the theGoodStuff item.
val itemNameStr = triggeringItem.name.substring(0, triggeringItem.name.length - 4).toString // get trigger item and remove "_tmp"
var itemName = gTest2.members.findFirst[ t | t.name == itemNameStr ] as GenericItem //get target item
sendCommand(itemName, ON)

You might want to check this out too (someone should get this into the docs)…

1 Like

To revisit the original rule …

items

Group gTest
Switch theGoodStuff "switch me"
Switch theGoodStuff_tmp "switch me" (gTest)

rule (exact copy of original post)

rule "Please make it work"
when
        Member of gTest changed
then
          var triggerItem = triggeringItem.name
          var itemName = triggerItem.substring(0, triggerItem.length - 4).toString
          sendCommand(itemName, ON) //edit :)
end

openhab.log error message (exactly as reported)

2020-02-26 22:45:55.405 [ERROR] [ntime.internal.engine.RuleEngineImpl] - rule 'Please make it work': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(java.lang.String,java.lang.String) on instance: null

revised rule (quotemarks only added)

rule "Please make it work"
when
        Member of gTest changed
then
          var triggerItem = triggeringItem.name
          var itemName = triggerItem.substring(0, triggerItem.length - 4).toString
          sendCommand(itemName, "ON") //edit :)
end

demonstration of it working in events.log

2020-02-26 22:56:12.569 [ome.event.ItemCommandEvent] - Item 'theGoodStuff_tmp' received command OFF
2020-02-26 22:56:12.580 [vent.ItemStateChangedEvent] - theGoodStuff_tmp changed from ON to OFF
2020-02-26 22:56:12.585 [ome.event.ItemCommandEvent] - Item 'theGoodStuff' received command ON
2020-02-26 22:56:12.598 [vent.ItemStateChangedEvent] - theGoodStuff changed from NULL to ON

Seriously, all it needed was “ON” instead of ON :wink:

4 Likes

Hey all, I have a simular question. With this example I got it, to definine a variable Item name. My next goal ist to figure out the state of this variable Item.

when 
   Item solarttttt received command  
then
    var Number  stunde = now.getHour() -10
    var triggerItem = "forecast" + stunde
    var itemName = triggerItem.toString
    var erste = itemName.state.toString
    logInfo("Solartest", "" + erste)  

Script execution of rule with UID ‘Solar-7’ failed: ‘state’ is not a member of ‘java.lang.String’; line 347, column 17, length 14 in Solar

Thx for help, Joe