[SOLVED] How to create a rule with check of item state in period of time

Hello,

i want to create a rule and need some help at this point now.

what i want to do:
i got two switch items and i want to fire the rule if item2 changed to on, but only if item1 changed to on, too, in a period of time before item2 (for excample 10 minutes)

usually i would do something like this:

when
item2 changed to on
then
if item1.state == ON (for max. 10 minutes) …

so how to insert the limit of 10 minutes?

thanks for help!
Alex

What about, the actual state of item2?

the actual state of item2 is off. i want take the change to ON as trigger for the rule.

I would use the item1 change to ON as trigger and so the question is, what about iem2 has changed to ON and OFF in the last 10 minutes.

There are two simple approaches.

  1. Set a global variable with the time stamp when item1 changes to ON and check that variable in the Rule triggered by item2.
var item1Changed = now.minusDays(1) // initialize to sometimes in the past

rule "item 1 changed to ON"
when
    Item item1 changed to ON
then
    item1Changed = now
end

rule "item2 changed to ON"
when
    Item item2 changed to ON
then
    if(item1.state == ON && item1Changed.isAfter(now.minusMinutes(10)) {
        ...
    }
end
  1. Set up persistence on item1 and use lastUpdate.
rule "item2 changed to ON"
when
    Item item2 changed to ON
then
    if(item1.state == ON && item1.lastUpdate.isBefore(now.minusMinutes(10).millis){
        ...
    }
end
1 Like

thanks for your replies and your help! :slight_smile:

I am trying to do this but for some reason, I can’t get either option to work is there a different way you have to do this in the 2.5 version of openhab?

It should work with the current version of OH. There is a multitude of possibilities for “can’t get it to work.”

It’s probably be best to open a new thread, post what you’ve tried and post relevant logs and other configs (e.g. Item definitions).