How to have an editable u-list?

Hi everyone

I'm new to Node-red, but I'm loving it!
In my project I need to send a txt file to a few esp32 clients.
I managed to get the .txt file to 1 esp32 client, but what I'll need is to send to several clients, tahnt can vary.
So what I'm trring to get is have an editable list of esp32 clients ( which will turn into subscribed subscribed topics I'm be updating lets say, e.g.: Client_1, Client_2..etc).
I found thenode-red-node-ui-list weeks days example with radio buttons (here) to be a perfect display for what I need, the thing is, how to produce and editable list that would feed the ui-list demands.

The ui-list needs data in this format: [{"title":"Esp32","isChecked":false}] .
I managed to produce a list of names: ["Client_1","Client_2"], resorting to an input box that but what I'm not getting able to do is turn this list of names into a list or series (not sure what to call it) that I can feed the ui-list.

In ui-list example we can see how multiple elements should be fed to the list widget:

[{"title":"Sunday","isChecked":true},{"title":"Monday"},{"title":"Tuesday"},{"title":"Wednesday"},{"title":"Thursday"},{"title":"Friday"},{"title":"Saturday","isChecked":true}]

This seems like an list of JSON objects where title and isChecked are the keys, am I right?

the question, hot to get from a list of strings ["Client_1","Client_2"] to that list of objects =
Its a lack of javascript skills for sure.
if anyone has any example or answer that might help me I'm very gratefull.

Evenatually a delete option would probably be also usefull.

And still have to figure out hou to have this list permanent (dont loose it in every reset or power-off).

Thank everyone.

Here is my current flow, where i can, measure the length, print and delete a global list:

[
    {
        "id": "81b5cdfd56ea7bbc",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "9422ceb76fba4d42",
        "type": "inject",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read length",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Ola",
        "payloadType": "str",
        "x": 510,
        "y": 260,
        "wires": [
            [
                "f9ad60dcd7d9447a"
            ]
        ]
    },
    {
        "id": "c4c42d4670044e4f",
        "type": "debug",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read length",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 260,
        "wires": []
    },
    {
        "id": "f9ad60dcd7d9447a",
        "type": "function",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read length",
        "func": "\nvar fs = global.get('deviceList')||0;\n\n//fs[fs.length + 1] = msg.payload;\n\n//msg.payload = fs.length;\n//msg = { payload:fs[33]}\nmsg = { payload:fs.length}\nif (!msg.payload) msg.payload=\"Vazio\";\n//msg.payload=[{ \"title\": msg.payload, \"isChecked\": false }];\n//return msg;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 810,
        "y": 260,
        "wires": [
            [
                "c4c42d4670044e4f"
            ]
        ]
    },
    {
        "id": "82a053ecc65e397e",
        "type": "inject",
        "z": "81b5cdfd56ea7bbc",
        "name": "Add element",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Client_1",
        "payloadType": "str",
        "x": 510,
        "y": 340,
        "wires": [
            [
                "4c0274504c14fe11"
            ]
        ]
    },
    {
        "id": "8aa2953926e0644a",
        "type": "debug",
        "z": "81b5cdfd56ea7bbc",
        "name": "Add element",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 340,
        "wires": []
    },
    {
        "id": "4c0274504c14fe11",
        "type": "function",
        "z": "81b5cdfd56ea7bbc",
        "name": "Add element",
        "func": "const fsd = 'deviceList';\n\nlet fs = global.get(fsd) || [];\n\nglobal.set(fsd, fs);\n\nfs[fs.length] = msg.payload;\n\n\n//msg.payload = fs.length;\n//msg = { payload:fs[33]}\n//msg = { payload:fs.length}\n\n//msg.payload=[{ \"title\": msg.payload, \"isChecked\": false }];\n//return msg;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 810,
        "y": 340,
        "wires": [
            [
                "8aa2953926e0644a"
            ]
        ]
    },
    {
        "id": "755407d906abd3bc",
        "type": "inject",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read deviceList",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Ola",
        "payloadType": "str",
        "x": 520,
        "y": 300,
        "wires": [
            [
                "6510945a12641605"
            ]
        ]
    },
    {
        "id": "b731204d6fe17dfe",
        "type": "debug",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read deviceList",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1120,
        "y": 300,
        "wires": []
    },
    {
        "id": "6510945a12641605",
        "type": "function",
        "z": "81b5cdfd56ea7bbc",
        "name": "Read deviceList",
        "func": "\nvar deviceList = global.get('deviceList');\n\n//fs[fs.length + 1] = msg.payload;\n\n//msg.payload = fs.length;\n//msg = { payload:fs[33]}\nmsg = { payload: deviceList}\n\n//msg.payload=[{ \"title\": msg.payload, \"isChecked\": false }];\n//return msg;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 820,
        "y": 300,
        "wires": [
            [
                "b731204d6fe17dfe"
            ]
        ]
    },
    {
        "id": "b6e926ea63f838f3",
        "type": "inject",
        "z": "81b5cdfd56ea7bbc",
        "name": "Delete list",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 500,
        "y": 380,
        "wires": [
            [
                "a9b54a8c025573b7"
            ]
        ]
    },
    {
        "id": "cf15719dd8237868",
        "type": "debug",
        "z": "81b5cdfd56ea7bbc",
        "name": "Delete deviceList",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1130,
        "y": 380,
        "wires": []
    },
    {
        "id": "a9b54a8c025573b7",
        "type": "change",
        "z": "81b5cdfd56ea7bbc",
        "name": "",
        "rules": [
            {
                "t": "delete",
                "p": "deviceList",
                "pt": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 850,
        "y": 380,
        "wires": [
            [
                "cf15719dd8237868"
            ]
        ]
    }
]

Cheers, LuĂ­s Pereira.

Hope these examples of converting an array of strings to an array of object helps.

[{"id":"9422ceb76fba4d42","type":"inject","z":"81b5cdfd56ea7bbc","name":"create array of objects from array of strings","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Client_1\",\"Client_2\"] ","payloadType":"json","x":630,"y":260,"wires":[["f9ad60dcd7d9447a","b3af3b2a.fb525"]]},{"id":"f9ad60dcd7d9447a","type":"function","z":"81b5cdfd56ea7bbc","name":"Read length","func":"msg.payload.forEach((str,index) => msg.payload[index] = {title: str})\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":260,"wires":[["c4c42d4670044e4f"]]},{"id":"b3af3b2a.fb525","type":"change","z":"81b5cdfd56ea7bbc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.{\"title\": $}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":320,"wires":[["bc3a386d.a3a3f8"]]},{"id":"c4c42d4670044e4f","type":"debug","z":"81b5cdfd56ea7bbc","name":"javascript","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":260,"wires":[]},{"id":"bc3a386d.a3a3f8","type":"debug","z":"81b5cdfd56ea7bbc","name":"JSONata","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":320,"wires":[]}]

Javascript

msg.payload.forEach((str,index) => msg.payload[index] = {title: str});
return msg;

JSONata

$$.payload.{"title": $}

But probably better to keep you context array as an array of objects in the first place.

1 Like

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