Save more variables in file with function node

Hi,
I try to save more or one variable in the nodered file system with the same function node.
I have two different MQTT sources, one is a zyklic response with all variables at once and the other source only sends one variable onchange.
The Problem is, that only the first argument is working. I tried if, else if and else, but don´t get it, obviously.

Here is the example code, It would be great, if someone could have a look:

[
    {
        "id": "18c63b397ab83688",
        "type": "function",
        "z": "b778fbe9f2d14352",
        "name": "",
        "func": "if (msg.payload.hasOwnProperty(\"var01\")) {\n    context.set('var01', msg.payload.var01, 'file');\n}\nelse if (msg.payload.hasOwnProperty(\"var02\")) {\n    context.set('var02', msg.payload.var02, 'file');\n}\nelse if (msg.payload.hasOwnProperty(\"var03\")) {\n    context.set('var03', msg.payload.var03, 'file');\n}\nelse if (msg.payload.hasOwnProperty(\"var04\")) {\n    context.set('var04', msg.payload.var04, 'file');\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1840,
        "y": 1560,
        "wires": [
            [
                "aa9f002d8ad708ed"
            ]
        ]
    },
    {
        "id": "aa9f002d8ad708ed",
        "type": "debug",
        "z": "b778fbe9f2d14352",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 2010,
        "y": 1560,
        "wires": []
    },
    {
        "id": "af034db73782efa0",
        "type": "inject",
        "z": "b778fbe9f2d14352",
        "name": "var01 - var04",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"var01\":\"1\",\"var02\":\"2\",\"var03\":\"3\",\"var04\":\"4\"}",
        "payloadType": "json",
        "x": 1650,
        "y": 1560,
        "wires": [
            [
                "18c63b397ab83688"
            ]
        ]
    },
    {
        "id": "ad388d7199e6e134",
        "type": "inject",
        "z": "b778fbe9f2d14352",
        "name": "{\"var01\":\"10\"}",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"var01\":\"10\"}",
        "payloadType": "json",
        "x": 1650,
        "y": 1620,
        "wires": [
            [
                "18c63b397ab83688"
            ]
        ]
    },
    {
        "id": "4985b6a75870bc1a",
        "type": "inject",
        "z": "b778fbe9f2d14352",
        "name": "{\"var02\":\"20\"}",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"var02\":\"20\"}",
        "payloadType": "json",
        "x": 1650,
        "y": 1660,
        "wires": [
            [
                "18c63b397ab83688"
            ]
        ]
    },
    {
        "id": "caccf9ef63152951",
        "type": "inject",
        "z": "b778fbe9f2d14352",
        "name": "{\"var03\":\"30\"}",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"var03\":\"30\"}",
        "payloadType": "json",
        "x": 1650,
        "y": 1700,
        "wires": [
            [
                "18c63b397ab83688"
            ]
        ]
    },
    {
        "id": "37738e780b95fa2f",
        "type": "inject",
        "z": "b778fbe9f2d14352",
        "name": "{\"var04\":\"40\"}",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"var04\":\"40\"}",
        "payloadType": "json",
        "x": 1650,
        "y": 1740,
        "wires": [
            [
                "18c63b397ab83688"
            ]
        ]
    }
]

You need a series of if statements, not if else else else.

Alternatively, split the message up and handle each element individually

[{"id":"af034db73782efa0","type":"inject","z":"1b9d498f6a1f24f8","name":"var01 - var04","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"var01\":\"1\",\"var02\":\"2\",\"var03\":\"3\",\"var04\":\"4\"}","payloadType":"json","x":190,"y":100,"wires":[["63757ad8c0489ae9"]]},{"id":"ad388d7199e6e134","type":"inject","z":"1b9d498f6a1f24f8","name":"{\"var01\":\"10\"}","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"var01\":\"10\"}","payloadType":"json","x":190,"y":160,"wires":[["63757ad8c0489ae9"]]},{"id":"4985b6a75870bc1a","type":"inject","z":"1b9d498f6a1f24f8","name":"{\"var02\":\"20\"}","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"var02\":\"20\"}","payloadType":"json","x":190,"y":200,"wires":[["63757ad8c0489ae9"]]},{"id":"caccf9ef63152951","type":"inject","z":"1b9d498f6a1f24f8","name":"{\"var03\":\"30\"}","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"var03\":\"30\"}","payloadType":"json","x":190,"y":240,"wires":[["63757ad8c0489ae9"]]},{"id":"37738e780b95fa2f","type":"inject","z":"1b9d498f6a1f24f8","name":"{\"var04\":\"40\"}","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"var04\":\"40\"}","payloadType":"json","x":190,"y":280,"wires":[["63757ad8c0489ae9"]]},{"id":"63757ad8c0489ae9","type":"split","z":"1b9d498f6a1f24f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":180,"wires":[["6cadb57c1f5e1426"]]},{"id":"b8992c27c59ae555","type":"debug","z":"1b9d498f6a1f24f8","name":"debug 139","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":180,"wires":[]},{"id":"6cadb57c1f5e1426","type":"function","z":"1b9d498f6a1f24f8","name":"function 3","func":"context.set(msg.parts.key, msg.payload, 'file')\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":180,"wires":[["b8992c27c59ae555"]]}]

That makes sence, thank you!
The "parts" function is very handy :slight_smile:

1 Like

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