Help with timers and sleep in rules

openhab2
Tags: #<Tag:0x00007fe056fd2350>

(Shane Grieve) #1

Hi All,
I am struggling with rules with timers I am note sure the best way to handle it. I have one for my pool filter pressure sensor rule it turns the pump off if the pressure drops to low or goes to high. The problem is I need it to wait 30 seconds for the pressure to come up after turning on I have tried. sleep and timers here is what sometime works

import org.openhab.model.script.actions.*
rule “Pool_ Filter”
when
Item pool_filter_preasure changed
then
habpcmd.sendCommand(“SCREEN_ON”)
Thread::sleep(29000)
if (pump.state == ON && pool_filter_preasure.state > 270) {
pushNotification(“Information”, “Filter Preasure High!”)
pump.sendCommand(OFF)
}

else if (pump.state == ON && pool_filter_preasure.state < 170) {
pushNotification(“Information”, “Filter Preasure Low!”)
pump.sendCommand(OFF)
}
end

The other one is a water level sensor I would like it to check 3 times before shutting pump off I have tried timers and setting a switch but still no luck

import org.openhab.model.script.actions.Timer

var Timer delayTimer = null

rule “Pond_Level”

when
Item Pond_Level received update
then
{
if (Pond_L.state==OFF) {
if (Pond_Level.state > 25) {
logInfo(“FILE”, “Pond Low”)
pushNotification(“Information”, “Pond Water Low!”)
pond.sendCommand(OFF)
Pond_L.sendCommand(ON)
}
}
if (delayTimer === null) {
delayTimer = createTimer(now.plusMinutes(60)) [|
logInfo(“FILE”, “Timer Pond expired and setting to OFF”)
Pond_L.sendCommand(OFF)
delayTimer = null
]
}
}
end

Thanks all if you have any ideas it would be great.


(CM6.5 H102) #2

For Pond_Level.state and other items that are numbers try using something similar to this in the rule. Pond_Level.state as Number > 25

The item in first rule habpcmd, what type of item is this?


(Vincent Regaud) #3

Ok, first things first,
Please use the code fences when publishing code:

So your ule is:

import org.openhab.model.script.actions.*
rule “Pool_ Filter”
when
Item pool_filter_preasure changed
then
habpcmd.sendCommand(“SCREEN_ON”)
Thread::sleep(29000)
if (pump.state == ON && pool_filter_pressure.state > 270) {
pushNotification(“Information”, “Filter Preasure High!”)
pump.sendCommand(OFF)
}

else if (pump.state == ON && pool_filter_preasure.state < 170) {
pushNotification(“Information”, “Filter Preasure Low!”)
pump.sendCommand(OFF)
}
end

Remove the import, you don’t need it
Use indents
Thread:sleep has it’s uses but a long one is a very bad idea:

So use a timer:

rule “Pool_ Filter”
when
    Item pool_filter_preasure changed
then
    habpcmd.sendCommand(“SCREEN_ON”)
    createTimer(now.plusSeconds(29), [ |
        if (pump.state == ON && pool_filter_preasure.state > 270) {
            pushNotification(“Information”, “Filter Preasure High!”)
            pump.sendCommand(OFF)
        }
        else if (pump.state == ON && pool_filter_preasure.state < 170) {
            pushNotification(“Information”, “Filter Preasure Low!”)
            pump.sendCommand(OFF)
        }
    ])
end

That’s the first rule


(Vincent Regaud) #4

3 times how often? I put every 60 minutes but you can change that:

var Timer timer = null
var counter = 0

rule “Pond_Level”
when
    Item Pond_Level received update
then
if (Pond_L.state==OFF) {
    if (Pond_Level.state > 25) {
        logInfo(“FILE”, “Pond Low”)
        pushNotification(“Information”, “Pond Water Low!”)
        if (timer === null) {
            timer = createTimer(now, [ |
                if (Pond_Level.state > 25) counter = counter + 1
                else timer = null
            ])
        } else if (counter <= 3) timer.reschedule(now.plusMinutes(60))
        else { // counter reached 3
            pond.sendCommand(OFF)
            Pond_L.sendCommand(ON)
            //timer.cancel()
            timer = null
        }
    } else {
        timer.cancel()
        timer = null
    }
}
end

(Crispin) #5

That could be creating hundreds of timers if the pressure gets chatty.


(Rossko57) #6

Yup, it would smart to manage a single global timer in both rules.