Perhaps I am just to new to this stuff - could you please explain a bit further what you mean?
if I understand you right, xxx.state is the item and not the state of the item?
EDIT: ahhh, got you. Will try it later this evening. Thank you!
No, exactly the other way round. You use Stromzahler_ZahlerstandTimestamp which is the item (the class is DateTimeItem) and try to cast it to DateTimeType. This is not possible. Stromzahler_ZahlerstandTimestamp.state is the state of the item and can be cast to DateTimeType.
Did you try now.toInstant.toEpochMilli like you do for the other one. You could also try using the ZonedDateTime directly (instead of .toInstant.toEpochMilli) and use elapsedTime = Duration.MILLIS.between(now, lastChanged) (not sure if this works).
var lastChangeMilli = (Stromzahler_ZahlerstandTimestamp.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
elapsedTime = Duration.MILLIS.between(now, Stromzahler_ZahlerstandTimestamp.state)
logInfo("kWhPower", "elapsedTime: " + elapsedTime)
and this results in
1. The method elapsedTime(Object) is undefined; line 35, column 1456, length 11
2. The method or field MILLIS is undefined for the type Class<Duration>; line 35, column 1479, length 6
3. The method or field elapsedTime is undefined; line 36, column 1581, length 11
var lastUpdate = (NewItem.state as DateTimeType).zonedDateTime
var elapsed = Duration.between(lastUpdate, now).toMillis
logInfo("test", "elapsed=" + elapsed)
var lastUpdate = (Stromzahler_ZahlerstandTimestamp.state as DateTimeType).zonedDateTime
var elapsed = Duration.between(lastUpdate, now).toMillis
logInfo("test", "elapsed=" + elapsed)
leads to this
2022-11-07 08:00:39.893 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'rulAggregationenStrom' failed: rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested
//variables...
var lastChangeMilli = (Stromzahler_ZahlerstandTimestamp.state as DateTimeType).zonedDateTime
var elapsedTime = Duration.between(lastChangeMilli, now).toMillis
logInfo("test", "elapsed=" + elapsedTime)
logInfo("test", "lastchange=" + lastChangeMilli)
logInfo("kWhPower", "now.millis: " + now.millis + " lastChangeMilli: " + lastChangeMilli)
// Calculate time base
var duration = (now.millis - lastChangeMilli)
var duration2 = Math::round(duration.floatValue())
logInfo("kWhPower", "duration in ms: " + elapsedTime)
// Calculate energy base
var Number energyDiff = ((Stromzahler_Zahlerstand.state as DecimalType) - (Stromzahler_Zahlerstand.previousState(true) as DecimalType)) * 1000
var energyDiff2 = Math::round(energyDiff.floatValue())
logInfo("kWhPower", "energyDiff in Wh: " + energyDiff2)
//Calculate power
var Number powerCalc = energyDiff2 * 3600 * 1000 / elapsedTime
postUpdate(kWhPower, powerCalc)
postUpdate(kWhPowerDT, new DateTimeType())
I changed so much this morning that the error I reported is gone - but there are 5 new errors
1. The method or field millis is undefined for the type ZonedDateTime; line 6, column 294, length 6
2. The method or field millis is undefined for the type ZonedDateTime; line 9, column 390, length 6
==> fixed 3. The method or field power is undefined; line 18, column 816, length 5
==> fixed 4. The method or field kWhPower is undefined; line 20, column 898, length 8
==> fixed 5. The method or field kWhPowerDT is undefined; line 21, column 931, length 10