Hey the title said a lot of what I need ! I need something to remind me when i have to take out the trash … If it was every friday it would be easy but it’s every two week ( on friday )
Is it possible to make a rule to send command to an item every two friday ?
I think the simplest way would be something like this:
rule "trash reminder"
when
Time cron "0 0 7 ? * FRI" //every Friday @ 7 O'clock
then
if (now.weekOfWeekyear()/2 == int(now.weekOfWeekyear()/2)) { //even weeks
//do some stuff
}
if (now.weekOfWeekyear()/2 != int(now.weekOfWeekyear()/2)) { //uneven weeks
//do some stuff
}
end
but be aware that sometimes there is a 53rd week in a year, so sometimes you will have to correct the code.
Another easy way would be to use caldav binding and define an appointment through your preferred calendar…
if trashweek is an item. Maybe you ment something like this?
rule "trash reminder"
when
Time cron "0 0 7 ? * FRI" //every Friday @ 7 O'clock
then
trashweek.postUpdate(if(trashweek.state == ON) OFF else ON) //toggle state once a week
end
But you will have to ensure the state is correct when starting openHAB.
I modified the solution of Udo_Hartmann by calculating with millseconds till 01.01.1970 instead of week number. Replace 1484079952989L (L stands for long, it’s too big for an integer) with the milliseconds timestamp of a day in the past where the event first took place.
rule "trash reminder"
when
Time cron “0 0 7 ? * FRI” //every Friday @ 7 O’clock
then
if((now.getMillis()-1484079952989L)/(8640010007) % 2 == 0) {
logInfo(“Test”, “EVEN WEEK”)
} else {
logInfo(“Test”,“ODD WEEK”)
}
end
Explanation:
Let’s say the litter service came on 1484079952989. Between 1484079952989 and 1484079952989-1+7864001000 (that’s how many milliseconds a week has) the modulo two is zero. Between 1484079952989-1+2786400*1000 it is one. Then again zero and so on.
Only thing to mention: This is not really ODD or EVEN week, because either you do week number calculation like ISO 8601 (week number 1 is the one with the first thursday of the year) or maybe you do it the american way (week number 1 is the one with january 1st).
The former will result in some years with 52 weeks and some years with 53 weeks, the latter will result in "every year has 53 weeks, but some of them aren’t completed."
If a year has 53 weeks, an odd week (53) will be followed by an odd week (1).
However, this is no flaw in question of the “garbage week”
if that’s an integer type to begin with, that makes sense.
You could either convert to a decimal type, or do an integer math trick;
the objective is to determine odd/even, yes?
If ( ((now.getWeekOfWeekyear / 2) * 2) == now.getWeekOfWeekyear ) {
// it's even