triggeringItem is an Item.
lrf1 is an Item
triggeringItem.name is a String
The string “lrF1” does not equal the Item lrF1
NOTE: Because triggeringItem.name and itemName are already a String, it is redundant to call toString on them,
rule "whatever"
when
Item lrF1 received command or
Item lrF2 received command or
Item lrF3 received command
then
val itemName = triggeringItem.name
logInfo("triggering item: " , itemName)
if(itemName == "lrF1") {
logInfo("who triggered: ","Fan 1")
fan1.sendCommand(if(receivedCommand == 1) ON else OFF)
}
else if(triggeringItem.name == "lrF2") {
...
Some further notes on what I changed:
- use val instead of var for constants like itemName
- don’t call toString on Strings
- use quotes around “lrF1” to compare it itemName
- use receivedCommand instead of lrF1.state because the Rule will be triggered before the Item get’s updated so lrF1.state is not guaranteed to equal the command that triggered the Rule
- I use the trinary operator to reduce the conversion from lrF1.state to a command to fan1 down to one line.
You can make this Rule generic by applying Design Pattern: Associated Items and Design Pattern: How to Structure a Rule. Then the complete rule could be something like
rule "whatever"
when
Member of lrFs received commands
then
val name = triggeringItem.name
val fanName = "fan" + name.charAt(name.length() - 1)
val newState = if(receivedCommand == 1) "ON" else "OFF"
logInfo("whatever", "Sending " + newState + " to " + fanName)
sendCommand(fanName, newState)
end
That’s all there is to it. Assuming you continue to use the same naming scheme and have no more than 10 lrFs (i.e. no two digit numbers) this rule will work for all of your lrFs and fans.