I have a bunch of rules which are very similar (see below).
May I kindly ask for what would you’d suggest on how to simplify these rules, maybe and ideally with one rule.
I thought of using the overarching group to the iterate through the members of for the last updated OFF state… but could not get this to work.
rule "Irrigation1 valve 1 OFF"
when
Item Irrigation1_1 received command OFF
then
var int valveFlow = (PumpStation1_Flow.state as Number).intValue
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Flow1", valveFlow.toString)
var int valveVolume = (PumpStation1_Meter.state as Number).intValue
valveVolume = valveVolume - PumpStation1_MemoriseMeterRead
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Volume1", valveVolume.toString)
PumpStation1_LastSessionMemory.postUpdate((PumpStation1_LastSessionMemory.state as Number).intValue + valveVolume)
end
rule "Irrigation1 valve 2 OFF"
when
Item Irrigation1_2 received command OFF
then
var int valveFlow = (PumpStation1_Flow.state as Number).intValue
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Flow2", valveFlow.toString)
var int valveVolume = (PumpStation1_Meter.state as DecimalType).intValue
valveVolume = valveVolume - PumpStation1_MemoriseMeterRead
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Volume2", valveVolume.toString)
PumpStation1_LastSessionMemory.postUpdate((PumpStation1_LastSessionMemory.state as Number).intValue + valveVolume)
end
rule "Irrigation1 valve 3 OFF"
when
Item Irrigation1_3 received command OFF
then
var int valveFlow = (PumpStation1_Flow.state as Number).intValue
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Flow3", valveFlow.toString)
var int valveVolume = (PumpStation1_Meter.state as DecimalType).intValue
valveVolume = valveVolume - PumpStation1_MemoriseMeterRead
publish("mymosquitto", "ArgyleCourt/Property/PumpStation1/Volume3", valveVolume.toString)
PumpStation1_LastSessionMemory.postUpdate((PumpStation1_LastSessionMemory.state as Number).intValue + valveVolume)
end
The new rule engine has been available for several years and can be installed through Paper UI in OH2. Here is an easy way to install Jython and the helper libraries…
If you have OH2, then you have Things. Your Items are linked to Channels inside of the Things.
If you are interested in moving forward with Jython, let me know and I will help out! If not, everything I did in my example can be done in the rules DSL… just not as easily.
But you are using a recent version of OH2? Hopefully 2.5? You still should use Paper UI to configure some stuff, even if you have everything configured in text files. I have my Items in files, but I use managed Things (created by the system and not in files except for the jsondb text files).
Hmmm… are you not using the OH2 MQTT binding? Then IIRC there is an MQTT action that you can access through core.actions.py.
I had no need for PaperUI; so why use it?
I also stay corrected: I just checked, and see that I have one .things file for astro, network and logreader (set up once and forgotten about.)
I did not use the QH2 MQTT broker due to performance/drop-out issues, and stayed with mosquitto, also due to using Owntracks.
I will migrate to OH3 and explore different ways of working after having set-up the environment on a rPi4.
See Design Pattern: Cascading Timers for one such approach. Essentially what you need to do is parse out the pump station number valve number from the Item name that triggers the Rule. That tells you the valve that turned off. Then you can use those numbers to build the Item names and MQTT Topic names to send the messages to.
Put all the Irrigation Items into a Group, I’ll call it PumpStation1. If you have more than one PumpStations you might consider similarly parsing the pumpstation number out of the Item name.
import org.eclipse.smarthome.model.script.ScriptServiceUtil
rule "PumpStation1 Irrigation Item received command OFF"
when
Member of PumpStation1 received command OFF
then
val parts = triggeringItem.name.split('_')
val valve = parts.get(1)
val valveFlow = PumpStation1_Flow.state as Number
val valveVolume = (PumpStation1_Meter.state as Number) - PumpStation1_MemoriseMeterRead // gloval variable?
val topicRoot = "ArgyleCourt/Property/PumpStation1/"
// MQTT 1.x version
publish("mymosquitto", topicRoot+"Flow"+valve, valveFlow.toString)
publish("mymosquitto", topicRoot+"Volume"+valve, valveVolume.toString)
// MQTT 2.x version
// val mqttActions = getActions("mqtt","mqtt:broker:mymosquitto") // use Thing ID for broker Thing
// mqttActions.publishMQTT(topicRoot+"Flow"+valve, valveFlow.toString)
// mqttActions.publishMQTT(TopicRoot+"Volvume"+valve, valveVolume.toString)
PumpStation1_LastSessionMemory.postUpdate((PumpStation1_LastSessionMemory.state as Number) + valveVolume)
end