Looping through objects

Hello.

I've tried to find a way to do what I'm looking for but can't seem to figure it out.
What i need to do is loop through "msg.payload.data.Nodes[1 through however many there are]" and search for key "Name" and Properties[0] or [1] Status.
I've created a short test for it.
The end goal is to save it as flow.set("Name","Status") from above, the "Node" can be anything from 1 through 154 at different times.


I've tried to sort it out in different ways, but the two things I'm coming closest to is something like this:
(Either of the two)

/*for (var key in msg.payload.data.Nodes){
    var myObj = msg.payload.data.Nodes[key]
    
    msg.obj2 += myObj
msg.test += key
msg.payload = myObj*/
for (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){
     msg.key += key   
    msg.val += value
       
    }
    
return msg;

The msg.key/val etc is just for checking the outputs.
And as you can see in the top image, the first part always gets "undefined+key".
If I change the msg.val from += to = so I only get the last object, it's working

Test-flow:

[{"id":"a21d0ac1.37a3e8","type":"debug","z":"649a18f3.49a458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1450,"y":460,"wires":[]},{"id":"b5700a0b.66f718","type":"function","z":"649a18f3.49a458","name":"","func":"/*for (var key in msg.payload){\n    var myObj = msg.payload[key]\n    \n    msg.obj2 += myObj\nmsg.test += key\nmsg.payload = myObj*/\nfor (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){\n     msg.key += key   \n    msg.val += value\n       \n    }\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1120,"y":480,"wires":[["58948649.b189f8","a21d0ac1.37a3e8"]]},{"id":"cb8201a0.43b0c","type":"function","z":"649a18f3.49a458","name":"","func":"msg.payload = {data: {Nodes: {\n    1:{ \n    Name:\"Test\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"Off\"\n        }\n    }\n       \n    },\n    2:{ \n    Name:\"Test2\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Not:\"1\"\n        }\n    }\n       \n    },\n    3:{ \n    Name:\"Test3\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"On\"\n        }\n    }\n       \n    }\n}\n    \n    \n    \n}\n\n\n\n\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":460,"wires":[["2edbe93d.724836","b5700a0b.66f718","a21d0ac1.37a3e8","a2848293.d9125"]]},{"id":"79983133.954c9","type":"inject","z":"649a18f3.49a458","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":710,"y":380,"wires":[["cb8201a0.43b0c"]]}]

IT is "msg.payload.data.Nodes the "1" is the name of the first object in nodes.
So you may only need to use Object.values("msg.payload.data.Nodes).
If you do not require the key "1" etc

for (key in msg.payload.data.Nodes) {
    for (state in msg.payload.data.Nodes[String(key)].Properties) {
        if(msg.payload.data.Nodes[String(key)].Properties[String(state)].Status){
            flow.set(msg.payload.data.Nodes[String(key)].Name,
                     msg.payload.data.Nodes[String(key)].Properties[String(state)].Status)
    }
  }
}
[{"id":"a21d0ac1.37a3e8","type":"debug","z":"c991dbcde03824f2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1568,"y":114,"wires":[]},{"id":"b5700a0b.66f718","type":"function","z":"c991dbcde03824f2","name":"loop obj","func":"/*for (var key in msg.payload){\n    var myObj = msg.payload[key]\n    \n    msg.obj2 += myObj\nmsg.test += key\nmsg.payload = myObj*/\n/*for (const [key, value] of Object.entries(msg.payload.data.Nodes[1])){\n     msg.key += key   \n    msg.val += value\n       \n    }\n    \nreturn msg;\n*/\n\nfor (key in msg.payload.data.Nodes) {\n    for (state in msg.payload.data.Nodes[String(key)].Properties) {\n        if(msg.payload.data.Nodes[String(key)].Properties[String(state)].Status){\n            flow.set(msg.payload.data.Nodes[String(key)].Name,\n                     msg.payload.data.Nodes[String(key)].Properties[String(state)].Status)\n    }\n  }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1440,"y":114,"wires":[["a21d0ac1.37a3e8"]]},{"id":"cb8201a0.43b0c","type":"function","z":"c991dbcde03824f2","name":"","func":"msg.payload = {data: {Nodes: {\n    1:{ \n    Name:\"Test\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"Off\"\n        }\n    }\n       \n    },\n    2:{ \n    Name:\"Test2\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Not:\"1\"\n        }\n    }\n       \n    },\n    3:{ \n    Name:\"Test3\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"On\"\n        }\n    }\n       \n    }\n}\n    \n    \n    \n}\n\n\n\n\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1308,"y":114,"wires":[["b5700a0b.66f718"]]},{"id":"79983133.954c9","type":"inject","z":"c991dbcde03824f2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1154,"y":114,"wires":[["cb8201a0.43b0c"]]}]

You can try this as well, this disregards where the status is not "On" or 'Off"

image

[
    {
        "id": "be9dbca190ca4687",
        "type": "tab",
        "label": "Flow 7",
        "disabled": false,
        "info": ""
    },
    {
        "id": "a21d0ac1.37a3e8",
        "type": "debug",
        "z": "be9dbca190ca4687",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 220,
        "wires": []
    },
    {
        "id": "cb8201a0.43b0c",
        "type": "function",
        "z": "be9dbca190ca4687",
        "name": "",
        "func": "msg.payload = {data: {Nodes: {\n    1:{ \n    Name:\"Test\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"Off\"\n        }\n    }\n       \n    },\n    2:{ \n    Name:\"Test2\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Not:\"1\"\n        }\n    }\n       \n    },\n    3:{ \n    Name:\"Test3\",\n    Properties:{\n        0:{\n            0:\"0\"\n        },\n        1:{\n            Status:\"On\"\n        }\n    }\n       \n    }\n}\n    \n    \n    \n}\n\n\n\n\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 280,
        "y": 220,
        "wires": [
            [
                "cd6170b76258eec6"
            ]
        ]
    },
    {
        "id": "79983133.954c9",
        "type": "inject",
        "z": "be9dbca190ca4687",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 220,
        "wires": [
            [
                "cb8201a0.43b0c"
            ]
        ]
    },
    {
        "id": "cd6170b76258eec6",
        "type": "change",
        "z": "be9dbca190ca4687",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.data.Nodes",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 220,
        "wires": [
            [
                "914995cf5d692d96"
            ]
        ]
    },
    {
        "id": "914995cf5d692d96",
        "type": "split",
        "z": "be9dbca190ca4687",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 590,
        "y": 220,
        "wires": [
            [
                "0f99d41a8d97fc54"
            ]
        ]
    },
    {
        "id": "0f99d41a8d97fc54",
        "type": "switch",
        "z": "be9dbca190ca4687",
        "name": "",
        "property": "payload.Properties[\"1\"].Status",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "Off",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "On",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 710,
        "y": 220,
        "wires": [
            [
                "62eb5b3becb99671"
            ],
            [
                "62eb5b3becb99671"
            ]
        ]
    },
    {
        "id": "62eb5b3becb99671",
        "type": "function",
        "z": "be9dbca190ca4687",
        "name": "",
        "func": "flow.set(msg.payload.Name, msg.payload.Properties[\"1\"].Status)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 860,
        "y": 220,
        "wires": [
            [
                "a21d0ac1.37a3e8"
            ]
        ]
    }
]

Thanks a lot @RootShell-coder and @ScheepersJohan !
Both of your solutions seems to work for me, I will have to try it with the live flow this weekend to make sure but it seems to do exactly what I want

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