Subtraction of two values


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

1 Like

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.

2 Likes

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

Just a hint:
Untitled 1

1 Like

One option:

[
    {
        "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": []
    }
]
1 Like
[{"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":[]}]
1 Like

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