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
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
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.
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.
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:
Get the item that triggered the rule (theGoodStuff_tmp) and remove the “_tmp” part
Get the target item from the string “itemNameStr”
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)
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
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