Help for dealing with repetitive values

Hi all
I hope if anyone can help me with this problem. I recently started to experiment with Node-red; however, my progress is currently limited by my lack of experience in JS.

I currently have a sensor that outputs the received signals as numbers; however, I noticed that the sensor would keep repeating the last value without new signals. (it supposed to send 0).

Do you guys know any existing or custom nodes that can send out a default value if the last three incoming payloads are the same value.

Again for simplicity, essentially, what I am trying to create is a node that sends "0" if the last several msgs are the same.

Thanks in advance guys

Hello there, @viixiv

I am not sure if I understand this correctly. Could you maybe use the filter node? It could block messages if they are the same and only allow a message to go through if it's different from the previous one. However, I think it can't count to three :stuck_out_tongue:

Maybe you can share a bit more? What kind of sensor is that?

It might be a little convoluted but you could try this in a function node

let values = context.get("lastvalues") 
if(values === undefined){
    values = context.set("lastvalues",[0,0,0])
// max 2 values, remove the first
// append latest value 

// create unique array
let uniq = Array.from(new Set(values));

// the array will only have 1 element if they are all the same
    // send 0 here
    node.status({text:`Last values: ${values}`})

return null

It keeps track of the last 3 values, within the context of the function node and the received values and checks the uniqueness.

Thank you so much for your suggestions. I think the filter works for now but I have no idea what is coming up in the future that may render it ineffective. Following your suggestion, I have created a new flow with a trigger following the filter node. So far it works great. so far...

and for a bit of context, I am actually doing HCI research through biofeedback. I am working with an EMG sensor that measures muscular impulses. What was giving me issues is that my amplifier seems to repeat the same data once no new signals are detected, at least in node-red.

Thank you sooo much for this. I will go test out and reply with updates :slight_smile: