# Want to pass through value but only if it has changed

**URL:** https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475
**Category:** General
**Created:** [17 October 2024 21:26 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475 "2024-10-17T21:26:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)
#### Post date: [17 October 2024 21:26 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/1 "2024-10-17T21:26:38Z")

</div>

Hi,

I have a question. Maybe a noob question but I am not able to solve the problem by myself.

I got a value via mqtt in my node-red flow every second. I want to publish it again but not every time I receive one. I only want to pass on/publish the value if it has changed by at least 50.  
How can I achieve this goal?

```auto
[{"id":"2584687fee6166c8","type":"tab","label":"Huawei input","disabled":false,"info":"","env":[]},{"id":"97e7328d5072a82a","type":"mqtt in","z":"2584687fee6166c8","name":"electricity meter","topic":"sensors/display/solar","qos":"0","datatype":"json","broker":"f839540e.7b7858","nl":false,"rap":false,"inputs":0,"x":420,"y":400,"wires":[["ebbd2f03875511e3","29c733b699d3db05"]]},{"id":"29c733b699d3db05","type":"debug","z":"2584687fee6166c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":500,"wires":[]},{"id":"ebbd2f03875511e3","type":"mqtt out","z":"2584687fee6166c8","name":"publish to L1L2L3","topic":"Huawei/Huawei_d37c92/L1L2L3","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"f839540e.7b7858","x":890,"y":400,"wires":[]},{"id":"f839540e.7b7858","type":"mqtt-broker","name":"","broker":"192.168.178.44","port":"1883","clientid":"clientid","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

 ![Unbenannt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40f4f6b6e06f2ee43277b406d08979d45308133b.png)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [17 October 2024 21:42 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/2 "2024-10-17T21:42:01Z")

</div>

Try the filter node

---

<div class="post-metadata">

### Author: ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)
#### Post date: [18 October 2024 01:58 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/3 "2024-10-18T01:58:17Z")

</div>

As @dceejay mentioned you can use a "Filter-Node" which will only pass the data if your data has changed. It fill not let you specify a threshold of 50 (or so)

to handle with threshold you could

- define a FLOW variable (with a high init value)
- either write a function node to compare the new value from your electricity meter or use the "If-Node" to compare the new and old values (plus any threshold you want)
- save new electricity value in a FLOW variable (to be compared with next new value)

---

<div class="post-metadata">

### Author: ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)
#### Post date: [18 October 2024 06:25 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/4 "2024-10-18T06:25:55Z")

</div>

Hello, thank you already!  
I have found an example that shows me the difference to the direct predecessor value. but that is not yet what I need. I may have expressed myself somewhat incorrectly. I'll try again.  
For example, I have a start value:  
200  
now the input changes every second...  
210  
190  
192  
213  
nothing should happen here.  
Only when, for example, 150 (or 250) is reached should 150 (or 250) be forwarded as a mqtt message.  
The new reference value should then be 150. And so on.  
Unfortunately, I'm not good at programming and I'm jumping from example to example. Perhaps someone here has a suitable example of a function that I can adapt.  
Thank you very much!

my little progress:

```auto
var oldvalue = flow.get("actualvalue") || 0;
flow.set("actualvalue", msg.payload);
msg.actualvalue = oldvalue;
msg.payload = msg.payload - msg.actualvalue;
return msg;

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [18 October 2024 06:45 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/5 "2024-10-18T06:45:28Z")

</div>

> [@trombose009](#):
>
> Unfortunately, I'm not good at programming

Luckily for you it's not necessary to try and write a javascript function for this.

Did you try the filter node which @dceejay pointed you to?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b2320809f8ac378f933d04bd867f0ec51c17704.png)

Set up some inject nodes 200, 190, 258, ...... and try it. Does it work?

---

<div class="post-metadata">

### Author: ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)
#### Post date: [18 October 2024 09:24 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/6 "2024-10-18T09:24:35Z")

</div>

you are right, sorry, i didn't try it until now.  
It is working!  
An additional question:  
how can i make it so that if values above 1000 are received, a mqtt message is not sent again even if there are changes (e.g. 1200 to \>1250). So that the controller only adjusts the value up to 1000 and not above 1000, only below 1000 again.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [18 October 2024 09:25 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/7 "2024-10-18T09:25:48Z")

</div>

Just add a switch node before the MQTT OUT. Check that payload is \<= 1000

* * *

As someone new to Node-RED, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

### Author: ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)
#### Post date: [18 October 2024 10:31 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/8 "2024-10-18T10:31:20Z")

</div>

thx a lot, you guys are great

---

<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: [1 November 2024 10:31 UTC](https://discourse.nodered.org/t/want-to-pass-through-value-but-only-if-it-has-changed/92475/9 "2024-11-01T10:31:37Z")

</div>

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