Check if the state's item is the same for X seconds

If the power energy consumption goes down into x-y range for z seconds, then I would like to fire the rule.


rule “play”
when Item b changed
then
if (b.state >=10 && b.state <= 50)
{
if(!b.changedSince(now.minusSeconds(1))),
[|b.sendCommand(OFF)]
}
end

My code is not working. :frowning:
Thanks for pointing me into the right direction.

needs to be

if (!b.changedSince(now.minusSeconds(1)))
b.sendCommand(OFF)

Also, b cannot be of integer type (for state comparison) and OnOffType (in sendCommand) at the same time

Also, you must be sure that you properly persist the b item on everyChange (which doesn’t work for me). Do debug prints to see if what’s stored in persistence database now and X seconds ago really differ.
I wouldn’t use persistence here anyway but global variables.

As the rule gets triggered by

Item b changed

there is no chance that the condition

if( ! b.changedSince(now.minusSeconds(1)) )

will ever get true.