NodeRED Subtraktion Zählerstand

Ich betreibe 2 Zähler in Kaskadenschaltung. Mein HA bzw. das Energie Dashboard füttere üner mqtt mit Zählerauslesung via Tasmota.
Um den Korrekten Zählerstand zu ermitteln muss ich Zählwerk Z2 von Z1 subtrahieren.
Hier hänge ich seit Weihnachten dran rum und komme nicht weiter.

Anbei der Flow

[
    {
        "id": "d2af8ff2e9dc87aa",
        "type": "comment",
        "z": "1a45702f9f8357ba",
        "name": "Zähler EVU Z1",
        "info": "",
        "x": 90,
        "y": 60,
        "wires": []
    },
    {
        "id": "2b182a8dd690478d",
        "type": "mqtt in",
        "z": "1a45702f9f8357ba",
        "name": "",
        "topic": "tele/Zahler/EVUZ1WMP/Zahlerstand/KWH/SENSOR",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "571760fff4daa5ae",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 200,
        "y": 100,
        "wires": [
            [
                "9b40db818510d472",
                "e2ff64f61b29671e"
            ]
        ]
    },
    {
        "id": "9b40db818510d472",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "debug 21",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 490,
        "y": 100,
        "wires": []
    },
    {
        "id": "da8efacfadb49365",
        "type": "function",
        "z": "1a45702f9f8357ba",
        "name": "Komma wegrechnen",
        "func": "msg.payload = parseFloat((msg.payload / 1).toFixed(1));\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 220,
        "wires": [
            [
                "26e698448e4036f1",
                "e711f96b7fcb418d",
                "90479da841cf1470"
            ]
        ]
    },
    {
        "id": "26e698448e4036f1",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "Zählerstand Z1 1.8.0",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 680,
        "y": 220,
        "wires": []
    },
    {
        "id": "e2ff64f61b29671e",
        "type": "change",
        "z": "1a45702f9f8357ba",
        "name": "Umwandeln in Jason",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.MT691.Total_in",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 260,
        "y": 160,
        "wires": [
            [
                "2310e8cd545e9795",
                "da8efacfadb49365"
            ]
        ]
    },
    {
        "id": "2310e8cd545e9795",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "debug 23",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 490,
        "y": 160,
        "wires": []
    },
    {
        "id": "e2f3b0f135576722",
        "type": "comment",
        "z": "1a45702f9f8357ba",
        "name": "Zähler EVU Z2",
        "info": "",
        "x": 90,
        "y": 400,
        "wires": []
    },
    {
        "id": "3be7a1929bbdd9fa",
        "type": "mqtt in",
        "z": "1a45702f9f8357ba",
        "name": "",
        "topic": "tele/Zahler/EVUZ2Haushalt/Zahlerstand/KWH/SENSOR",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "571760fff4daa5ae",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 210,
        "y": 440,
        "wires": [
            [
                "5c62af95e7401e5f",
                "580ce86e9bfe60ee"
            ]
        ]
    },
    {
        "id": "5c62af95e7401e5f",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "debug 24",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 520,
        "y": 440,
        "wires": []
    },
    {
        "id": "ab3d2a649afddc0f",
        "type": "function",
        "z": "1a45702f9f8357ba",
        "name": "Komma wegrechnen",
        "func": "msg.payload = parseFloat((msg.payload / 1).toFixed(1));\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 560,
        "wires": [
            [
                "d34ee5122af73ddf",
                "d0be112a76829812"
            ]
        ]
    },
    {
        "id": "d34ee5122af73ddf",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "Zählerstand Z2 1.8.0",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 700,
        "y": 560,
        "wires": []
    },
    {
        "id": "580ce86e9bfe60ee",
        "type": "change",
        "z": "1a45702f9f8357ba",
        "name": "Umwandeln in Json",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.MT691.Total_in",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 270,
        "y": 500,
        "wires": [
            [
                "2d5ae647be3dd027",
                "ab3d2a649afddc0f"
            ]
        ]
    },
    {
        "id": "2d5ae647be3dd027",
        "type": "debug",
        "z": "1a45702f9f8357ba",
        "name": "debug 26",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 510,
        "y": 500,
        "wires": []
    },
    {
        "id": "e711f96b7fcb418d",
        "type": "influxdb out",
        "z": "1a45702f9f8357ba",
        "influxdb": "8d4670114b89464c",
        "name": "Zahlerstand Z1 180 EVU",
        "measurement": "Zahlerstand Z1 180 EVU",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "EFH-goetz",
        "bucket": "efhgoetz",
        "x": 710,
        "y": 280,
        "wires": []
    },
    {
        "id": "90479da841cf1470",
        "type": "mqtt out",
        "z": "1a45702f9f8357ba",
        "name": "",
        "topic": "Zahler/EVUZ1/180/Zahlerstand",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "571760fff4daa5ae",
        "x": 730,
        "y": 340,
        "wires": []
    },
    {
        "id": "d0be112a76829812",
        "type": "mqtt out",
        "z": "1a45702f9f8357ba",
        "name": "",
        "topic": "Zahler/EVUZ2/180/Zahlerstand",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "571760fff4daa5ae",
        "x": 750,
        "y": 620,
        "wires": []
    },
    {
        "id": "571760fff4daa5ae",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.3.150",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "8d4670114b89464c",
        "type": "influxdb",
        "hostname": "192.168.2.27",
        "port": "8086",
        "protocol": "http",
        "database": "database",
        "name": "InfluxDB",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "2.0",
        "url": "http://192.168.3.150:8086",
        "rejectUnauthorized": true
    }
]

Von mqtt kommt ein Objekt rein. Da zeihe ich mir den Zählerstand raus.
Mit einem change in JSONData.
Danach mach kürze ich noch die Komma stellen.

Ich habe schon mit dem Node Join experimentiert.
Allerdings begreiffe ich nicht wie den Werten Topic zuteilen kann.

evtl. kann mir jemand hlefen.

Ich versteh nicht so wirklich das Problem.

Zähler 1 auslesen, in flow variable1 speichern, Zähler 2 auslesen, in flow variable2 speichern, Werte abziehen. fertig.

Alles nach einander und nicht gleichzeitzig.
Beispiel :

flow.set( "zaehler_1",[hier deine wert z.B. Number(msg.payload) ] );
flow.set("zaehler_1",Number(msg.payload));
[deine Variable] = flow.get("zaehler_1";

mache etwas ähnliches mit der PV-Leistung

join-node → funktion → Entität

var PV1 = msg.payload.PV1_W;
var PV2 = msg.payload.PV2_W;
var PV3 = msg.payload.PV3_W;

var math = Number(PV1) + Number(PV2) + Number(PV3);
msg.payload = math.toFixed(0);
msg.topic = "PV Summe";

return msg;

mit dem flow.set kannte ich bisher nicht.
@Dreistein kannst du uns ein komplettes Beispiel posten, wäre cool

Ne, mein Spagetti Code ist nichts für die Öffentlichkeit.

Könntest du mal bitte beide Objekte aus den MQTT-Nodes posten, würde mal etwas probieren wollen.

@crypton

bin ich gemeint?

31.12.2024, 08:21:23node: debug 21
tele/Zahler/EVUZ1WMP/Zahlerstand/KWH/SENSOR : msg.payload : Object
object
Time: "2024-12-31T08:21:24"
MT691: object
Total_in: 820.546
Power_cur: 1337
Power_p1: 543
Power_p2: 378
Power_p3: 415
Total_out: 0.058
31.12.2024, 08:26:09node: debug 24
tele/Zahler/EVUZ2Haushalt/Zahlerstand/KWH/SENSOR : msg.payload : Object
object
Time: "2024-12-31T08:26:10"
MT691: object
Total_in: 348.522
Power_cur: 222
Power_p1: 118
Power_p2: 33
Power_p3: 70
Total_out: 0.327

Meinst du damit die Objekte? Als Ausgabe des Debugs?

Ich habe es geschaft die Zählerstände in eine Flow Variable zu packen.
Weiter bin ich noch nicht.

Ja, als JSON wären die beiden Objekte schön, wenn du die so posten könntest.
Im DEBUG-Node auf ganzes Nachrichtenobjekt umschalten
image

und dann das Objekt kopieren.
image

Anbei das Objekt

{"topic":"tele/Zahler/EVUZ1WMP/Zahlerstand/KWH/SENSOR","payload":{"Time":"2024-12-31T15:34:34","MT691":{"Total_in":836.837,"Power_cur":2516,"Power_p1":1118,"Power_p2":743,"Power_p3":654,"Total_out":0.058}},"qos":0,"retain":false,"_msgid":"8fc02c84df26d82f"}

Habe es jetzt nicht groß getestet. Geht bestimmt noch kompakter :wink:

[
    {
        "id": "d0be112a76829812",
        "type": "mqtt out",
        "z": "bde505a4529c0a34",
        "name": "",
        "topic": "Zahler/EVUZ2/180/Zahlerstand",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "571760fff4daa5ae",
        "x": 1050,
        "y": 2280,
        "wires": []
    },
    {
        "id": "b424af2eccb2872b",
        "type": "debug",
        "z": "bde505a4529c0a34",
        "name": "Ausgabe",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1580,
        "y": 2200,
        "wires": []
    },
    {
        "id": "68e34a44a47e923c",
        "type": "join",
        "z": "bde505a4529c0a34",
        "name": "Werte zusammenfügen",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": false,
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1030,
        "y": 2200,
        "wires": [
            [
                "219b4b1fb631202d"
            ]
        ]
    },
    {
        "id": "219b4b1fb631202d",
        "type": "change",
        "z": "bde505a4529c0a34",
        "name": "Werte subtrahieren und Objekt bereinigen",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.Z2 - payload.Z1",
                "tot": "jsonata"
            },
            {
                "t": "delete",
                "p": "topic",
                "pt": "msg"
            },
            {
                "t": "delete",
                "p": "qos",
                "pt": "msg"
            },
            {
                "t": "delete",
                "p": "retain",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1340,
        "y": 2200,
        "wires": [
            [
                "b424af2eccb2872b"
            ]
        ]
    },
    {
        "id": "29ebe0c39b39f59a",
        "type": "change",
        "z": "bde505a4529c0a34",
        "name": "Payload und Topic setzen",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$round(payload.MT691.Total_in, 0)",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Z1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 650,
        "y": 2160,
        "wires": [
            [
                "68e34a44a47e923c",
                "5eb932f2c2577352",
                "13034f17b4062c9b",
                "53d5ce3b68817ecc"
            ]
        ]
    },
    {
        "id": "658f1a3c6f3245ae",
        "type": "change",
        "z": "bde505a4529c0a34",
        "name": "Payload und Topic setzen",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$round(payload.MT691.Total_in, 0)\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Z2",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 650,
        "y": 2260,
        "wires": [
            [
                "68e34a44a47e923c",
                "d0be112a76829812",
                "212a34a350fefb4e"
            ]
        ]
    },
    {
        "id": "5eb932f2c2577352",
        "type": "mqtt out",
        "z": "bde505a4529c0a34",
        "name": "",
        "topic": "Zahler/EVUZ1/180/Zahlerstand",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "571760fff4daa5ae",
        "x": 1050,
        "y": 2060,
        "wires": []
    },
    {
        "id": "212a34a350fefb4e",
        "type": "debug",
        "z": "bde505a4529c0a34",
        "name": "Zählerstand Z2 1.8.0",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1030,
        "y": 2340,
        "wires": []
    },
    {
        "id": "13034f17b4062c9b",
        "type": "debug",
        "z": "bde505a4529c0a34",
        "name": "Zählerstand Z1 1.8.0",
        "active": true,
        "tosidebar": false,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1030,
        "y": 2000,
        "wires": []
    },
    {
        "id": "b2366e857432c654",
        "type": "comment",
        "z": "bde505a4529c0a34",
        "name": "Zähler EVU Z2",
        "info": "",
        "x": 140,
        "y": 2220,
        "wires": []
    },
    {
        "id": "72c11282b72bf78c",
        "type": "mqtt in",
        "z": "bde505a4529c0a34",
        "name": "",
        "topic": "tele/Zahler/EVUZ2Haushalt/Zahlerstand/KWH/SENSOR",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "571760fff4daa5ae",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 260,
        "y": 2260,
        "wires": [
            [
                "658f1a3c6f3245ae"
            ]
        ]
    },
    {
        "id": "53d5ce3b68817ecc",
        "type": "influxdb out",
        "z": "bde505a4529c0a34",
        "influxdb": "8d4670114b89464c",
        "name": "Zahlerstand Z1 180 EVU",
        "measurement": "Zahlerstand Z1 180 EVU",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "EFH-goetz",
        "bucket": "efhgoetz",
        "x": 1030,
        "y": 2120,
        "wires": []
    },
    {
        "id": "f6cf91f12e0eee23",
        "type": "comment",
        "z": "bde505a4529c0a34",
        "name": "Zähler EVU Z1",
        "info": "",
        "x": 140,
        "y": 2120,
        "wires": []
    },
    {
        "id": "1199125fa092df22",
        "type": "mqtt in",
        "z": "bde505a4529c0a34",
        "name": "",
        "topic": "tele/Zahler/EVUZ1WMP/Zahlerstand/KWH/SENSOR",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "571760fff4daa5ae",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 250,
        "y": 2160,
        "wires": [
            [
                "29ebe0c39b39f59a"
            ]
        ]
    },
    {
        "id": "571760fff4daa5ae",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.3.150",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

Habe wieder Zeit gefunden und das Dank crypton hinbekommen.
Daaaanke