Comparing previous values

Hello.
Im trying to compare a value which is entered into the text field to the one that was put in before, failing hard to make it work. Instead of getting the result of Delta i get a ever longer getting string of nonsense, please help

Node-RED version: v2.2.2
Node.js version: v18.0.0
Windows_NT 10.0.19044 x64 LE
Dashboard version 3.1.6

[
    {
        "id": "3a2688a1cd092798",
        "type": "ui_text_input",
        "z": "602cc2206a13741f",
        "name": "",
        "label": "",
        "tooltip": "",
        "group": "5f36819feb573bce",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "number",
        "delay": "0",
        "topic": "topic",
        "sendOnBlur": false,
        "className": "",
        "topicType": "msg",
        "x": 250,
        "y": 260,
        "wires": [
            [
                "bf7937ddf575b2ef",
                "a9e4e0f1090131c6",
                "9f547a1462a5dafa"
            ]
        ]
    },
    {
        "id": "bf7937ddf575b2ef",
        "type": "change",
        "z": "602cc2206a13741f",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 270,
        "y": 300,
        "wires": [
            [
                "3a2688a1cd092798"
            ]
        ]
    },
    {
        "id": "8095094f3d91dfa2",
        "type": "inject",
        "z": "602cc2206a13741f",
        "name": "",
        "props": [
            {
                "p": "payload.bf",
                "v": "0",
                "vt": "num"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 230,
        "y": 220,
        "wires": [
            [
                "a62f8009e55a879d"
            ]
        ]
    },
    {
        "id": "a62f8009e55a879d",
        "type": "function",
        "z": "602cc2206a13741f",
        "name": "",
        "func": "msg.payload = {bf: msg.payload}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 220,
        "wires": [
            [
                "cdbdc68a34ca4c3f"
            ]
        ]
    },
    {
        "id": "a9e4e0f1090131c6",
        "type": "function",
        "z": "602cc2206a13741f",
        "name": "",
        "func": "msg.payload = {rn: msg.payload}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 260,
        "wires": [
            [
                "cdbdc68a34ca4c3f",
                "6e05202087d8cfda"
            ]
        ]
    },
    {
        "id": "da17e047c6aac9dc",
        "type": "function",
        "z": "602cc2206a13741f",
        "name": "",
        "func": "var bf = { payload: msg.payload.bf };\nvar rn = { payload: msg.payload.rn };\nvar delta = rn - bf;\ndelta = { payload: msg.payload.delta};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 920,
        "y": 220,
        "wires": [
            [
                "f7ed5c24f10c3da2",
                "817cca998a743ce0",
                "079a235c91fe0252"
            ]
        ]
    },
    {
        "id": "cdbdc68a34ca4c3f",
        "type": "join",
        "z": "602cc2206a13741f",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 690,
        "y": 220,
        "wires": [
            [
                "da17e047c6aac9dc",
                "ebb69e2f63316bf3"
            ]
        ]
    },
    {
        "id": "f7ed5c24f10c3da2",
        "type": "function",
        "z": "602cc2206a13741f",
        "name": "",
        "func": "msg.payload.bf = msg.payload.rn;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1200,
        "y": 200,
        "wires": [
            [
                "a62f8009e55a879d"
            ]
        ]
    },
    {
        "id": "817cca998a743ce0",
        "type": "ui_text",
        "z": "602cc2206a13741f",
        "group": "5f36819feb573bce",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Δ",
        "format": "{{msg.payload.delta}}",
        "layout": "row-spread",
        "className": "",
        "x": 1190,
        "y": 240,
        "wires": []
    },
    {
        "id": "6e05202087d8cfda",
        "type": "debug",
        "z": "602cc2206a13741f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 260,
        "wires": []
    },
    {
        "id": "ebb69e2f63316bf3",
        "type": "debug",
        "z": "602cc2206a13741f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 930,
        "y": 260,
        "wires": []
    },
    {
        "id": "9f547a1462a5dafa",
        "type": "debug",
        "z": "602cc2206a13741f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 490,
        "y": 300,
        "wires": []
    },
    {
        "id": "079a235c91fe0252",
        "type": "file",
        "z": "602cc2206a13741f",
        "name": "Temp.txt",
        "filename": "C:\\Users\\m.bosshart\\Documents\\Temp.txt",
        "appendNewline": true,
        "createDir": false,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1200,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "5f36819feb573bce",
        "type": "ui_group",
        "name": "Delta",
        "tab": "dbf999e51b777958",
        "order": 8,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "dbf999e51b777958",
        "type": "ui_tab",
        "name": "Demo",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

Well this function node looks wrong to me: you assign a value to var delta, then you replace it with something else and then you don't use it in the returned mesage.
Untitled 5

But it all seems very complicated just to compare curent and previous values.

You can store the previous value in context data flow.set("lastvalue", msg.payload)
And use it var lastvalue = flow.get("lastvalue") || 0

thanks a lot for answering.
can you explain like i'm five?
sadly im rather new to node red

This very simple flow will display the difference between two injected values. In this case timestamps, so milliseconds between injects


"compare the wo" should of course be "compare the two".

[{"id":"53d29c1985fcfdd3","type":"inject","z":"b5d64c1e024cd3e1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":60,"wires":[["e02db477d785d7d2"]]},{"id":"e02db477d785d7d2","type":"function","z":"b5d64c1e024cd3e1","name":"","func":"// Get stored value, use payload if nothing is stored\nvar previous = flow.get(\"previousvalue\") || msg.payload;\n\n// Save this value for next time\nflow.set(\"previousvalue\", msg.payload);   \n\n// Compare the wo\nvar difference = msg.payload - previous;\nmsg.payload = difference;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":120,"wires":[["2cd5ad48a602b649"]]},{"id":"2cd5ad48a602b649","type":"debug","z":"b5d64c1e024cd3e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":180,"wires":[]}]

Well you made it here now! :grin:

1 Like

Thank you very much, i was grinding my teeth over this for way too long :slight_smile:

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