# Help for dealing with repetitive values

**URL:** https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922
**Category:** General
**Created:** [6 October 2021 14:04 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922 "2021-10-06T14:04:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![viixiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viixiv/32/49074_2.png) [@viixiv](https://discourse.nodered.org/u/viixiv)
#### Post date: [6 October 2021 14:04 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/1 "2021-10-06T14:04:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [6 October 2021 14:21 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/2 "2021-10-06T14:21:20Z")

</div>

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 😛

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

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [6 October 2021 16:57 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/3 "2021-10-06T16:57:22Z")

</div>

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

```auto
let values = context.get("lastvalues") 
if(values === undefined){
    values = context.set("lastvalues",[0,0,0])
}
// max 2 values, remove the first
if(values.length>2){
    values.shift()
}
// append latest value 
values.push(msg.payload)

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

// the array will only have 1 element if they are all the same
if(uniq.length==1){
    // send 0 here
    node.send({payload:"UNIQUE"})
    node.status({text:"Uniq!"})
}else{
    node.send(msg)
    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.

---

<div class="post-metadata">

### Author: ![viixiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viixiv/32/49074_2.png) [@viixiv](https://discourse.nodered.org/u/viixiv)
#### Post date: [7 October 2021 16:39 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/4 "2021-10-07T16:39:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![viixiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viixiv/32/49074_2.png) [@viixiv](https://discourse.nodered.org/u/viixiv)
#### Post date: [7 October 2021 16:40 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/5 "2021-10-07T16:40:09Z")

</div>

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

cheers

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 December 2021 16:40 UTC](https://discourse.nodered.org/t/help-for-dealing-with-repetitive-values/51922/6 "2021-12-06T16:40:30Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
