# Subtraction of two values

**URL:** <https://discourse.nodered.org/t/subtraction-of-two-values/74396>\
**Category:** General\
**Created:** [30 January 2023 09:55 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396 "2023-01-30T09:55:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierpaolo1972](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pierpaolo1972/32/71260_2.png) [@pierpaolo1972](https://discourse.nodered.org/u/pierpaolo1972)\
**Post date:** [30 January 2023 09:55 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/1 "2023-01-30T09:55:15Z")

</div>

![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/1441e79327496f87e60218c3b4e799cec9f42a22.jpeg)  
I need to subtract two values. "sensor.instant\_solar\_power" - "sensor.recharge\_auto\_power" = ?  
Which node should I use and which code? Can someone help me? thank you

---

<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:** [30 January 2023 10:01 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/2 "2023-01-30T10:01:13Z")

</div>

You need a way to distinguish between A and B, perhaps using msg.topic.  
Then you need a way to get A and B into the same message, either using a join node or context storage.  
Finally you need the values to be numbers not strings.

---

<div class="post-metadata">

**Author:** ![pierpaolo1972](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pierpaolo1972/32/71260_2.png) [@pierpaolo1972](https://discourse.nodered.org/u/pierpaolo1972)\
**Post date:** [30 January 2023 10:49 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/3 "2023-01-30T10:49:06Z")

</div>

Ok, the string was my mistake. It is correct now. For the rest can you give me a code example. I'm not very good with 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:** [30 January 2023 11:03 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/4 "2023-01-30T11:03:03Z")

</div>

Just a hint:  
 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e70f9eb7f56f164f24b7e59a2ad399395080032f.jpeg)

---

<div class="post-metadata">

**Author:** ![nico25](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@nico25](https://discourse.nodered.org/u/nico25)\
**Post date:** [30 January 2023 11:08 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/5 "2023-01-30T11:08:50Z")

</div>

One option:

```auto
[
    {
        "id": "32311aa6846c5342",
        "type": "change",
        "z": "ad5eb12b388e5d52",
        "name": "Connect to Payload A",
        "rules": [
            {
                "t": "set",
                "p": "ValueA",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 400,
        "wires": [
            [
                "5d0b9f3abf8da0b1"
            ]
        ]
    },
    {
        "id": "03f4aca5b398a77e",
        "type": "change",
        "z": "ad5eb12b388e5d52",
        "name": "Connect to Payload B",
        "rules": [
            {
                "t": "set",
                "p": "ValueB",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 440,
        "wires": [
            [
                "5d0b9f3abf8da0b1"
            ]
        ]
    },
    {
        "id": "5d0b9f3abf8da0b1",
        "type": "function",
        "z": "ad5eb12b388e5d52",
        "name": "Substract",
        "func": "var payloadA\nvar payloadB\n\npayloadA = flow.get(\"ValueA\"||0);\npayloadB = flow.get(\"ValueB\"||0);\nmsg.payload = payloadA - payloadB;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 420,
        "wires": [
            [
                "7f8252d4dd3a5681"
            ]
        ]
    },
    {
        "id": "7f8252d4dd3a5681",
        "type": "debug",
        "z": "ad5eb12b388e5d52",
        "name": "result",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 810,
        "y": 420,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [30 January 2023 15:51 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/6 "2023-01-30T15:51:59Z")

</div>

```auto
[{"id":"93f16891d794e03b","type":"join","z":"ac30d9279e67c57b","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":320,"wires":[["e9111659cebc98f5"]]},{"id":"70b47bac5cae85b0","type":"inject","z":"ac30d9279e67c57b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"50","payloadType":"num","x":390,"y":280,"wires":[["93f16891d794e03b"]]},{"id":"cb134e7c5a77d728","type":"inject","z":"ac30d9279e67c57b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"30","payloadType":"num","x":390,"y":360,"wires":[["93f16891d794e03b"]]},{"id":"e9111659cebc98f5","type":"change","z":"ac30d9279e67c57b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.one-payload.two","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":320,"wires":[["bf32970f8c0d987a"]]},{"id":"bf32970f8c0d987a","type":"debug","z":"ac30d9279e67c57b","name":"debug 230","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":320,"wires":[]}]

```

---

<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:** [13 February 2023 15:52 UTC](https://discourse.nodered.org/t/subtraction-of-two-values/74396/7 "2023-02-13T15:52:22Z")

</div>

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