In some applications, the difference between two measured values should be normalised to a certain time unit, e.g. to monitor the power consumption per hour. It should also be taken into account that a data packet may be lost due to poor signal quality, i.e. the time stamp of both measured values should be taken into account.
Design and implementation
Create a new data state of the type “Virtual device aggregation” in the desired virtual device or digital twin. Select the desired data state for the aggregation (in the example shown below, “kwh”). Store the following in the transformer:
module.exports = (data, lastData, meta) => {
const currentValue = _.get(data, 'kwh');
const currentValueTime = _.get(data, 'kwh_time');
const deltaTimeMinutes = _.get(lastData, 'value.timestamp')
? ((new Date(currentValueTime) - new Date(lastData.value.timestamp)) / 60000)
: 1;
const delta = _.get(lastData, 'value.timestamp') ? (currentValue - lastData.value.orgValue) : 0;
const deltaPerHour = delta * (60 / deltaTimeMinutes);
return {timestamp: currentValueTime, orgValue: currentValue, deltaPerHour};