Want to pass through value but only if it has changed

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?

[{"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":""}]

Try the filter node

1 Like

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)
1 Like

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:

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

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?

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

4 Likes

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.

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. 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.

1 Like

thx a lot, you guys are great

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