[SOLVED] Dynamic Item Name in rule

Below is rule I’m trying to build to allow me to trigger proxy items, in the case here it’s my motion sensors. For the rule to work I need to build the Item name and then send the command to the Item. How can I change the rule below to make it work?

val String filename = "vMotion_Sensors.rules"

rule "Which Motion Sensor"
    Member of gMotion_Sensors changed
if(previousState == NULL) return;
val vLocation = triggeringItem.name.split("_").get(0)
val vDevice = triggeringItem.name.split("_").get(1)
val vStatus = triggeringItem.state
logInfo(filename, "Motion Sensors: Activation Zone " + vLocation)
logInfo(filename, "Motion Sensors: " + vLocation + " is " + vStatus)
val vItem = vLocation + "_" + vDevice + "_vMotion"
logInfo (filename, "Testing: " + vItem + " " + vStatus)

For reference here’s an example Motion Sensor Item

Switch  Kitchen_SP3102_Motion "Motion Sensor" <motion> (gMotion_Sensors) { channel="zwave:device:49d470ee:node19:sensor_binary" }

And here is the proxy Motion Sensor Item

Switch Kitchen_SP3102_vMotion (gMotion_Sensors)

It’s a subtle change to the Item



There are four options I can think of, but the easiest is to use the sendCommand Action rather than using the sendCommand method…

sendCommand (vItem, vStatus)

You may also learn something from this…

The new rule engine currently only uses something similar to the Action.

Thanks for your response Scott. I’ve changed my rule as you suggested below, sadly that’s not worked.

Here’s the error I get, if it helps.

2020-02-08 17:47:19.047 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Which Motion Sensor': 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

From your original post , vStatus is a state object. which isn’t quite the string that sendCommand() asks for.
sendCommand (vItem, vStatus.toString)

1 Like

Thanks @rossko57, that worked perfectly.