How to get hold of the rule trigger time?


I’m using a rule to read the energy meter electric car charging station through Modbus. That gives me the total energy loaded into the car. I’m interested in the charge rate → Energy per hour. For that I do:

	Item ModbusDataEnergyL1 changed
	var Number ChargeRate
	val oldS = previousState as Number
	val newS = newState as Number
	ChargeRate = (newS - oldS) * 60 / 1000

Now the calculation depends on the time between 2 trigger events. The Modbus Poller is set to 60sec, so that is where the 60 comes from. Yet sometimes the Modbus reading failed (communication error), so the calculation is wrong. I want to replace the 60 in the equation with the time between old and new trigger event, similar to what I do with the previousState and newState implicit variables for the meter value.

Can we get implicit variables for the trigger time too?
Or can I extract the timeBetween somehow from the Item state?


Yes, you don’t really want to be relying on timekeeping like that.

The closest to that would be the datetime “now”.

You’d want to store “this” now trigger time in an Item or global variable, to become “last” trigger time at next rule execution.

There are other ways to do this kind of thing in persistence services, but sometimes just calculating it yourself is simpler.
(I’m thinking the deltaSince function here)