Skip to content

Instantly share code, notes, and snippets.

@Stubbs Stubbs/Debounce Node
Created Dec 22, 2016

Embed
What would you like to do?
Debounce node for Node Red.
// A little function to debounce messages
// that tend to flip flop a lot
// Uses the message topic as the key.
var debounceTracker = global.get("debounceTracker");
if (!debounceTracker) {
debounceTracker = {};
}
// Get the last value (payload) of the last
// message on this topic.
node.warn(debounceTracker);
var last = debounceTracker[msg.topic];
// If it's a new message, construct a
// fake last instance of it.
if (!last) {
last = {
count: 0,
payload: msg.payload
}
}
node.warn(last);
// Record a match.
if (last.payload && last.payload == msg.payload) {
last.count = last.count + 1;
} else {
last.count = 1;
last.payload = msg.payload;
}
debounceTracker[msg.topic] = last;
global.set("debounceTracker", debounceTracker);
node.status({text: last.count})
if (last.count == 5) {
return msg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.