Set the Timer in Robonect for Example every x day

If someone is interested :wink:

The rule triggers when the mower get´s into the dock and then the timer is set new…
You only need a mowerTimerset item to set how often the timer should run (every second day mowerTimerset == 2)

  rule "Timer setzen"
when
    Item mowerStatus changed from 3 to 4
then
    val wochentag = now.getDayOfWeek.toString
    if(mowerTimerset.state == 1){
            sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=1&we=1&th=1&fr=1&sa=0&su=0&enable=1")
            sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
    }else if (mowerTimerset.state == 2){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=1&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=1&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=1&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=1&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=1&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=1&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
            }
    }else if (mowerTimerset.state == 3){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=1&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=1&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=10&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
            }
    }else if (mowerTimerset.state == 4){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }
    }else if (mowerTimerset.state == 5){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }
    }else if (mowerTimerset.state == 6){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=1&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=1&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
            }
    }else if (mowerTimerset.state == 7){
            if(wochentag == "1"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "2"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=1&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "3"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=1&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "4"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=1&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "5"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=1&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
            }else if (wochentag == "6"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=0&enable=1")
            }else if (wochentag == "7"){
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=0&enable=1")
                    sendHttpPostRequest("http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa=0&su=1&enable=1")
            }
    }

end

I recommend applying Design Pattern: How to Structure a Rule. And if I’m looking at this right, you might be able to If for some reason the URL changes or you get some other API this Rule is going to be a huge pain to edit. At a minimum, store the the base URL in a variable and reuse that.

val baseURL = "http://192.168.xxx.xx/json?cmd=timer&"
val baseURL1 = baseURL + "timer=1&start=07:00&end=11:30&"
val baseURL2 = baseURL + "timer=2&start=05:30&end=10:00&"

if(mowerTimerSet.state == 1) {
    sendHttpPostRequest(baseURL1+"mo=1&tu=1&we=1&th=1&fr=1&sa=0&su=0&enable=1")
    sendHttpPostRequest(baseURL2+"mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
}

...

Look for patterns on the on/off states. Maybe changing the order that you check for things (e.g. test wochentag first and then check mowerTimerset.state) might result in fewer lines of code. Sometimes setting it out in a table can show patterns that are not otherwise apparent.

Consider moving the calls to sendHttpPostRequest to the end so if you need to change the action at some point (hint hint when HTTP 2 binding comes out) you only have two lines to change instead of the dozens you have here.

val baseURL = "http://192.168.xxx.xx/json?cmd=timer&"
var baseURL1 = baseURL + "timer=1&start=07:00&end=11:30&"
var baseURL2 = baseURL + "timer=2&start=05:30&end=10:00&"

if(mowerTimerSet.state == 1) {
    baseURL1 = baseURL1+"mo=1&tu=1&we=1&th=1&fr=1&sa=0&su=0&enable=1")
    baseURL2 = baseURL2+"mo=0&tu=0&we=0&th=0&fr=0&sa=1&su=1&enable=1")
}

...
sendHttpPostRequest(baseURL1)
sendHttoPostRequest(baseURL2)

If you use the String format method you can potentially make this even easier to read.

val baseURL = "http://192.168.xxx.xx/json?cmd=timer&timer={}&start={}&end={}&mo={}&tu={}&we={}&th={}&fr={}&sa={}&su={}&enable=1"
var url1 = ""
var url2 = ""
if(mowerTimerSet.state == 1) {
    //                            Timer  start     end    mo   tu   we   th   fr   sa   su
    url1 = String::format(baseURL, "1", "07:00", "11:30", "1", "1", "1", "1", "1", "0", "0")
    url2 = String::format(baseURL, "2", "05:30", "10:00", "0", "0", "0", "0", "0", "1", "1")
}
...
sendHttpPostRequest(url1)
sendHttpPostRequest(url2)

I’m pretty sure that the whole url can be calculated with something like this (just a rough idea):

val int[] x = newIntArrayOfSize(7)       // 7 days
var i = wochentag                        // start
while((i+=stepsize) < wochentag + 8) {   // step through days 
    var j = 0                            // start from monday
	while((j+=1) < 7) {                  // for each day of week
	   x(j) = if(j-1=i%7) 1 else 0       // if current day matches -> 1
	}
}
// build urls:
url1 -> mo=x(0),tu=x(1),we=x(2),th=x(3),fr=x(4) timer=1
url2 -> sa=x(5),su=x(6) timer = 2

But I don’t get the whole reason of calculating more than the next start :wink:

1 Like

Thanks for your hints :+1:

@Udo_Hartmann What means the += I didn´t see this before …

I don’t know if this rule will work as intended :wink: but at least VSCode gives no warnings:

rule "set timer"
when
    Item mowerStatus changed from 3 to 4
then
    val ip = "192.168.xxx.xx"                                                                                            // set this to correct IP!

    val baseURL = "http://{}/json?cmd=timer&timer={}&start={}&end={}&mo={}&tu={}&we={}&th={}&fr={}&sa={}&su={}&enable=1"

    if(!(mowerTimerset.state instanceof Number)) {                                                                       // some simple tests to avoid errors
        logWarn("setTimer", "mowerTimerset.state not of type Number! ({})",mowerTimerset.state)
        return;
    }
    if((mowerTimerset.state as Number) > 7 || (mowerTimerset.state as Number) < 1 ) {                                    // some simple tests to avoid errors
        logWarn("setTimer", "mowerTimerset.state illegal Number! ({})",mowerTimerset.state)
        return;
    }

    val int[] x = newIntArrayOfSize(7)                                                                                   // initialize 7 days
    var Integer j = 0                                                                                                    // start from sunday
    while((j = j + 1) < 7) {                                                                                             // for each day of week
        x.set(j,0)                                                                                                       // initialize -> 0
        var Integer i = now.getDayOfWeek                                                                                 // start
        while((i = i + (mowerTimerset.state as Number).intValue) < now.getDayOfWeek + 8) {                               // step through days 
            if(j == i % 7) x.set(j,1)                                                                                    // if current day matches -> 1
        }
    }
    // build urls:
    val url1 = String::format(baseURL, ip, "1", "07:00", "11:30", x.get(1), x.get(2), x.get(3), x.get(4), x.get(5), "0", "0")
    val url2 = String::format(baseURL, ip, "2", "05:30", "10:00", "0", "0", "0", "0", "0", x.get(6), x.get(0))

    logInfo("setTimer","url1: {}",url1)
    logInfo("setTimer","url2: {}",url2)
    sendHttpPostRequest(url1)
    sendHttpPostRequest(url2)
end

i+=1 => same as i = i + 1 I’m not sure if this shortcut is allowed in a while() context, at least this is ok in common rules.

EDIT: changed the rule slightly for a more readable code :wink:

Thanks I will check this…

My intention not only to set the next timer is for security reasons :wink:
If I am on vacation and openhab stops working (but it never stops the last 3 years :rofl:) the timers are set for one week and not only one for a week :wink:

1 Like

@Udo_Hartmann When I try your code, I get an error:

   -23 10:49:25.802 [INFO ] [ipse.smarthome.model.script.setTimer] - url1: http://{}/json?cmd=timer&timer={}&start={}&end={}&mo={}&tu={}&we={}&th={}&fr={}&sa={}&su={}&enable=1&save=1                                                               
-23 10:49:25.803 [INFO ] [ipse.smarthome.model.script.setTimer] - url2: http://{}/json?cmd=timer&timer={}&start={}&end={}&mo={}&tu={}&we={}&th={}&fr={}&sa={}&su={}&enable=1&save=1                                                               
-23 10:49:25.804 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'set timer': Illegal character in authority at index 7: http://{}/json?cmd=timer&timer={}&start={}&end={}&mo={}&tu={}&we={}&th={}&fr={}&sa={}&su={}&enable=1&save=1        

Any ideas ?

Well, obviously String::format() did not do it’s job.

As an ugly workaround, try this:

// build urls:
val url1 = "http://"+ip+"/json?cmd=timer&timer=1&start=07:00&end=11:30&mo="+x.get(1).toString+"&tu="+ x.get(2).toString+"&we="+x.get(3).toString+"&th="+x.get(4).toString+"&fr="+x.get(5).toString+"&sa=0&su=0&enable=1")
val url2 = "http://"+ip+"/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa="+x.get(6).toString+"&su="+x.get(0).toString+"&enable=1")

Thanks, but only the sunday doesn´t work…I think it must be

su="+x.get(7)

and not

su="+x.get(0)

But wenn I change it, I get:

2019-08-23 16:08:53.612 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'set timer': 7

And what does this mean ?

That’s the modulo operator, sometimes called remainder. Divide by 7 and return the remainder. For example:

21 % 7 = 0
22 % 7 = 1
27 % 7 = 6
28 % 7 = 0
1 Like

The array x is defined with a size of 7, so it’s 0 to 6.

.getDayOfWeek on the other hand is Monday (1) to Sunday (7)

So, in theory, Sunday should stay in x.get(0).

I have no idea why this part fails. Maybe we have to do some logging :slight_smile:

[…]

while((j = j + 1) < 7) {                                                                                             // for each day of week
        x.set(j,0)                                                                                                       // initialize -> 0
        var Integer i = now.getDayOfWeek                                                                                 // start
        while((i = i + (mowerTimerset.state as Number).intValue) < now.getDayOfWeek + 8) {                               // step through days 

            logInfo("setTimer","j: {} i: {} i%7: {}",j,i,i%7) // add this line to the code

            if(j == i % 7) x.set(j,1)                                                                                    // if current day matches -> 1
        }
    }

[…]

I found that the modulo operator sometimes is a bit tricky, so this as a first attempt…

2019-08-24 17:22:22.060 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.062 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.065 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.067 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.070 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.073 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 12 i%7: 5                                                                                   
2019-08-24 17:22:22.075 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 1 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.079 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.082 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.084 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.087 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.090 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.092 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 12 i%7: 5                                                                                   
2019-08-24 17:22:22.094 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 2 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.099 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.101 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.104 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.106 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.109 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.111 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 12 i%7: 5                                                                                   
2019-08-24 17:22:22.114 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 3 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.118 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.120 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.123 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.126 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.128 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.131 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 12 i%7: 5                                                                                   
2019-08-24 17:22:22.134 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 4 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.138 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.140 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.143 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.145 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.148 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.150 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 12 i%7: 5              
2019-08-24 17:22:22.153 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 5 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.157 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 7 i%7: 0                                                                                    
2019-08-24 17:22:22.160 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 8 i%7: 1                                                                                    
2019-08-24 17:22:22.163 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 9 i%7: 2                                                                                    
2019-08-24 17:22:22.165 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 10 i%7: 3                                                                                   
2019-08-24 17:22:22.168 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 11 i%7: 4                                                                                   
2019-08-24 17:22:22.171 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 12 i%7: 5                                                                                   
2019-08-24 17:22:22.174 [INFO ] [ipse.smarthome.model.script.setTimer] - j: 6 i: 13 i%7: 6                                                                                   
2019-08-24 17:22:22.180 [INFO ] [ipse.smarthome.model.script.setTimer] - url1: http://192.168.xxx.xx/json?cmd=timer&timer=1&start=07:00&end=11:30&mo=1&tu=1&we=1&th=1&fr=1&sa
2019-08-24 17:22:22.180 [INFO ] [ipse.smarthome.model.script.setTimer] - url2: http://192.168.xxx.xx/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa

The rule didnt get the j = 0, so I changed this

var Integer j = -1

and it seems to work. :grinning:
Or is this the wrong wäy ?

jepp, that’s right. Please also change

var Integer i = now.getDayOfWeek

to

var Integer i = now.getDayOfWeek - (mowerTimerset.state as Number).intValue)

Why ? With

var Integer i = now.getDayOfWeek

it works…

Here is my complete rule:

rule "set timer"
when
    Item mowerStatus changed from 3 to 4 or
    Item mowerTimerset changed
then
    val ip = "192.168.xxx.xx"                                                                                            // set this to correct IP!
    if(!(mowerTimerset.state instanceof Number)) {                                                                       // some simple tests to avoid errors
        logWarn("setTimer", "mowerTimerset.state not of type Number! ({})",mowerTimerset.state)
        return;
    }
    if((mowerTimerset.state as Number) > 7 || (mowerTimerset.state as Number) < 1 ) {                                    // some simple tests to avoid errors
        logWarn("setTimer", "mowerTimerset.state illegal Number! ({})",mowerTimerset.state)
        return;
    }
    val int[] x = newIntArrayOfSize(7)                                                                                   // initialize 7 days
    var Integer j = -1                                                                                                    // start from sunday
    while((j = j + 1) < 7) {                                                                                             // for each day of week
        x.set(j,0)                                                                                                       // initialize -> 0
        var Integer i = now.getDayOfWeek                                                                                 // start
//      var Integer i = (now.getDayOfWeek - (mowerTimerset.state as Number).intValue)                                             // start
        while((i = i + (mowerTimerset.state as Number).intValue) < now.getDayOfWeek + 8) {                               // step through days
                //logInfo("setTimer","j: {} i: {} i%7: {}",j,i,i%7)
            if ((j == i % 7) || now.getDayOfWeek == j) x.set(j,1)                                                                                    // if current day matches -> 1
        }
    }
        val url1 = "http://"+ip+"/json?cmd=timer&timer=1&start=07:00&end=11:30&mo="+x.get(1).toString+"&tu="+ x.get(2).toString+"&we="+x.get(3).toString+"&th="+x.get(4).toString+"&fr="+x.get(5).toString+"&sa=0&su=0&enable=1&save=1"
        val url2 = "http://"+ip+"/json?cmd=timer&timer=2&start=05:30&end=10:00&mo=0&tu=0&we=0&th=0&fr=0&sa="+x.get(6).toString+"&su="+x.get(0).toString+"&enable=1&save=1"
                logInfo("setTimer",url1)
                logInfo("setTimer",url2)
    sendHttpPostRequest(url1)
    sendHttpPostRequest(url2)
end

Well, when it’s working as it should, then forget about it :wink: I thought the timer should start with the current day.