Performing procedures without waiting for the end

Code:

 val procedure = [ String varLamp|
  if varLamp == "test_1") { 
    sendCommand(ZigbeeLamp1, ON)
    Thread::sleep(5000)
    sendCommand(ZigbeeLamp1, OFF)
  }
  if varLamp == "test_2") { 
    sendCommand(ZigbeeLamp2, ON)
    Thread::sleep(5000)
    sendCommand(ZigbeeLamp2, OFF)
  }
  if varLamp == "test_3") { 
    sendCommand(ZigbeeLamp3, ON)
    Thread::sleep(5000)
    sendCommand(ZigbeeLamp3, OFF)
  }
 ]
//---------------------------------------------------
 rule "Button One"
  when
   Item Switch received update
  then
   val variable_lamp1 = procedure.apply("test_1")
   val variable_lamp2 = procedure.apply("test_2")
   val variable_lamp3 = procedure.apply("test_3")      
  end

I am looking for help. In the example above, each procedure is performed after the previous one has finished. Llamas light up one at a time. How to perform the next procedure without waiting for the end of the previous one? Thanks

In the body of each if statement, create a timer that performs the ON-sleep-OFF steps. This also will ensure that the rule doesn’t block for 15+ seconds (which ties up a rule thread for an extended period).

One other thought…

Why don’t you pass the item (as type GenericItem) into the lambda? That way you can get rid of all the if statements. And, you can use the construct varLamp.sendCommand(ON).

So, it would look something like this.

val procedure = [ GenericItem varLamp |
    createTimer(now) [ |
        varLamp.sendCommand(ON)
        Thread::sleep(5000)
        varLamp.sendCommand(OFF)
    ]
 ]

rule "Button One"
when
    Item Switch received update
then
    procedure.apply(ZigbeeLamp1)
    procedure.apply(ZigbeeLamp2)
    procedure.apply(ZigbeeLamp3)      
end

Thank you for your support. I’m trying to use the createTimer procedure.