A while back I posted a
similar solution for a heating sensor, and I think it would work for you as well (minus the time conversions/power calculations)...
As Julian suggests, an accumulator like this will require storing the running total
somewhere that it will be remembered. You can use the node's context (if this is the only logic that needs to access it), or the flow (only nodes in this flow) or global (all nodes) context. I would also set a variable in the first line that inits the conversion factor for the calculations -- not required, but it is a bit more self-documenting than embedding it in the formula. So, assuming that each msg.payload contains the number of revolutions since the last check, you can do something like this (untested):
var multiplier = 25.4; // e.g. mm/rev
var totalizer = flow.get("mmTotalDistance") || 0; // initial value
if (!isNaN(msg.payload)) {
totalizer += (msg.payload * multiplier); // increment the counter
context.set("mmTotalDistance", totalizer); // save it back in context
}
// if you want to pass the new total along the flow, update the payload:
msg.payload = totalizer;
return msg;
The main problem with context is that it does not persist across restarts. So a better alternative (if you need to save it) would be to send the output (new total) to an MQTT Out node, with a custom topic and the Retain message flag set to true. Wire up an MQTT In node to the same topic, to ensure that the last value published to that topic is always injected into your flow at startup, and send that "startup" value to a Change node that stores it in flow's context.