Skip to content

Instantly share code, notes, and snippets.

@Stubbs
Created December 22, 2016 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stubbs/3ceb71f0b00f5112adfcf3f00c99c2e4 to your computer and use it in GitHub Desktop.
Save Stubbs/3ceb71f0b00f5112adfcf3f00c99c2e4 to your computer and use it in GitHub Desktop.
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