Could someone help with a function (or is there a node that can do this)?

I am unfamiliar with writing functions, perhaps someone could help with some knowledge in this area.

I have a power plug that transmits power consumption (Watts) in a cumulative series of outputs:

eg:
power: 288.1

next update:
power: 290.3

etc.

I am wanting to use this node: node-red-contrib-power-monitor but it requires "The node assumes the input value is the average power consumption since the last input."

So, (i am now doubting myself) the power consumption in the above example would be… 2.2

Could anyone offer a suggestion please?

const lastReading = context.get("power");
context.set("power", msg.payload);

if (lastReading === undefined) return null; 

msg.payload = msg.payload - lastReading;
node.status({"text": msg.payload});

return msg;
2 Likes