# Comparing previous values

**URL:** <https://discourse.nodered.org/t/comparing-previous-values/62118>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [3 May 2022 11:45 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118 "2022-05-03T11:45:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Creeper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m_creeper/32/60974_2.png) [@M\_Creeper](https://discourse.nodered.org/u/M_Creeper)\
**Post date:** [3 May 2022 11:45 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/1 "2022-05-03T11:45:34Z")

</div>

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

 ![Screenshot 2022-05-03 134028](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/daaf1bdca6f6748e4b51718501205a423e92c8e1.png)

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

```

---

<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:** [3 May 2022 12:01 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/2 "2022-05-03T12:01:30Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a30912105564bda271736486793f47483b6490d3.jpeg)

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

---

<div class="post-metadata">

**Author:** ![M\_Creeper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m_creeper/32/60974_2.png) [@M\_Creeper](https://discourse.nodered.org/u/M_Creeper)\
**Post date:** [3 May 2022 12:15 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/3 "2022-05-03T12:15:13Z")

</div>

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

---

<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:** [3 May 2022 12:35 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/4 "2022-05-03T12:35:33Z")

</div>

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

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b230f70c8b42a9d4338fdd84a66afc9939cf6db8.jpeg)  
"compare the wo" should of course be "compare the two".

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

```

> [@M\_Creeper](#):
>
> sadly im rather new to node red

Well you made it here now! 😁

---

<div class="post-metadata">

**Author:** ![M\_Creeper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m_creeper/32/60974_2.png) [@M\_Creeper](https://discourse.nodered.org/u/M_Creeper)\
**Post date:** [3 May 2022 12:47 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/5 "2022-05-03T12:47:17Z")

</div>

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

---

<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:** [17 May 2022 12:47 UTC](https://discourse.nodered.org/t/comparing-previous-values/62118/6 "2022-05-17T12:47:40Z")

</div>

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