Some energy meters do not provide instantaneous consumptions / productions such as kW for electricity or m3/h for water, but only a global value indicating the total electricity (kWh) or water (m3) consumed since the meter is installed. Chapter 1 explains how to convert a counter value to instantaneous (derivative computation).
Sometimes on the contrary the available data is only instantaneous power such as kW or m3/h. In this case the gateway can do the counting (integral computation) in an address whose value is kept after a restart, as explained in Chapter 2.
The easiest method is to use the native block covOverTime and a more advanced and customizable approach is to code the routing. The covOverTime block is described in the Routing blocks reference.
With this approach, by editing the route Source value field, the covOverTime (Change Of Value over time) block can be added. The time unit can be selected (millisecond, second, minute, hour, day). For example to convert m3 to m3/h or kWh to kW the unit hour needs to be selected.


It is possible to achieve the same result by designing a custom Javascript function. in the routing function, the options.functionContext is used to store the previous value and its timestamp in order to do the computation. The resulting function (below m3_to_m3_per_hour) can be stored in an internal gateway and re-used on multiple routes.
Note that we don't need to sample the meter value at precise predefined time intervals, because the difference of time is computed inside the function.
Unlike this function, the covOverTime block only keeps a value as the new reference once it has been written to the destination.
function m3_to_m3_per_hour(v,src,dst,options){
var last = options.functionContext.last
var lastDate = options.functionContext.lastDate
// Stores the current value and its timestamp for the future execution
options.functionContext.last = v
options.functionContext.lastDate = Date.now()
// If it is the first value received, we cannot do the difference.
// In this case, the routing function returns nothing and stops here.
if(last === undefined) return
// Date.now returns the time in milliseconds, and here the "dt" variable
// represents the time elapsed between the 2 last values in milliseconds
// and there is 3600000 milliseconds in one hour.
var dt = Date.now() - lastDate
// the difference of value (derivative) divided by the time difference
// returns the rate of change over time.
return (v-last)*( 3600000 / dt)
}
The following routing function can be used to integrate the instantaneous value over time.
The function adds to the current value of the destination address the mean of the last two values multiplied by the elapsed time in hours (trapezoidal rule). Use an Internal address as destination and set its Store value to always, otherwise the counter starts again from 0 after a reboot. The first value received after a restart only initialises the function.
function m3_per_hour_to_m3(v,source,destination,options){
var last = options.functionContext.last
var lastDate = options.functionContext.lastDate
options.functionContext.last = v
options.functionContext.lastDate = Date.now()
// If it is the first value received, we cannot do the difference.
// In this case, the routing function returns nothing and stops here.
if(last === undefined) return
var dtHours = (Date.now() - lastDate) / 3600000
return (destination.value || 0 ) + (( last + v ) / 2)*dtHours
}
Example of a route

It is important to store the value of the counter, in case of a reboot.
