Trying to calculate seconds between 2 times

Hi,

I’m trying to calculate the seconds between two times so I can calculate from Power(flow) to kW/h

rule "CalculateGeneratedGrid"
// calculating the total generated power from the grid per day
when 
    Item FroniusSymoInverter_GridPower changed
then
    //calculate the time difference
    var timenow = new DateTimeType()
    val DateTimeType endTimeState = (Solar_Feed_LastUpdate.state as DateTimeType)
    
    val Instant programmedStart = timenow.toInstant()
    val Instant programmedEnd = endTimeState.toInstant()

    val Duration programDuration = Duration.between(programmedEnd, programmedStart)

....

but toInstant does’t work nor does

   val Duration programDuration = Duration.between(endTimeState, timenow)

Then the error is: Type mismatch: cannot convert from DateTimeType to Temporal(org.eclipse.xtext.xbase.validation.IssueCodes.incompatible_types)

any idea how to get the seconds of time difference?

DateTimeType isn’t the same as aa ZonedDateTime. So of course there’s no toInstant(). As an aside, a ZonedDateTime is an Instant so you don’t need to convert it to one to use Duration.

val endTimeState = (Solar_Feed_LastUpdate.state as DateTimeType).zonedDateTime
val programDuration = Duration.between(now, endTimeState)

Note, it causes problems when you unnecessarily force the types of variables in Rules DSL. Also note that now already exists in the rule.

I don’t know if Duration is imported by default so you may need to use java.time.Duration or add an import to the top of the file.