Change node with multiple conditions, around 100

Hello there, I'm looking a way to use the Change Node to replace string variable (Mac address) for another string variable( Name of device). I have a bunch of sensors, around 100 devices, that are being receive in my flow so each one gets a custom trigger. Is there a way of reading a record table, compare the string and then change it to another string?

Does the string to be changed consist entirely of the IP address or have you got to search for the address in a longer string?

1 Like

In addition to @Colin question, is the list of ip/names static?

1 Like

Consist only of that particular string. Photo attached of the payload

In a function node, you can do a lookup e.g...

const lookup = {
   "ac233facb541": "accelerometer 1",
   "ac233facb222": "accelerometer 2",
   "ac233facb333": "accelerometer 3",
}

msg.payload.deviceName = lookup[msg.payload.deviceId] || "Unknown"
return msg
2 Likes

To save the table being created every time through you can use an Inject node to send it to a Global context variable and then fetch that in the function node.

1 Like

How can I do that?

Do what? Store the lookup in global context? Do a lookup in function? Something else?

1 Like

How can I store the global context and where do I retrieve that data?

https://nodered.org/docs/user-guide/context

1 Like

Yes it is possible in a chnage node, using JSONata
e.g.

[{"id":"a4ecc5df.f941a","type":"inject","z":"30af2d3e.d94ea2","name":"initialise lookup in context storage","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ac233facb541\":\"accelerometer 1\",\"ac233facb222\":\"accelerometer 2\",\"ac233facb333\":\"accelerometer 3\"}","payloadType":"json","x":390,"y":1840,"wires":[["f99756f6.0e1558"]]},{"id":"f99756f6.0e1558","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"lookup_device_id","pt":"global","to":"{    \"ac233facb541\": \"accelerometer 1\",    \"ac233facb222\": \"accelerometer 2\",    \"ac233facb333\": \"accelerometer 3\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1840,"wires":[[]]},{"id":"86d67ab3e9db1030","type":"inject","z":"30af2d3e.d94ea2","name":"lookup","props":[{"p":"payload.deviceId","v":"ac233facb333","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":280,"y":1900,"wires":[["4080f32e181f862f"]]},{"id":"4080f32e181f862f","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$lookup($globalContext(\"lookup_device_id\"), $$.payload.deviceId)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1900,"wires":[["323c3cb09266e08c"]]},{"id":"323c3cb09266e08c","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":1900,"wires":[]}]

Click first inject to store the lookup object to global context, click second to look up accelerator name.

expression

$lookup($globalContext("lookup_device_id"), $$.payload.deviceId)	

//or 

$globalContext("lookup_device_id." & $$.payload.deviceId)	
1 Like

thanks for the advice, was able to configure my flow accordign to my needs.

[
    {
        "id": "b004a5d84cee9331",
        "type": "ui_table",
        "z": "a53e0c5832346364",
        "group": "6c31418514176c72",
        "name": "",
        "order": 3,
        "width": 20,
        "height": 10,
        "columns": [
            {
                "field": "timestamp",
                "title": "fecha y hora",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "batteryPercentage",
                "title": "Porcentaje de bateria",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "deviceId",
                "title": "Ubicacion",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Alarma",
                "title": "Alarma",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            }
        ],
        "outputs": 1,
        "cts": true,
        "x": 1490,
        "y": 1500,
        "wires": [
            []
        ]
    },
    {
        "id": "7dedeaff123a5b1a",
        "type": "function",
        "z": "a53e0c5832346364",
        "name": "",
        "func": "const tableData = flow.get(\"savedData\") || [];\n\ntableData.unshift({\n\n  \"timestamp\": new Date(msg.payload.timestamp).toLocaleString(),\n  \"batteryPercentage\": msg.payload.batteryPercentage,\n  \"deviceId\": msg.payload.deviceId,\n  \"Alarma\": msg.payload.isButtonPressed,\n\n});\n\nmsg.payload = tableData;\n\nflow.set(\"savedData\", tableData);\n\n\nreturn msg;\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1140,
        "y": 1500,
        "wires": [
            [
                "2c264dbf75181401",
                "bf3b602d43abd630"
            ]
        ]
    },
    {
        "id": "eabb77dd53c53cc2",
        "type": "inject",
        "z": "a53e0c5832346364",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "00 03 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 570,
        "y": 1700,
        "wires": [
            [
                "a29e30711013d61a"
            ]
        ]
    },
    {
        "id": "a29e30711013d61a",
        "type": "change",
        "z": "a53e0c5832346364",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "savedData",
                "pt": "flow",
                "to": "[]",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 1700,
        "wires": [
            []
        ]
    },
    {
        "id": "8971f669e153b123",
        "type": "rbe",
        "z": "a53e0c5832346364",
        "name": "",
        "func": "rbe",
        "gap": "",
        "start": "",
        "inout": "out",
        "septopics": false,
        "property": "payload.deviceId",
        "topi": "topic",
        "x": 750,
        "y": 1480,
        "wires": [
            [
                "2f5dac90ba372f87",
                "a82ad743b0f91154",
                "11b0c3337f6d2de7"
            ]
        ]
    },
    {
        "id": "39e44bc43bf9c6b8",
        "type": "delay",
        "z": "a53e0c5832346364",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "5",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": false,
        "outputs": 1,
        "x": 580,
        "y": 1480,
        "wires": [
            [
                "8971f669e153b123"
            ]
        ]
    },
    {
        "id": "0bcde84ce588aa43",
        "type": "inject",
        "z": "a53e0c5832346364",
        "name": "Reset 5 min",
        "props": [
            {
                "p": "reset",
                "v": "",
                "vt": "str"
            }
        ],
        "repeat": "300",
        "crontab": "",
        "once": false,
        "onceDelay": "60",
        "topic": "",
        "x": 590,
        "y": 1520,
        "wires": [
            [
                "8971f669e153b123"
            ]
        ]
    },
    {
        "id": "8c76528de7bd48b2",
        "type": "ui_template",
        "z": "a53e0c5832346364",
        "group": "6c31418514176c72",
        "name": "Custom Styling",
        "order": 1,
        "width": 0,
        "height": 0,
        "format": "<style>\n.nr-dashboard-text.red{\n    background-color: red;\n}\n\n.nr-dashboard-text.green{\n    background-color: green;\n}\n</style>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": false,
        "templateScope": "local",
        "className": "",
        "x": 580,
        "y": 1640,
        "wires": [
            []
        ]
    },
    {
        "id": "a90bf22c14461ce5",
        "type": "ui_text",
        "z": "a53e0c5832346364",
        "group": "6c31418514176c72",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Status",
        "format": "{{msg.payload}} {{msg.status}}",
        "layout": "row-center",
        "className": "",
        "x": 1210,
        "y": 1420,
        "wires": []
    },
    {
        "id": "34e7e5889a2bed18",
        "type": "change",
        "z": "a53e0c5832346364",
        "name": "Green",
        "rules": [
            {
                "t": "set",
                "p": "className",
                "pt": "msg",
                "to": "green",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "status",
                "pt": "msg",
                "to": "Normal",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1070,
        "y": 1440,
        "wires": [
            [
                "a90bf22c14461ce5"
            ]
        ]
    },
    {
        "id": "4988f7f3c04fb2c3",
        "type": "change",
        "z": "a53e0c5832346364",
        "name": "Red",
        "rules": [
            {
                "t": "set",
                "p": "className",
                "pt": "msg",
                "to": "red",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "status",
                "pt": "msg",
                "to": "Alerta",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1070,
        "y": 1400,
        "wires": [
            [
                "a90bf22c14461ce5"
            ]
        ]
    },
    {
        "id": "a82ad743b0f91154",
        "type": "trigger",
        "z": "a53e0c5832346364",
        "name": "",
        "op1": " ",
        "op2": " ",
        "op1type": "str",
        "op2type": "str",
        "duration": "15",
        "extend": true,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 2,
        "x": 910,
        "y": 1420,
        "wires": [
            [
                "4988f7f3c04fb2c3"
            ],
            [
                "34e7e5889a2bed18"
            ]
        ]
    },
    {
        "id": "2f5dac90ba372f87",
        "type": "ui_toast",
        "z": "a53e0c5832346364",
        "position": "dialog",
        "displayTime": "3",
        "highlight": "",
        "sendall": true,
        "outputs": 1,
        "ok": "OK",
        "cancel": "Cancel",
        "raw": true,
        "className": "",
        "topic": "Activacion de evento, proceder con la liberacion de la alarma",
        "name": "Pop-UP",
        "x": 880,
        "y": 1560,
        "wires": [
            []
        ]
    },
    {
        "id": "bf3b602d43abd630",
        "type": "delay",
        "z": "a53e0c5832346364",
        "name": "",
        "pauseType": "delay",
        "timeout": "1",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 1310,
        "y": 1520,
        "wires": [
            [
                "b004a5d84cee9331"
            ]
        ]
    },
    {
        "id": "2c264dbf75181401",
        "type": "change",
        "z": "a53e0c5832346364",
        "name": "clear out the table",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "[]",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1320,
        "y": 1480,
        "wires": [
            [
                "b004a5d84cee9331"
            ]
        ]
    },
    {
        "id": "11b0c3337f6d2de7",
        "type": "function",
        "z": "a53e0c5832346364",
        "name": "Tabla de nombres",
        "func": "const lookup = {\n    \"ac233facb540\": \"accelerometer 1\",\n    \"ac233facb541\": \"accelerometer 2\",\n    \"ac233facb542\": \"accelerometer 3\",\n}\n\nmsg.payload.deviceId = lookup[msg.payload.deviceId] || msg.payload.deviceId\n\nreturn msg;\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 970,
        "y": 1500,
        "wires": [
            [
                "7dedeaff123a5b1a"
            ]
        ]
    },
    {
        "id": "b83115b84ecf9aab",
        "type": "inject",
        "z": "a53e0c5832346364",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"deviceId\":\"ac233fabcb7d\",\"deviceIdType\":2,\"timestamp\":1661890462765,\"batteryVoltage\":3.714,\"temperature\":25,\"txCount\":5008270,\"uptime\":19911526000,\"batteryPercentage\":100,\"isButtonPressed\":[true]}",
        "payloadType": "json",
        "x": 410,
        "y": 1420,
        "wires": [
            [
                "39e44bc43bf9c6b8"
            ]
        ]
    },
    {
        "id": "6c31418514176c72",
        "type": "ui_group",
        "name": "Dispositivos",
        "tab": "3f963849c557866a",
        "order": 7,
        "disp": true,
        "width": "20",
        "collapse": false,
        "className": ""
    },
    {
        "id": "3f963849c557866a",
        "type": "ui_tab",
        "name": "Sistema Vimo - Alarmas NCSISTELCORP S.A.",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]
1 Like

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