[SOLVED] Rule: Calc end time from start time and duration

Hi all,
I would like to calculate an end time from a start time and a duration in a rule.
start time and end time are DateTime items.
duration is a Number item.

I have read all the DateTime explanations and tutorials, but somehow still fail.

Here is my code:


DateTime  IrrigationStarted   "Bewässerung letzter Start [%1$td.%1$tm.%1$tY %1$tH:%1$tM]" <time>                  // virtual item, rule setting, mapdb persistence
DateTime  IrrigationEndTime   "Bewässerung Ende [%1$td.%1$tm.%1$tY %1$tH:%1$tM]" <time>                           // virtual item, rule setting, mapdb persistence 
Group:Number:SUM gIrrigation_Times "Total Irrigation Time is [%d mins]"


// does not work: Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2019-06-27T21:40:16.535+02:00
IrrigationEndTime.postUpdate(now.plusMinutes(gIrrigation_Times.state as Number) as DateTimeType)
IrrigationEndTime.postUpdate(new DateTimeType(now.plusMinutes(gIrrigation_Times.state as Number)))
IrrigationEndTime.postUpdate(now.plusMinutes(gIrrigation_Times.state as Number).intValue)

// Does not work: Cannot cast from DateTime to DateTimeType
IrrigationEndTime.postUpdate(now.plusMinutes(gIrrigation_Times.intValue) as DateTimeType) 

Maybe I cannot use .plusMinutes here and have to convert the DateTime item to .toEpochMilli, do the sum and convert back?
Any help is appreciated.

Please have a look at this tutorial. Very helpful.

This was one of the tutorials I already looked at, but I will have a deeper look again.

One important thing to realize is that DateTime and DateTimeType are not the same. You need to convert from one to the other. The other is to realize that plusMinutes requires a primitive int, just a Number. Finally, nothing will work if the state of gIrrigation_Times is UNDEF or NULL.

IrrigationEndTime.postUpdate(now.plusMinutes((gIrrigation_Times.state as Number).intValue).toString)

You were pretty close . You were just missing the toString. You can’t set a DateTimeItem using a DateTime, but it can parse the ISO 8601 formatted date String that DateTime outputs by default.

That works, great, thanks, Rich!

  • Just as addition: After reading the tutorial again, I found another (nearly similar) way:
IrrigationEndTime.postUpdate(new DateTimeType(new DateTime(now.millis + (gIrrigation_Times.state as Number).intValue * 60000).toString))

This works as well.