Comparing two payload at new message but keeping last value if not refreshed

Hi,

First post here.

For the context, I'm receiving a temperature from a KNX node and I would like to compare it with a set value to trigger a Reset Set gate.

What I would like my flow to do is :
If temperature is bigger or equal than the high value set the RS gate to true but if the temperature is below than the low value set to false.

If the set value (high or low ) change the function is triggered but I struggle to keep the last state of my previous payload or function if it wasn't refreshed and the output become wrong or alternate.

Hope this is understandable. Here is a simplified flow without the KNX part:

[{"id":"1ee4050.64e7dfb","type":"function","z":"eeca8943.a75718","name":">=","func":"var local=context.get('data') || {};\n\nlocal.In1=msg.In1;\nlocal.In2=msg.In2;\n\nif (local.In1===undefined)//test exists\n{\n  local.In1=20;\n}\n\nif (local.In2===undefined)//test exists\n{\n  local.In2=18;\n}\n\n\nif (local.In1 >= local.In2) {\n    result = true\n} \nelse {\n    result = false\n}\nmsg = {topic : \">=\", payload : result};\n\ncontext.set('data',local);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":400,"wires":[["b5eed4d1.ed7b28"]]},{"id":"9cadaae7.757b48","type":"function","z":"eeca8943.a75718","name":"<","func":"var local=context.get('data') || {};\n\nlocal.In1=msg.In1;\nlocal.In2=msg.In2;\n\nif (local.In1===undefined)//test exists\n{\n  local.In1=20;\n}\n\nif (local.In2===undefined)//test exists\n{\n  local.In2=15;\n}\n\n\nif (local.In1 < local.In2) {\n    result = true\n} \nelse {\n    result = false\n}\nmsg = {topic : \">=\", payload : result};\n\ncontext.set('data',local);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":500,"wires":[["20c1fbaa.5b1fa4"]]},{"id":"4192a5ba.60707c","type":"function","z":"eeca8943.a75718","name":"RS","func":"var local=context.get('data') || {};\n\nlocal.In1=msg.In1;\nlocal.In2=msg.In2;\n\nif (local.In1===undefined)//test exists\n{\n  local.In1=false;\n}\n\nif (local.In2===undefined)//test exists\n{\n  local.In2=false;\n}\n\n\nif (local.In2 == true) {\n    result = false;\n} else if (local.In1 == true) {\n    result = true;\n} else {\n    result = false;\n}\ncontext.set('data',local);\nmsg = {topic : \"RS\", payload : result};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1150,"y":440,"wires":[["c64cac0e.761cb"]]},{"id":"b5eed4d1.ed7b28","type":"change","z":"eeca8943.a75718","name":"In1","rules":[{"t":"move","p":"payload","pt":"msg","to":"In1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":400,"wires":[["4192a5ba.60707c"]]},{"id":"20c1fbaa.5b1fa4","type":"change","z":"eeca8943.a75718","name":"In2","rules":[{"t":"move","p":"payload","pt":"msg","to":"In2","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":500,"wires":[["4192a5ba.60707c"]]},{"id":"de34e696.58d1f8","type":"change","z":"eeca8943.a75718","name":"In1","rules":[{"t":"move","p":"payload","pt":"msg","to":"In1","tot":"msg"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":380,"wires":[["1ee4050.64e7dfb"]]},{"id":"310651a0.b5617e","type":"change","z":"eeca8943.a75718","name":"In2","rules":[{"t":"move","p":"payload","pt":"msg","to":"In2","tot":"msg"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":420,"wires":[["1ee4050.64e7dfb"]]},{"id":"684d7bad.15cdb4","type":"change","z":"eeca8943.a75718","name":"In1","rules":[{"t":"set","p":"payload","pt":"msg","to":"In1","tot":"msg"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":480,"wires":[["9cadaae7.757b48"]]},{"id":"a91f8395.f0304","type":"change","z":"eeca8943.a75718","name":"In2","rules":[{"t":"set","p":"payload","pt":"msg","to":"In2","tot":"msg"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":520,"wires":[["9cadaae7.757b48"]]},{"id":"c64cac0e.761cb","type":"debug","z":"eeca8943.a75718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1450,"y":440,"wires":[]},{"id":"7247de3a.b7449","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23","payloadType":"num","x":200,"y":340,"wires":[["de34e696.58d1f8","684d7bad.15cdb4"]]},{"id":"a580aa23.718368","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":190,"y":420,"wires":[["310651a0.b5617e"]]},{"id":"fdfdfa54.77cd08","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"num","x":220,"y":520,"wires":[["a91f8395.f0304"]]}]

I would greatly appreciate some help or material that can help me find a solution :slight_smile:

Your flow seems over complicated and flawed.

Are you sending 2 values at once or is it one value to set the threshold(value to compare with) and another is current data ?

Give this a try, it stores both input values and then does a comparison. Current high value is shown on function node.

Changing either input value does another comparison, and outputs true or false

[{"id":"4192a5ba.60707c","type":"function","z":"624963ed82f87a01","name":"RS","func":"var high = flow.get('high') || 20 //set a default \n\nvar value = flow.get('value') || 0 //set a default \n\nlet result = (value >= high ? true : false)\n\nnode.status({text:high});\n\nmsg = {topic : \"RS\", payload : result};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":800,"wires":[["c64cac0e.761cb"]]},{"id":"c64cac0e.761cb","type":"debug","z":"624963ed82f87a01","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":800,"wires":[]},{"id":"7247de3a.b7449","type":"inject","z":"624963ed82f87a01","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23","payloadType":"num","x":450,"y":880,"wires":[["310651a0.b5617e"]]},{"id":"a580aa23.718368","type":"inject","z":"624963ed82f87a01","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":450,"y":760,"wires":[["de34e696.58d1f8"]]},{"id":"fdfdfa54.77cd08","type":"inject","z":"624963ed82f87a01","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"num","x":450,"y":820,"wires":[["310651a0.b5617e"]]},{"id":"de34e696.58d1f8","type":"change","z":"624963ed82f87a01","name":"high","rules":[{"t":"set","p":"high","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":760,"wires":[["4192a5ba.60707c"]]},{"id":"310651a0.b5617e","type":"change","z":"624963ed82f87a01","name":"value","rules":[{"t":"set","p":"value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":840,"wires":[["4192a5ba.60707c"]]},{"id":"23452a062761be1d","type":"inject","z":"624963ed82f87a01","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"num","x":450,"y":720,"wires":[["de34e696.58d1f8"]]}]
1 Like

You're probably right about the over complicated part.

The threshold is another payload.

I tried your solution and it seems to work the way I want. Thank you

I started trying working with flow too, still in a complicated manner but less than my previous flow.

[{"id":"2dfcb015.a5844","type":"change","z":"eeca8943.a75718","name":"","rules":[{"t":"set","p":"thresholdLow","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":180,"wires":[[]]},{"id":"c385e243.8fa3a","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"18","payloadType":"num","x":470,"y":180,"wires":[["2dfcb015.a5844","f43ee3e7.bbbde"]]},{"id":"730c69fd.5b8088","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":470,"y":140,"wires":[["7440758a.1f0c5c","f43ee3e7.bbbde"]]},{"id":"7440758a.1f0c5c","type":"change","z":"eeca8943.a75718","name":"","rules":[{"t":"set","p":"thresholdHigh","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":140,"wires":[[]]},{"id":"b5058f66.93d06","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23","payloadType":"num","x":470,"y":100,"wires":[["38a9c61.7aec83a","f43ee3e7.bbbde"]]},{"id":"38a9c61.7aec83a","type":"change","z":"eeca8943.a75718","name":"","rules":[{"t":"set","p":"current","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":100,"wires":[[]]},{"id":"f43ee3e7.bbbde","type":"function","z":"eeca8943.a75718","name":"logic","func":"var current=flow.get('current') || 25;\nvar tLow=flow.get('thresholdLow') || 18;\nvar tHigh=flow.get('thresholdHigh') || 15;\n\n\nif (current < tLow) {\n    result = false\n} \nelse if (current >= tHigh) {\n    result = true\n} \nelse {\n    result = false\n}\n\nmsg = {topic : \"none\", payload : result};\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":260,"wires":[["9fbe8fde.5271f"]]},{"id":"9fbe8fde.5271f","type":"debug","z":"eeca8943.a75718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":180,"wires":[]},{"id":"50ba7cb.e91af84","type":"inject","z":"eeca8943.a75718","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"25","payloadType":"num","x":440,"y":260,"wires":[["7440758a.1f0c5c","f43ee3e7.bbbde"]]}]

Hope I'll improve my node-red coding with time.

Again thank you for your help @smcgann99 .

1 Like

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