Looking for rule to count days and do some actions

Hi everyone :slight_smile:

I try to figure it out by myself but after three days no joy :confused:

  1. I have to figure out how to count 360 days (counter can’t be reset by system shutdown, I can save it in InfluxDB).
  2. After 360 I need push message or switch item with text (reminder about sth)
  3. Finally I have to reset counter and start again.

I best case scenario I want to let user to put data from some kind set point, but right now upper scenario will be fine.

Have a number item called counter persisted influx (everyChange) AND mapdb (restoreOnStartup)

Number counter

rule:

rule "increase day counter"
when
    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
then
    val currentCounter = counter.state as Number
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 360) {
        // DO YOUR STUFF
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end

NOTE: This will skip a day if the server happens to be down at 00:01am

If you have 2 persisted variables, you can have one be day of last change and the other be days, that way if you miss an update you can fix it later, even if say the power is off for a few days.

Ira

My Persistence:

Strategies {
everyMinute : "0 * * * * ?"
everyHour : "0 0 * * * ?"
everyDay : "0 0 0 * * ?"
every2Minutes : "0 */2 * ? * *"
}

Items {
mapdb: strategy = restoreOnStartup
counter,COUNTER01,COUNTER02: strategy = everyChange
SETPOINT,TEMP01: strategy = everyHour
}

My item:

Number SETPOINT "Ustaw temperaturę: [%.1f °C]" <temperature>
Number SETPOINT01
Number counter
Number COUNTER01
Number COUNTER02

My Sitemap:
Text item=counter

Rule from above

//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
        Time cron "0/10 0 0 ? * * *" // Every 10 seconds
then
    val currentCounter = counter.state as Number
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 360) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end

Log:

[el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'counter.rules', using it anyway:
Assignment to final variable

How to view actual state of counter in sitemap?

Change the definition of the item:

Number counter "Counter [%d]"

See:https://www.openhab.org/docs/configuration/items.html#item-definition-and-syntax

Thanks vzorglub :slight_smile:

Still no joy with rule, right now looks like:

rule "increase day counter"
when
//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
         Time cron "0/10 0 0 ? * * *" // Every 10 seconds
then
    val currentCounter = counter.state as Number
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 7) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end

A log give me:

2018-11-17 20:43:09.313 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘counter.rules’, using it anyway:
Assignment to final variable
2018-11-17 20:43:09.376 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘counter.rules’

That is my fault. Sorry. Use var instead of val

rule "increase day counter"
when
//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
         Time cron "0/10 0 0 ? * * *" // Every 10 seconds
then
    var currentCounter = counter.state as Number
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 7) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end

Thanks :slight_smile:

So right now I don’t have any errors in log, but rule give me nothing in sitemap I got “-”.

Also I don’t have anything in influx… My persistence file is ok?

Strategies {
everyMinute : "0 * * * * ?"
everyHour : "0 0 * * * ?"
everyDay : "0 0 0 * * ?"
every2Minutes : "0 */2 * ? * *"
}

Items {
mapdb: strategy = restoreOnStartup
counter,COUNTER01,COUNTER02: strategy = everyChange
SETPOINT,TEMP01: strategy = everyHour
}

Do i need configure sth more with “mapdb”?

Mmmmhhh…

rule "increase day counter"
when
//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
         Time cron "0/10 0 0 ? * * *" // Every 10 seconds
then
    logInfo("Counter State: ", counter.state.toString)
    currentCounter = counter.state as Number
    logInfo("CurrentCounter: ". currentCounter.toString)
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 7) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end

Nothing goes to log :confused:

Also when I try read data in Grafana I don’t have “counter” stat.

Is that the complete rules file?
Try to move the rule to it’s own file.
Anything in the logs when you save it?

This is all, I create single file for that rule. I also restart openhab.

2018-11-17 21:06:36.884 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘counter.rules’
2018-11-17 21:06:39.354 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘counter.rules’
2018-11-17 21:10:46.095 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘counter.rules’

What is value of counter right now?

On sitemap " - "

rule "increase day counter"
when
//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
         Time cron "0/10 0 0 ? * * *" // Every 10 seconds
then
    logInfo("Counter State: ", counter.state.toString)
/*
    currentCounter = counter.state as Number
    logInfo("CurrentCounter: ". currentCounter.toString)
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 7) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
*/
end

Logs?

1 Like

Empty… maybe cron is wrong?

Yes, Duh…
cron "0/10 * * ? * * *"

1 Like

2018-11-17 21:25:50.836 [INFO ] [arthome.model.script.Counter State: ] - NULL
2018-11-17 21:25:50.852 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘increase day counter’: Could not cast NULL to java.lang.Number; line 8, column 22, length 23

Yes, that’s good!!!

rule "increase day counter"
when
//    Time cron "0 1 * ? 0 0 0" // one minute after midnight everyday
         Time cron "0/10 * * ? * * *" // Every 10 seconds
then
    logInfo("Counter State: ", counter.state.toString)

    if (counter.state == NULL) { currentCounter = 0 }     //CHANGED
    else { currentCounter = counter.state as Number }     //CHANGED

    logInfo("CurrentCounter: ". currentCounter.toString)
    currentCounter = currentCounter + 1
    counter.postUpdate(currentCounter)
    if (currentCounter == 7) {
        // DO YOUR STUFF
        V1.sendCommand(ON)
        counter.postUpdate(1) // Or 0 depends when you want to start
    }
end
2018-11-17 21:30:41.064 [INFO ] [arthome.model.script.Counter State: ] - NULL
2018-11-17 21:30:41.073 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'increase day counter': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: counter.rules#|::0.2.0.2.0.1.1.0.0::0::/1)