Interpret MQTT object and update output according to which parameter is available

From one source (shelly1pmplus) I am receiving different objects on the same MQTT address:

E.g.

For the measured current:

{"src":"shellyplus1pm-441793cfda38","dst":"shellyplus1pm-pvbalkon/events","method":"NotifyStatus","params":{"ts":1663331015.85,"switch:0":{"id":0,"current":0.7}}}

or

For the measured power:

{"src":"shellyplus1pm-441793cfda38","dst":"shellyplus1pm-pvbalkon/events","method":"NotifyStatus","params":{"ts":1663331015.85,"switch:0":{"id":0,"apower":153.4}}}

How is it possible to parse for the specific values and forward to e.g. an influx database seperately?

The only difference between the two messages seems to be that one has msg.payload.params["switch:0"].apower while the other has msg.payload.params["switch:0"].current.

You could use a switch node to test for the existence of msg.payload.params["switch:0"].apower:
Untitled 1

Are they sometimes both there?

As far as I observed there is always only one. But there are more than these two messages. How do I handle three or more?

maybe try msg.payload.params["switch:0"] and test has key string apower etc

The final solution

[
    {
        "id": "e8302ea3fbb3a7d9",
        "type": "mqtt in",
        "z": "f6f2187d.f17ca8",
        "name": "PV Balkon",
        "topic": "shellyplus1pm-pvbalkon/events/rpc",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "b912bc20557e4732",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 100,
        "y": 3580,
        "wires": [
            [
                "489a4e6e68885a07"
            ]
        ]
    },
    {
        "id": "489a4e6e68885a07",
        "type": "switch",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "property": "payload.params[\"switch:0\"].apower",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nnull"
            },
            {
                "t": "null"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 250,
        "y": 3680,
        "wires": [
            [
                "ceef30dcabb6bd3a"
            ],
            [
                "92ea03355df26eaa"
            ]
        ]
    },
    {
        "id": "92ea03355df26eaa",
        "type": "switch",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "property": "payload.params[\"switch:0\"].current",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nnull"
            },
            {
                "t": "null"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 3760,
        "wires": [
            [
                "b7506c5431cae1ec"
            ],
            [
                "927abb61de571cf2"
            ]
        ]
    },
    {
        "id": "927abb61de571cf2",
        "type": "switch",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "property": "payload.params[\"switch:0\"].aenergy",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nnull"
            },
            {
                "t": "null"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 450,
        "y": 3840,
        "wires": [
            [
                "9e40c5ba36c4beee"
            ],
            [
                "03a1613c2e8ecf46"
            ]
        ]
    },
    {
        "id": "03a1613c2e8ecf46",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "nothing",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 480,
        "y": 3940,
        "wires": []
    },
    {
        "id": "ceef30dcabb6bd3a",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "apower",
        "func": "msg.payload = msg.payload.params[\"switch:0\"].apower;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 3600,
        "wires": [
            [
                "627178e3b76fa724"
            ]
        ]
    },
    {
        "id": "b7506c5431cae1ec",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "current",
        "func": "msg.payload = msg.payload.params[\"switch:0\"].current;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 3680,
        "wires": [
            [
                "89780d26bfac53e4"
            ]
        ]
    },
    {
        "id": "9e40c5ba36c4beee",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy",
        "func": "msg.payload = msg.payload.params[\"switch:0\"].aenergy;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 600,
        "y": 3780,
        "wires": [
            [
                "930e3b5e8b93fa4c",
                "bb313777d8e9c8c2",
                "6e998650359928ba",
                "12a7282a88077905",
                "a4038beb7c256672"
            ]
        ]
    },
    {
        "id": "930e3b5e8b93fa4c",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_total",
        "func": "msg.payload = msg.payload.total;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 3780,
        "wires": [
            [
                "7f5da6a85a103648",
                "45e625f26e3c6f1e"
            ]
        ]
    },
    {
        "id": "bb313777d8e9c8c2",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_minute_ts",
        "func": "msg.payload = msg.payload.minute_ts;\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 890,
        "y": 3840,
        "wires": [
            [
                "10ef6a290f00c758"
            ]
        ]
    },
    {
        "id": "6e998650359928ba",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_0",
        "func": "msg.payload = msg.payload.by_minute[0];\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 900,
        "y": 3900,
        "wires": [
            [
                "85e8c13f43995435"
            ]
        ]
    },
    {
        "id": "12a7282a88077905",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_1",
        "func": "msg.payload = msg.payload.by_minute[1];\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 900,
        "y": 3960,
        "wires": [
            [
                "a4dba4e489edff63"
            ]
        ]
    },
    {
        "id": "a4038beb7c256672",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_2",
        "func": "msg.payload = msg.payload.by_minute[2];\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 900,
        "y": 4020,
        "wires": [
            [
                "df892f8e02ea102e"
            ]
        ]
    },
    {
        "id": "7f5da6a85a103648",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_total",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 3780,
        "wires": []
    },
    {
        "id": "10ef6a290f00c758",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_minute_ts",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 3840,
        "wires": []
    },
    {
        "id": "85e8c13f43995435",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_0",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1120,
        "y": 3900,
        "wires": []
    },
    {
        "id": "a4dba4e489edff63",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1120,
        "y": 3960,
        "wires": []
    },
    {
        "id": "df892f8e02ea102e",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "aenergy_by_minute_2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1140,
        "y": 4020,
        "wires": []
    },
    {
        "id": "627178e3b76fa724",
        "type": "influxdb out",
        "z": "f6f2187d.f17ca8",
        "influxdb": "af175a885afb64ae",
        "name": "pv_balkon_apower",
        "measurement": "pv_balkon_apower",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 530,
        "y": 3600,
        "wires": []
    },
    {
        "id": "89780d26bfac53e4",
        "type": "influxdb out",
        "z": "f6f2187d.f17ca8",
        "influxdb": "af175a885afb64ae",
        "name": "pv_balkon_current",
        "measurement": "pv_balkon_current",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 650,
        "y": 3680,
        "wires": []
    },
    {
        "id": "45e625f26e3c6f1e",
        "type": "influxdb out",
        "z": "f6f2187d.f17ca8",
        "influxdb": "af175a885afb64ae",
        "name": "pv_balkon_aenergy_total",
        "measurement": "pv_balkon_aenergy_total",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "organisation",
        "bucket": "bucket",
        "x": 1130,
        "y": 3720,
        "wires": []
    },
    {
        "id": "b912bc20557e4732",
        "type": "mqtt-broker",
        "name": "Rapserry Pi Daten 1",
        "broker": "192.168.0.76",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "af175a885afb64ae",
        "type": "influxdb",
        "hostname": "192.168.0.76",
        "port": "8086",
        "protocol": "http",
        "database": "shellyDB",
        "name": "Shellys",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "1.x",
        "url": "http://localhost:8086",
        "rejectUnauthorized": true
    }
]

I am not able to look at your flow at the moment, but if you can choose the measurement name based on the property name, perhaps you don't need to test it at all. Perhaps you can use the property name directly in the influx write.

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