Pause in Rules

Hello everyone!
I use the rules https://community.openhab.org/t/i-o-module-bit-masks/102513/63?u=noobastik these are the rules to control my input and output modules. I need to wait for one ithem (ON OFF) to be executed and only then perform the next change in another Ithem (ON OFF).

JavaScript would work better here with array. But this should be a good start.


var java.util.ArrayList<Boolean> lightStates = newArrayList(false, false, false) // Initialize to 'false' for each light so add as many false as your number of lights very important
var Boolean isProcessing = false

rule "Device in"
when
    Item InOut changed
then
    var BigInteger register = (InOut.state as DecimalType).toBigDecimal.toBigInteger
    gLights.members.forEach[i |
        val Integer nBit = Integer::parseInt(i.name.split("_").get(1)) - 1
        val Boolean targetState = register.testBit(nBit)
        i.postUpdate(if(targetState) ON else OFF)
        lightStates.set(nBit, targetState)
    ]
    isProcessing = false
end

rule "Device out"
when
    Member of gLights received command
then
    if (!isProcessing) {
        isProcessing = true
        var BigInteger register = (InOut.state as DecimalType).toBigDecimal.toBigInteger
        val Integer nBit = Integer::parseInt(triggeringItem.name.split("_").get(1)) - 1
        if(receivedCommand == ON) {
            register = register.setBit(nBit)
        } else {
            register = register.clearBit(nBit)
        }
        InOut.sendCommand(register)
        isProcessing = false
    }
end

rule "Sequential Execution"
when
    Member of gLights changed
then
    if (!isProcessing) {
        isProcessing = true 
        for (var i = 0; i < lightStates.size; i++) {
            val light = gLights.members.findFirst[name | name.endsWith("_" + (i + 1).toString)]
            if (light !== null) {
                val targetState = lightStates.get(i)
                if ((light.state == ON && !targetState) || (light.state == OFF && targetState)) {
                    light.postUpdate(if(targetState) ON else OFF)
                }
            }
        }
        isProcessing = false
    }
end



1 Like

Hmm… I was thinking of adding a pause :slight_smile: I’ll try to figure out the code, thanks for the help!

Instead a pause / wait use a timer to schedule your code and run it later.

There are several examples if you try the search function

How many Items are we talking about here?

If it’s just two, trigger another rule to run once the first Item changes to perform the next task.

If it’s more than two, there’s Thread.sleep() or Timers.

Two I/O modules with 25 connections each.