Rules events AND condition

Hi everyone

I want to execute a rule when command ON is received by an item AND ever 5 seconds.
It’s possible ?

I think what you want is an OR condition:

rule "test"
when
    Time cron "0/5 * * * * ?" or
    Item myItem received command ON
then
   // do something
end

This would fire the rule every 5 seconds AND additionally when myItem received a command ON.

If you wand a logical AND, it depends on what you want to do:

Fire the rule every 5 seconds, once myItem received command ON, until myItem received command OFF:

rule "test"
when
    Time cron "0/5 * * * * ?"
then
    if (myItem.state==ON) {
       // do something
    }
end

Fire the rule once when myItem received command ON, but only when seconds can be divided by 5:

rule "test"
when
    Item myItem received command ON
then
    if((now.getSecondOfMinute/5).intValue == now.getSecondOfMinute/5) { //I'm not sure if this works ;)
        // do something
    }
end

I want to begin my rules when received command ON every 5 seconds.
The rule much finish when received command OFF.
So, when received ON AND every 5 seconds until received command OFF.

Maybe you could could create a little flow in Node-Red.
I’m just now starting to use it as a simple rule engine and it proves to be quite well suited for that.

You basically could get the Item State with a “openhab2”-Node, check if the state of the item is “ON” with an “openhab2 get”-node, check if the state is “ON” with a “function”-node and then make it “do something”.

In order to get it to do something every 5 seconds when it is on, just take the output from the “function”-node, feed it into a delay function. This output just goes back to the original “openhab2 get”-node’s input so it requests the current status of the item again, and so on…

You only need to make sure, that your function node only returns something, when the item’s state is on. Otherwise you will create an endless loop (and also trigger your “do something” regardles wether your device is “ON” or “OFF”).

So it’s my second example :slight_smile: The rule will fire every 5 seconds, but the code will only be executed, if (and as long as) the item is ON.

4 Likes

Ok, thank you, it’s THE solution.
Other question :
I want to start a rule when i activate “away mode” and from other events after that.

i think about this :

rule "Away events"
when
    Item switchAway received command ON and
    <other events>
then
    <actions>
end

But, if my switchAway will not be changed, the rule will not run… Am i right ?

Sorry for my english… :smiley:

Correct, these rules need an event to get started. So you would need again a cron-statement as posted by @Udo_Hartmann to get events that trigger the rule.

This might be what you are thinking (there can be no AND for trigger events)

rule " Initial Away events"
when
    Item switchAway changed to ON or
    <other events>
then
    if (switchAway.state == ON) {
        <actions>
    }
end

This does assume sending switchAway the command ON results in switchAway state becoming ON at some time (which OH autoupdate feature usually does make happen, by default).