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.
// 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.
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.
Okay, after updating to OH3, both methods do not work any more.
I have tried many variants, also from https://community.openhab.org/t/datetime-conversion-openhab-3-x/107197, without success.
I get errors like "Could not invoke method: org.openhab.core.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null in home "
Any idea how to fix it?
now is a Java ZonedDateTime. The key is now.toLocalDateTime().toString() for the first line.
For the second line there is no Joda DateTime any more. See #7 for how to get epoch (i.e. the old now.millis) and see #2 for how to create a DateTimeType from epoch.