[SOLVED] Rule that counts a set number of weeks and then resets to the first week

There was a thread about this, I seem to recall, a few weeks ago but I cannot find it.

If I have a rule that counts weeks, starting at at a week 1, up to say week 3, and then resetting to week 1 (and carrying out a different action at the beginning of the week, depending on which of the three weeks it is).

I have a rule that does this but I want to re-write it as it’s got a bit messy and it doesn’t always work.

My question:

Is it better to store and use the week number as a variable in a rule (and write it to a number Item with persistence), or just keep it as a number Item - and change the state (number) in the rule.

I just wondered which is the ‘correct’ way of doing it.

Thanks

You are counting up to three weeks. If you store the week number in a variable that means that your OH would have to remain running and your .rules file untouched for the full three weeks. When OH restarts of the .rules file is reloaded all your variables get reset and you would lose the week number.

If you store the week number in an Item, and configure that Item with persistence and restoreOnStartup, you won’t lose the week count.

As for what you describe, this is actually pretty easy to code. Assuming a number Item:

rule "Week count"
when
    Time "cron 0 1 0 ? * MON *" // Every Monday one minute after midnight
then
    val currWeekCount = WeekNum.state as Number
    val nextWeekCount = ((currWeekCount + 1) % 3) + 1 // counts from 1 to 3 then cycles back to 1
    WeekNum.postUpdate(nextWeekCount)
end

That was the important piece of information that I had overlooked!

I’ve been editing my rules quite a bit recently and this is probably why the week numbers keep changing.

It’s actually for a jobs rota for the kids and it works pretty well.
They can ask Alexa what they need to be doing that week and get a summary of what they need to do.

My oversight does explain the remarks from my kids such as “I swept the yard last week - why am I being asked to do it again this week!”

Thank you for your help.

1 Like

Just a quick thing. Is there any documentation online that explains what the % does in the code below?

I know what it does but would like to see some dcoumentation as it doesn’t appear to be in the Xtend documentation.

Scroll to Operators, Arithmetic Operators
https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html

1 Like

There’s no mention of %n being used to indicate the max. value for the variable in that documentation - which was my point.

It’s in there. Look for the modulo operator.

2 Likes

Got it. Thanks. All clear!

1 Like

I used this in an example and I thought it was working but now I’m not so sure so I created a test rule and for the following line:

I get:

 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change task week': Unknown variable or command '%';

Any ideas? I did try lots of different things.

Thanks

I think I know what is going on. By default, all calculations are stored in a BigDecimal object. There is no % operator defined for BigDecimal. You can probably get it to work by forcing the BigDecimal to become a primitive int.

val nextWeekCount = ((currWeekCount + 1).intValue % 3) + 1 // counts from 1 to 3 then cycles back to 1

If not, BigDecimal has a remainder method.

val nextWeekCount = ((currWeekCount + 1).remainder(3)) + 1

The above works - thanks.

I had already tried assigning nextWeekCount and currWeekCount as int types but that didn’t work.

That’s because the result of the calculation is BigDecimal, regardless of the types of the values that go into the calculation.

I still don’t understand how we’re meant to work out this sort of thing without asking these questions -or or hours of trial and error.

  • this forum and the knowledgeable users are a life-saver! Thank you again.

You’re not. And that’s a big reason why the Rules DSL will become deprecated in favor of the Next Gen Rules Engine which uses more traditional languages.

1 Like