(SOLVED) How to calculating diff between two DateTime

I’m trying compare and calculate difference between two DateTime items but it doesn’t work. Despite I have tried different approaches I always get some error. :confused: This was my last try:

   val String baseName = sp.name.split("_").get(0)+"_"+sp.name.split("_").get(1)
   var last_update = Heating_lastupdates.allMembers.findFirst[m|m.name.startsWith(baseName)].state as DateTimeType
   var mins_between = last_update.until(now, ChronoUnit.MINUTES)

I got following error:
'until' is not a member of 'org.eclipse.smarthome.core.library.types.DateTimeType'; line 150,

My aim is to check if last_update is older than one hour. Could you please help me?
Thank you,

Convert your DateTimeType to a ZonedDateTime, and then you can use its until method. But you will need to use a ZonedDateTime instead of a Joda DateTime…

var mins_between = last_update.zonedDateTime.until(new DateTimeType().zonedDateTime, ChronoUnit.MINUTES)

Hi 5iver,
Thank you for your quick reply, but I still get the following error:

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Heating - Check Demand': 'zoneedDateTime' is not a member of 'org.eclipse.smarthome.core.library.types.DateTimeType';

last_update value looks ok because when I write that into log I get this for example:

[INFO ] [eclipse.smarthome.model.script.rules] - lastupdate: 2020-10-18T19:25:59.000+0200

This is my code at the moment:

var last_update = Heating_lastupdates.allMembers.findFirst[m|m.name.startsWith(baseName)].state as DateTimeType 
logInfo("rules", "lastupdate: "+last_update)
var mins_between = last_update.zonedDateTime.until(new DateTimeType().zoneedDateTime, ChronoUnit.MINUTES)

There is a typo involving an extra e… it is zonedDateTime. I’ve updated my post.

BTW, you might want to use this…

I got an error message again: :confused:
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Heating - Check Demand': The name 'ChronoUnit' cannot be resolved to an item or type;

Oh… you used it in the original rule, so I thought you already had an import for it. Put this at the top of your rule file…

import java.time.temporal.ChronoUnit

Thank you for your suport! Now it works great! :+1:

1 Like