Combining similar rules

They’re very separate events. Definitely didn’t want things re-triggering.

Here are the rules as they stand (using @5iver’s hint):

rule "Ceiling Fans - Individual via 2.4 Nextion"
when
	Member of gLrFans24 received command
then
	Thread::sleep(100)
	sendCommand(triggeringItem.name.replace("lr24","lr"),if (triggeringItem.state == 1) "ON" else "OFF")
	postUpdate(triggeringItem.name.replace("lr24","lrOh"),if (triggeringItem.state == 1) "ON" else "OFF")
	sendCommand(triggeringItem.name.replace("lr24Fan","lr35f"),if (triggeringItem.state == 1) "1" else "0")
end

rule "Ceiling Fans - Individual via 3.5 Nextion"
when
	Member of gLrFans35 received command
then
	Thread::sleep(100)
	sendCommand(triggeringItem.name.replace("lr35","lr"),if (triggeringItem.state == 1) "ON" else "OFF")
	postUpdate(triggeringItem.name.replace("lr35","lrOh"),if (triggeringItem.state == 1) "ON" else "OFF")
	sendCommand(triggeringItem.name.replace("lr35Fan","lr24f"),if (triggeringItem.state == 1) "1" else "0")
end

Using his example or yours, how could I combine these two? I don’t know quite well enough how to manipulate item names inline. Also I had no idea so much could be done inline of the sendcommand/postupdate, very cool.

inputs to openhab from touchpanel
lr24Fan1(1-4)
lr35Fan1(1-4)

outputs to panel from openhab
lr24f1(1-4)
lr35f1(1-4)

OH switch for same
lrOhFan1(1-4)

Proxy to sonoff switch:
lrFan1(1-4)

I have groups already created to combine all of the fan switches (gLrFans) for when I understand how the rules can be combined.

The replace method can be used more than once. Also, use receivedCommand and remove the Thread::sleep.

rule "Ceiling Fans"
when
	Member of gLrFans24 received command
    or
    Member of gLrFans35 received command
then
	sendCommand(triggeringItem.name.replace("lr24","lr").replace("lr35","lr"),if (receivedCommand == 1) "ON" else "OFF")
	postUpdate(triggeringItem.name.replace("lr24","lrOh").replace("lr35","lrOh"),if (receivedCommand == 1) "ON" else "OFF")
	sendCommand(triggeringItem.name.replace("lr24Fan","lr35f").replace("lr35Fan","lr24f"),if (receivedCommand == 1) "1" else "0")
end
2 Likes

I’m trying to use multiple conditions but these are getting triggered anyway:

sendCommand(cornerAutoNXTOut, if ( triggeringItem.name == "cornerVentMode" && ventMode.state == 2) 1)

Is the contained if statement only capable of evaluating 1 thing?

I’m seeing this send command happening no matter what the triggering item is

No.

What are you trying to do in your rule? The If expression will always return a value, and the sendCommand will always be sent. You also needed an ‘else’ or it will always send 1. If you don’t want to always fire the sendCommand, use…

if ( triggeringItem.name == "cornerVentMode" && ventMode.state == 2) {
    sendCommand(cornerAutoNXTOut, 1)
}

eh, i was trying to do it in one line like your examples. I’ve reverted to something similar to what you just posted.

The problem is, it’s sending a command (0) even with no “else” ( i was hoping it would just bail out and not send anything) and it’s sending the command even if the triggeringItem.name != cornerVentMode

edit seems to be happening with a conventional if statement, will investigate further and update later on

The trinary operator doesn’t work like that. You must have an else. And it always returns a value.

I created another rule to replace mutiple rules:

rule "Distance readable"
when
    Member of G_Dist changed
then
    var itemName = triggeringItem.name.toString
	logInfo("geofencing.rules", "Triggering item: " + itemName)
	var String readable = "hmm..."
	var Number m = triggeringItem.state as DecimalType
	var Number km = m / 1000
  // change to readable format
  	if(m >= 0) {
		switch m {
//			case 0 : readable = "zuhause"
			case m < 1000 : readable = String::format("%.0f m", m.floatValue())
			case m < 50000 : readable = String::format("%.1f km", m.floatValue())
			case m >= 50000 : readable = String::format("%.0f km", km)
	  	}
		var String itemNameStr = itemName + "_Str"
// klappt das so?
		itemNameStr.postUpdate(readable)
  		logInfo("geofencing.rules", "Distance of " + itemNameStr + " to Home set to " + readable)
 	}
 	else {
 	// if distance is < 0, keep the current Dist_Str
  		logInfo("geofencing.rules", "Distance of " + itemName.state.toString + " is negative so current Distance String will be kept")
 	}
end

In the log it looks like two distances (in the G_Dist) Group changed and triggered the rule:

2018-07-31 19:12:10.718 [INFO ] [rthome.model.script.geofencing.rules] - Triggering item: mqttDNADist
2018-07-31 19:12:10.740 [INFO ] [rthome.model.script.geofencing.rules] - Triggering item: mqttNCODistDNA
2018-07-31 19:12:10.748 [INFO ] [rthome.model.script.geofencing.rules] - Distance of mqttDNADist_Str to Home set to 2287,0 km
2018-07-31 19:12:10.780 [INFO ] [rthome.model.script.geofencing.rules] - Distance of mqttNCODistDNA_Str to Home set to 2294,0 km

Does this mean the rule runs twice with both triggeringItems without interference?

That is how it is supposed to work. Each event generates a separate instance of the Rule and they run in parallel. As long as the two instances do not use any shared resources (global vars, commanding or updating Items) they will run at the same time without interference.

perfect - thanks. openhab rocks.
I’m loving it more and more each day… :slight_smile:

1 Like