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.
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
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’
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
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
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
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
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)