This is a pretty awkward way of getting the difference in days between two dates. I’m not 100% sure of the details here, but the necessary conversions between ZonedDateTime and LocalDate because of the use of withTimeAtStartOfDay (which I believe would be atStartOfDay here instead) will get long and complicated. You probably don’t need that part however and will still get the correct value (because of the math::round
) with:
val varRestmuellDatum = Restmuell.getStateAs(DateTimeType).zonedDateTime.withHour(0).withMinute(0).withSecond(0)
logInfo("all.rules", varRestmuellDatum.toString())
val Long varRestmuellDiff = Math::round((varRestmuellDatum.toInstant().toEpochMilli() - ZonedDateTime.now().toInstant().toEpochMilli()) / (86400000.0))
However there are much more elegant ways of getting the difference between two timepoints. This post list some examples:
Note the import and use of the ChronoUnit
to avoid the messy conversions to millis and back and rounding and the .until
method to directly get the amount of time between two datetimes.