If number increases or decreases

I have a flow that outputs a number that periodically changes. I want a node that states whether the number increased or decreased. ideally, I want a node that either:

a) outputs a msg stating the number either increased or decreased. or,
b) has 2 outputs, one for number decreased, other for number increased

Does anyone know how this can be achieved?

TIA

You would need to store the previous value and then compare to new value
Here is an example using a function node.

[{"id":"a6ce57e9.6689e8","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.2","payloadType":"num","x":100,"y":1060,"wires":[["3fbc9b4c.cbba54"]]},{"id":"3fbc9b4c.cbba54","type":"function","z":"c791cbc0.84f648","name":"","func":"let prev = context.get(\"previous\") || 0;\nif( prev > msg.payload){\n    msg.change = \"decrease\";\n}else if(prev < msg.payload){\n    msg.change = \"increase\";\n}else{\n    msg.change = \"unchanged\";\n}\ncontext.set(\"previous\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1120,"wires":[["e8823236e6e79e37"]]},{"id":"a2eef470.16b008","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.4","payloadType":"num","x":100,"y":1100,"wires":[["3fbc9b4c.cbba54"]]},{"id":"8029102f.62f728","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.6","payloadType":"num","x":100,"y":1140,"wires":[["3fbc9b4c.cbba54"]]},{"id":"e8823236e6e79e37","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":1100,"wires":[]},{"id":"1d5fa17768faa0ad","type":"function","z":"c791cbc0.84f648","name":"","func":"//create a look up table for sensors required and chart line name\nconst sensors = {\"201201/tempUoTwoBalco\": \"supplyTemp\", \n    \"201201/tempUoThreeBalco\": \"returnTemp\"};\n//set the payload to data    \nlet data = msg.payload.replace(\"NaN\", \"null\");\ntry {\n  data = JSON.parse(data)\n}\ncatch(err) {\n  data ={};\n}\n// set output to null\nlet temps = {};\nfor(const element of Object.values(sensors)){\n    temps[element] = null;\n}\n// loop through data getting keys\nfor(const key1 in sensors){\n    if( data[key1] ){ // checkif key in sensor lookup\n        temps[sensors[key1]] = {};  //initialise object\n        temps[sensors[key1]].payload = data[key1].value; // add payload\n        temps[sensors[key1]].topic = sensors[key1]; // add topic\n    }\n}\n// create return array\ntemps = Object.values(temps);\nreturn [temps];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":800,"wires":[["e8823236e6e79e37"]]},{"id":"f86aa943.42e648","type":"http request","z":"c791cbc0.84f648","name":"","method":"GET","ret":"txt","paytoqs":"body","url":"","tls":"","persist":true,"proxy":"","authType":"","x":480,"y":1040,"wires":[["e8823236e6e79e37"]]},{"id":"fb2e7c30.1a254","type":"html","z":"c791cbc0.84f648","name":"","property":"payload","outproperty":"payload","tag":"input","ret":"attr","as":"single","x":500,"y":960,"wires":[["7a1f9c4d.b6f23c","e8823236e6e79e37"]]},{"id":"f2a105a1.b216","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{    \"201201/tempUoTwoBalco\": {        \"value\": 63.1199951171875,        \"route\": \"/vui/platforms/benshome/devices/201201/tempUoTwoBalco\",        \"writable\": false    },    \"201201/tempUoThreeBalco\": {        \"value\": 74.12999725341797,        \"route\": \"/vui/platforms/benshome/devices/201201/tempUoThreeBalco\",        \"writable\": false    },    \"201201/Oat\": {        \"value\": 89.99999237060547,        \"route\": \"/vui/platforms/benshome/devices/201201/Oat\",        \"writable\": false    },    \"201201/RmTmpSpt\": {        \"value\": 79.99999237060547,        \"route\": \"/vui/platforms/benshome/devices/201201/RmTmpSpt\",        \"writable\": true    },    \"201201/RmTmp\": {        \"value\": NaN,        \"route\": \"/vui/platforms/benshome/devices/201201/RmTmp\",        \"writable\": false    },    \"201201/UhCmd\": {        \"value\": 0,        \"route\": \"/vui/platforms/benshome/devices/201201/UhCmd\",        \"writable\": false    },    \"201201/GlblHtgDsbl\": {        \"value\": 0,        \"route\": \"/vui/platforms/benshome/devices/201201/GlblHtgDsbl\",        \"writable\": false    }}","payloadType":"str","x":310,"y":840,"wires":[["1d5fa17768faa0ad"]]},{"id":"7a1f9c4d.b6f23c","type":"function","z":"c791cbc0.84f648","name":"","func":"let cookie= 'atuserid={\"name\":\"atuserid\",\"val\":\"3160539b-a16a-456a-9537-1a363993b0a8\",\"options\":{\"end\":\"2023-01-03T15:46:25.274Z\",\"path\":\"/\"}}; public=h8ha1a8ajdhjotpnr2nmsg3hu5; atauthority={\"name\":\"atauthority\",\"val\":{\"authority_name\":\"cnil\",\"visitor_mode\":\"exempt\"},\"options\":{\"end\":\"2023-01-19T09:33:17.178Z\",\"path\":\"/\"}}';\n\nmsg.headers = {};\nmsg.headers['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.0.xx.xx Safari/537.36';\nmsg.headers['accept-language']= 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6';\nmsg.headers['cache-control'] = 'max-age=0';\nmsg.headers['content-type']= 'application/x-www-form-urlencoded';\nmsg.cookies = msg.responseCookies;\ndelete msg.responseCookies;\n\n\n\nlet data = msg.payload.slice(0,10).reduce((obj,e) => {obj[e.id] = e.value; return obj},{})\nnode.warn(data)\n\nmsg.url='https://www.prix-carburants.gouv.fr';\n\n msg.payload = data;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":1000,"wires":[["f86aa943.42e648"]]},{"id":"ba593c35.0e7ad8","type":"http request","z":"c791cbc0.84f648","name":"","method":"GET","ret":"txt","paytoqs":"body","url":"","tls":"","persist":true,"proxy":"","authType":"","x":420,"y":900,"wires":[["fb2e7c30.1a254"]]},{"id":"c3db1f81.260b9","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1000,"wires":[["7a1f9c4d.b6f23c"]]},{"id":"bb2f08f3.8ddad8","type":"function","z":"c791cbc0.84f648","name":"","func":"msg.headers = {};\nmsg.headers['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.0.xx.xx Safari/537.36';\nmsg.headers['accept-language']= 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6';\nmsg.url='https://www.prix-carburants.gouv.fr/';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":940,"wires":[["ba593c35.0e7ad8"]]},{"id":"d450f140.e9b4b","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":940,"wires":[["bb2f08f3.8ddad8"]]}]

https://nodered.org/docs/user-guide/context

1 Like

You can use a Switch node for this. Configure one output to pass if msg.payload > previous value and one output for msg.payload < previous value. If it is the same value then there will be no output.

image

6 Likes

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