So I wanted a node to settle down chatty inputs.
My example: I have a dashboard number input using up and down buttons. It cycles through .5 steps to set HVAC temperature. After this input is a function that directly controls my hvac unit.
The problem was if I wanted up the set temperature by 3 I'd need to click the up button 6 times in rapid succession. This was causing the control function to fire 6 times swamping the HVAC controller. The delay or rate limit nodes were no good as they pass the first message then delay drop later messages, but I wanted the last stable number... So I built the below simple function to store the number stream, hold the last number then send it after the time delay.
A little like a debounce filter, but only the last value, not the first.
Let me know any thoughts or improvements:
// This function holds the unput until it settles down.
// It forwards the last number recieved after the cool down.
// Set the hold time
let delaySeconds = 1.5; // change this to whatever you want
let delayMs = delaySeconds * 1000;
// Clear previous timer
let timer = context.get("timer");
if (timer) {
clearTimeout(timer);
}
// store latest number input
context.set("lastMsg", msg);
// The timer
timer = setTimeout(() => {
node.send(context.get("lastMsg")); //output then settled
}, delayMs);
context.set("timer", timer);
return null;

