Function: Debounce Function to Output Only the Final Stable Value

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;

You can use a Trigger node configured like this

That will wait for a gap of 1.5 seconds between inputs and then send the latest message received.

2 Likes

Never thought of a trigger node... Good shout...

Whenever a timer feature is required then consider the trigger node, it is an extremely flexible and useful node.

1 Like

for this type of settings i work with standard NR web socket and <input type="range". A slider you drag to the value wanted, and when released it is saved/handled.
No debouncing action needed.

<input type="range" id="rgZB_TS011F_03ModeTime" min="10" max="60" step="10" value="" onchange="input_update('rgZB_TS011F_03ModeTime',this.value)"> <br>

Thanks, but I prefer the precision of the Up/Dn buttons. It was sliders originally.

In fact the number input node has a built in debounce function.

I don't see those options in the dashboard 1 nodes?

You may be right, I moved over to D2 over two years ago.

1 Like