Rule : Calculate number of seconds between two updates

696 views
Skip to first unread message

Samuel Lorette

unread,
Feb 4, 2015, 4:51:04 AM2/4/15
to ope...@googlegroups.com
Hello,

I'm a bit surprised to spend so much time to find a solution, but I cannot calculate the time between two events (in seconds).

What I would like to do is : when the heating goes on, store the time it was on, and when going of, calculate the amount of seconds it has ON to store this value.


My rule is a bit like that 


import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.joda.time.format.*


rule "Sum Daily Bruleur Time"
when
Item hearMazoutBruleurState_test changed  
then

if(hearMazoutBruleurState_test.state == ON ){

postUpdate(heatMazoutBruleurLastOnState, new DateTimeType())
logInfo("BRULEUR", "enclanchement du bruleur")
}else{
 
                var DateTime now = new DateTime()
var DateTime LastOnState = heatMazoutBruleurLastOnState.state as DateTimeType
logInfo("BRULEUR","LastOnState value " + LastOnState) //This returns the correct datetime

var Number execDuration = Seconds.secondsBetween(now, LastOnState).getSeconds()
//logInfo("BRULEUR", "Le bruleur vient de fonctionner " + execDuration + "secs.)
}

end



The logs :

2015-02-04 10:50:20.011 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Sum Daily Bruleur Time': The name 'Seconds' cannot be resolved to an item or type.



Do you have any idea of how can I do that? Thanks!!

Ben Jones

unread,
Feb 4, 2015, 5:20:14 AM2/4/15
to ope...@googlegroups.com
Try the following syntax;

    var Number execDuration = Seconds::secondsBetween(now, LastOnState).getSeconds()

Samuel Lorette

unread,
Feb 4, 2015, 5:31:26 AM2/4/15
to ope...@googlegroups.com


Le mercredi 4 février 2015 11:20:14 UTC+1, Ben Jones a écrit :
Try the following syntax;

    var Number execDuration = Seconds::secondsBetween(now, LastOnState).getSeconds()

It return the following error : 2015-02-04 11:30:08.391 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Sum Daily Bruleur Time': Could not invoke method: org.joda.time.Seconds.secondsBetween(org.joda.time.ReadableInstant,org.joda.time.ReadableInstant) on instance: null 

druciak

unread,
Feb 4, 2015, 5:47:41 AM2/4/15
to ope...@googlegroups.com
Try this:
var DateTimeType LastOnState = heatMazoutBruleurLastOnState.state as DateTimeType
var Number execDuration = Seconds::secondsBetween(now, new DateTime(LastOnState.calendar.timeInMillis)).getSeconds()


You also don't need to declare now, it is built-in variable.
Krzysztof

Samuel Lorette

unread,
Feb 4, 2015, 6:06:09 AM2/4/15
to ope...@googlegroups.com
Thank you Krzysztof

2015-02-04 12:03:54.399 [INFO ] [g.openhab.model.script.BRULEUR] - PrevOnState vaut 2015-02-04T12:03:47

2015-02-04 12:03:54.408 [INFO ] [g.openhab.model.script.BRULEUR] - Now vaut 2015-02-04T12:03:54.408+01:00

2015-02-04 12:03:54.444 [INFO ] [g.openhab.model.script.BRULEUR] - Le bruleur vient de fonctionner 6 secs.


My rules looks like this  :

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

import org.joda.time.*
import org.joda.time.format.*

rule "Sum Daily Bruleur Time"
when
Item hearMazoutBruleurState_test changed  
then

if(hearMazoutBruleurState_test.state == ON ){
postUpdate(heatMazoutBruleurLastOnState, new DateTimeType())
}else{
var DateTimeType prevOnState = heatMazoutBruleurLastOnState.state as DateTimeType
logInfo("BRULEUR","PrevOnState vaut " + prevOnState)
logInfo("BRULEUR","Now vaut " + now)
var Number execDuration = Seconds::secondsBetween(new DateTime(prevOnState.calendar.timeInMillis), now ).getSeconds()
logInfo("BRULEUR","Le bruleur vient de fonctionner " + execDuration + " secs.")
//Now I have just have to increment this amount of secs to have the total daily. 
}
end

Can you tell me why we use the :: (two semicolons) ? Thanks for the really fast help of this community. 

druciak

unread,
Feb 4, 2015, 6:12:45 AM2/4/15
to ope...@googlegroups.com
Can you tell me why we use the :: (two semicolons) ?
Static methods must be called with two colons, that is rules' syntax.
Krzysztof
Reply all
Reply to author
Forward
0 new messages