Global variable: change specific value inside an array entry

hi@all

i'm filling a global variable from a file to set some switch items in a dashboard
reading the value is working and sending to the dashboard item is working as well, but how to CHANGE the value in the global variable after the switch was changed in the dashboard?

snip/snap working:
....
node.send([
{payload: global.get("assets")[i].switch_1},
....

snip/snap not working:
...
global.set("assets["+i+"]".switch_1, msg.payload)
...

i've tried so many different 'syntax styles', none of them were working :frowning:
above it's just one of them

thx 4 help

I would like to help, but I can't see the question.

You have shown a working example. Good.

So.. Now what?

unfortunately no, that will create a new global variable:
"assets[0]" with a value of true
but i need the existing global variable "assets" changed

Well, all you need to do is (kind of) reverse the command.

Rather than set you use the get.

So - for instance:

var x = global.get("assets[0]";

Ok, after posting, I don't think that is what you want either.

again, i want to CHANGE the value when it was changed in the dashboard

here is the content of the file:

[
    {
        "name": "P8",
        "switch_1": false,
        "switch_2": false
    },
    {
        "name": "P9",
        "switch_1": false,
        "switch_2": false
    }
]

I can not resolve the meaning of in the dashboard.

When you enter a value from the dashboard and want to set the global variable to that value?

dashboard is of course irrelevant, it just explains my use case
i just need the correct SYNTAX to SET the value in a global variable inside an ARRAY of OBJECTS

with progress made, that helps, but don't clear it up to a point I can understand.

Then please don't include it in the question.
It only confuses people like me.

Did you try using the change-node for this task?

global.set("assets["+i+"]".switch_1, msg.payload)

I might be wrong, but shouldn´t it be like this:

global.set("assets["+i+"].switch_1", msg.payload)

this creates a new global variable with the name "assets[0].switch_1"

Maybe this helps?

[
    {
        "id": "e5924cbdf8979d8c",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "Change value 1 to 1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "payload": "1",
        "payloadType": "str",
        "x": 4250,
        "y": 1540,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "32ab4c4489e5739e",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "change value 1 to 2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "payload": "2",
        "payloadType": "str",
        "x": 4250,
        "y": 1600,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "0b1582a0c148955c",
        "type": "function",
        "z": "f6c346cb44030a48",
        "name": "",
        "func": "if(msg.topic == 1){\nglobal.set(\"aTestVar[\"+0+\"].value1\",msg.payload)\n}\nif(msg.topic == 2){\nglobal.set(\"aTestVar[\"+0+\"].value2\",msg.payload)   \n}\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 4560,
        "y": 1620,
        "wires": [
            []
        ]
    },
    {
        "id": "e4f3e83ec7996167",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "Change value 2 to 1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "1",
        "payloadType": "str",
        "x": 4250,
        "y": 1660,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    },
    {
        "id": "216df17272664a45",
        "type": "inject",
        "z": "f6c346cb44030a48",
        "name": "change value 2 to 2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "2",
        "payloadType": "str",
        "x": 4250,
        "y": 1720,
        "wires": [
            [
                "0b1582a0c148955c"
            ]
        ]
    }
]

Thanks a lot Nico

global.set("assets["+i+"].switch_1", msg.payload)
is working in all Node-red versions i tried now in a docker env
my "Node-red" is a bit special (cloud based)

Generally I suggest trying to avoid syntax like that. As you have found it just causes heartache. Instead I suggest accessing the whole object. It is often more efficient and much simpler

assets = global.get("assets")
assets[i].switch_1 = msg.payload
global.set("assets",assets)

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