Actions in Timer not firing

Hi
I have been tinkering with a remote for my home ventilation systyem, attached it to an arduino, added RFM to it so I can now influence the power of the fan (yay!). My regular rule setup to change the settings works fine:

rule "Select FAN level"
    when
        Item FAN_level received command
    then
        switch(receivedCommand) {
            case 0 : postUpdate(itm_node4_fan, "1") // night
            case 1 : postUpdate(itm_node4_fan, "2") // day
            case 2 : postUpdate(itm_node4_fan, "3") // high
            case 3 : postUpdate(itm_node4_fan, "4") // shower
        }
    end

So to take it a step further I am now measuring humidity in the shower and based on humidity levels am switching on the fan like this:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

var Timer timer4 = null
rule "Node 3 Humidity to FAN control"
    when
        Item itm_node3_hum_mqtt received update
    then
        if(itm_node3_hum_mqtt.state > 60 && Day_time.state == ON) {
            if(timer4==null) {
                sendCommand(postUpdate(itm_node4_fan, "4"))
                timer4 = createTimer(now.plusMinutes(60.intValue)) [|  
                    sendCommand(postUpdate(itm_node4_fan, "1"))
                    Thread::sleep(5000)
                    sendCommand(postUpdate(itm_node4_fan, "2"))   
                    timer4 = null
                ]
            }
        }
    end

The trouble is that the rule does get triggered, but only as far as the sendCommand(postUpdate(itm_node4_fan, “4”)) line, it seems the timer never starts up, as the actions within it do not get executed.

Been troubleshooting this for an entire weekend now and thought I’d try the collective intelligence of this board, thoughts? :slight_smile:

thanks!

ok… I’ll answer (part) of my own question: triggered by some other posts here I looked (once again) into the difference between postUpdate and sendCommand and their syntax (https://github.com/openhab/openhab/wiki/Actions

I have been nesting a postUpdate inside a sendCommand, which works, but not quite always. When I remove the sendCommand nesting the above rule does work quite nicely and executes all commands, except the last one after the Thread::sleep(5000). Inputs on that would be appreciated.

When using Thread::sleep inside a lambda (anything between [ ] is a lambda) needs to be put into a try catch clause because in the context of the lambda when it runs there is nothing to catch the InterruptedException and the underlying Java requires all such exceptions be caught.

Designer should have highlighted this as an error.

Hi Rich, thanks for the reply. The lambda and try-catch clause is slightly beyond my java knowledge. After reading up on it here I am still a bit confused, but I’ll keep at it.

thanks!

try{Thread::sleep(5000)}catch(InterruptedException e){ }

Simple as that.

thanks Rich!