[SOLVED] JSR223: javascript rule working with DateTime and Persistence

Hi,

I struggle using DateTime and Persistence in a rule.
I want to calculate the energy consumption for an hour, today and the last week.

I am using the Libraries from Helmut Lehmeyer.

Does anyone have an example how to use historicState or deltaSince for the PersistenceExtension.js?

I am able to get a DateTime with the actual time
var test = new DateTime(now().toString());

Using this with historicState yields an Error …
todayEnergyConsumption = historicState(totalEnergyConsumption.name, test );

Using a DateTimeType as
var test = new DateTimeType(now().toString());

yields a ClassCastException …
Cannot cast org.eclipse.smarthome.core.library.types.DateTimeType to org.joda.time.base.AbstractInstant

I appreciate any examples, hints, documents etc.

THANKS in advance

Alexander

I figured it out … sometimes a coffee break helps …

JSRule({
name: “Calculate Energy Consumption For Specific Time Intervals”,
description: “Line: “+LINE,
triggers: [
TimerTrigger(”*/10 * * * * ?”) // every Minute
],
execute: function( module, input)
{
logInfo( me + " triggered ");
var totalEnergyConsumption = getItem(“Smart_Meter_Energy_Consumption_Total”);
logInfo ( me + " — Total Energy Consumption in kWh: " + totalEnergyConsumption.state);

    var midnight = new Date().setHours(0,0,0,0);
    var dt = new DateTime(midnight);
    logInfo ( me + " --- dt: " + dt.toString());

    var todayEnergyConsumption = deltaSince(totalEnergyConsumption.getName(), dt );
    logInfo ( me + " --- Today Energy Consumption in kWh: " + todayEnergyConsumption);
}

});

Alexander