Context arrays/objects - stuck

I've done a bit of searching when starting the post and looked at a couple of suggestions.
Sorry. I can't see them as answers.

Code:

let value = msg.payload;
let red = flow.get("red");
let led = flow.get("led") || 0;
red[led] = value;
flow.set("red",red[]);

msg.payload if the value I want to store.
flow "red" is the array (object?) to which I want to save.

I probably have this back to front how I am doing it.
But I don't see anything wrong with this way, other than it can be done other ways.

8 LEDs.
A slider selected which LED.

Another 3 sliders that set the RGB value.

I slide the LED selector to which LED.
I then slide the RGB sliders to set the value.
I move to another LED and .... what ever.

I come back to this LED and it gets the stored values.

But I'm stuck how to store them in flow context.

let value = msg.payload;
let red = flow.get("red") || [];   // to be sure...
let led = flow.get("led") || 0;
red[led] = value;
flow.set("red",red);    // <--- it's just 'red' here...
1 Like

Ok, so I've expanded the code you posted and got this.

It is working.

const colour = "green";


let value = msg.payload;
let colourarray = flow.get(colour) || [];   // to be sure...
node.warn(colour + " " + colourarray);
let led = flow.get("led") || 0;
node.warn("LED " + led);
colourarray[led] = value;
node.warn("Value " + value);
flow.set(colour, colourarray);    // <--- it's just 'red' here...

return msg;

Then I repeat it for RED, GREEN and BLUE by just changing the name in the first line.

Perhaps it would be possible even to pass color & led via msg ... like msg.color and msg.led? That could save you the detour via the (flow) context storage...

Yeah, I am having a dog's breakfast time getting the messages to work.

I don't know a trick to save/get the values as I have it.

This is my effort at doing it.

The joining is not working - yet.

[{"id":"17dbb7a79d452473","type":"ui_slider","z":"aad05e94.e66068","name":"Led #","label":"LED #","tooltip":"","group":"63062421.073624","order":8,"width":"1","height":"4","passthru":true,"outs":"end","topic":"","topicType":"str","min":"0","max":"8","step":1,"className":"","x":370,"y":420,"wires":[["88c920636f44c4a2","87bb22d45d50f084","f631cfdc783f4675","37220ad3c699cef9"]]},{"id":"88c920636f44c4a2","type":"change","z":"aad05e94.e66068","name":"LED","rules":[{"t":"set","p":"led","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":420,"wires":[[]]},{"id":"87bb22d45d50f084","type":"function","z":"aad05e94.e66068","name":"Get value","func":"let led = msg.payload;\nlet colour = flow.get(\"blue\");\nlet value = colour[led] || 0;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":340,"wires":[["24bd77c7.2dddc"]]},{"id":"f631cfdc783f4675","type":"function","z":"aad05e94.e66068","name":"Get value","func":"let led = msg.payload;\nlet colour = flow.get(\"green\");\nlet value = colour[led] || 0;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":250,"wires":[["8f3b1f2f.9e01c"]]},{"id":"37220ad3c699cef9","type":"function","z":"aad05e94.e66068","name":"Get value","func":"let led = msg.payload;\nlet colour = flow.get(\"red\");\nlet value = colour[led] || 0;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":160,"y":160,"wires":[["ecf6cee4.39079","afbcc2d62aa85d4b"]]},{"id":"ecf6cee4.39079","type":"ui_slider","z":"aad05e94.e66068","name":"Red","label":"Red","tooltip":"","group":"63062421.073624","order":2,"width":"1","height":"6","passthru":true,"outs":"end","topic":"","topicType":"str","min":"1","max":"255","step":1,"className":"","x":370,"y":160,"wires":[["0fc7261e9ef75ad6","b6e196cfdabe0b9f","39c06764.4def"]]},{"id":"8f3b1f2f.9e01c","type":"ui_slider","z":"aad05e94.e66068","name":"Green","label":"Green","tooltip":"","group":"63062421.073624","order":3,"width":"1","height":"6","passthru":true,"outs":"end","topic":"","min":0,"max":"255","step":1,"x":370,"y":250,"wires":[["f1af2a73.5f669","f897fd3f34685f74"]]},{"id":"24bd77c7.2dddc","type":"ui_slider","z":"aad05e94.e66068","name":"Blue","label":"Blue","tooltip":"","group":"63062421.073624","order":4,"width":"1","height":"6","passthru":true,"outs":"end","topic":"","topicType":"str","min":"1","max":"255","step":1,"className":"","x":370,"y":340,"wires":[["ce0753a5.23b75","b2092751f72b5254"]]},{"id":"b2092751f72b5254","type":"function","z":"aad05e94.e66068","name":"function 23","func":"const colour = \"blue\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg.blue = value;\nmsg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":340,"wires":[["7d60d18efd5768f4","604d314712cea41f"]]},{"id":"f897fd3f34685f74","type":"function","z":"aad05e94.e66068","name":"function 22","func":"const colour = \"green\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg.green = value;\nmsg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":250,"wires":[["7d60d18efd5768f4","02072a97ee82277e"]]},{"id":"b6e196cfdabe0b9f","type":"function","z":"aad05e94.e66068","name":"function 21","func":"const colour = \"red\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg.red = value;\nmsg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":160,"wires":[["51c7bfa856021aca","7d60d18efd5768f4"]]},{"id":"63062421.073624","type":"ui_group","name":"LED colour adjustments","tab":"e92ac76d.c24e7","order":10,"disp":true,"width":"7","collapse":false},{"id":"e92ac76d.c24e7","type":"ui_tab","name":"Real_World_Control","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

Sorry.

There are 4 sliders.

(I don't know why I punish myself like this.)

Oh, ok, just before I post:
The 3 function nodes.
(I'm trying to do the right thing with variable names.)
But I seem to be missing something.

const colour = "blue";     //  This sets which colour is being done.


let value = msg.payload;
let colourarray = flow.get(colour) || [];   // to be sure...
//node.warn(colour + " " + colourarray);
let led = flow.get("led") || 0;
//node.warn("LED " + led);
colourarray[led] = value;
//node.warn("Value " + value);
flow.set(colour, colourarray);    // <--- it's just 'red' here...

//    How do I use colour here though?   Rather than saying BLUE how can I use the
//    variable name?   That way I only change the one line for all 3 colours.
msg.blue = value;
msg.payload = "";
return msg;

Change

to

msg[color] = value;
1 Like

{{THANKS}}

I am really redefining my stupidity today.

I think I'm going for a new record. :wink:

(Really pushing the friendship.)

Now I'm stuck with how to join the three messages.
Other than a join node that's about the best way to create the message with the structure:
rgb,led,red.green,blue from the flow contexts stored.

If the different values arrive one by one (like in the flow you posted above), there's little more option than to go via a (flow) context store ... & join the data.

Where's this data coming from in reality?
How's you flow looking so far?

The flow (the other one) is looking good.

This one:
The data does come from the sliders set.
(Well.... Kind of.)
The sliders set the flow context. Then the message goes down to a join node.
That gets the 3 messages (RGB) and joins them together to make the message to send to the LED.

I just don't understand the terms to describe the types of messages to create from the join.

I'm making slow progress though. but am open to help with what type of message I want to create coming out of the join node.
It really needs to be text.

Update:

Ok. I NEARLY have it working now.

:frowning:

No. Sorry, I am missing something.

Here's what I have.

Excuse the mess. It is very much Work In Progress.

[{"id":"35372c65.4d2b44","type":"inject","z":"aad05e94.e66068","name":"R","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":850,"y":40,"wires":[["b6e196cfdabe0b9f"]]},{"id":"0e626f214207980d","type":"inject","z":"aad05e94.e66068","name":"G","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":850,"y":80,"wires":[["f897fd3f34685f74"]]},{"id":"a6471d5af6829c96","type":"inject","z":"aad05e94.e66068","name":"B","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":850,"y":120,"wires":[["b2092751f72b5254"]]},{"id":"b6e196cfdabe0b9f","type":"function","z":"aad05e94.e66068","name":"function 21","func":"const colour = \"red\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg[colour] = value;\nmsg.led = led;\nmsg.payload = \"\";\nmsg.count = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":160,"wires":[["7d60d18efd5768f4"]]},{"id":"f897fd3f34685f74","type":"function","z":"aad05e94.e66068","name":"function 22","func":"const colour = \"green\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg[colour] = value;\nmsg.led = led;\nmsg.payload = \"\";\nmsg.count = 2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":250,"wires":[["7d60d18efd5768f4"]]},{"id":"b2092751f72b5254","type":"function","z":"aad05e94.e66068","name":"function 23","func":"const colour = \"blue\";\n\n\nlet value = msg.payload;\nlet colourarray = flow.get(colour) || [];   // to be sure...\n//node.warn(colour + \" \" + colourarray);\nlet led = flow.get(\"led\") || 0;\n//node.warn(\"LED \" + led);\ncolourarray[led] = value;\n//node.warn(\"Value \" + value);\nflow.set(colour, colourarray);    // <--- it's just 'red' here...\n\nmsg[colour] = value;\nmsg.led = led;\nmsg.payload = \"\";\nmsg.count = 3;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":340,"wires":[["7d60d18efd5768f4"]]},{"id":"80710d1c90418e74","type":"function","z":"aad05e94.e66068","name":"check led is set.","func":"let led = flow.get(\"led\");\nif (led == undefined)\n{\n    led = 0;\n}\nmsg.payload = led;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":815,"y":250,"wires":[["5bcad49e022276f6","70ad4dc8f6934651","b1261f7c8bbfd9ec","ed98e13865cb8bd3"]],"l":false},{"id":"7d60d18efd5768f4","type":"junction","z":"aad05e94.e66068","x":740,"y":250,"wires":[["80710d1c90418e74"]]},{"id":"5bcad49e022276f6","type":"join","z":"aad05e94.e66068","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":960,"y":250,"wires":[["b8fa5a143c8d46e0"]]},{"id":"b8fa5a143c8d46e0","type":"function","z":"aad05e94.e66068","name":"Get LED","func":"msg.led = flow.get(\"led\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1045,"y":250,"wires":[["10cea6927ad0f342"]],"l":false},{"id":"10cea6927ad0f342","type":"template","z":"aad05e94.e66068","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"rgb,{{led}},{{payload}}","output":"str","x":1180,"y":250,"wires":[["b3889d2734bf3fd4","5519e5f0.fe798c"]]},{"id":"5519e5f0.fe798c","type":"debug","z":"aad05e94.e66068","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1160,"y":200,"wires":[]},{"id":"70ad4dc8f6934651","type":"switch","z":"aad05e94.e66068","name":"","property":"red","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":1055,"y":70,"wires":[["51c7bfa856021aca"]],"l":false},{"id":"51c7bfa856021aca","type":"debug","z":"aad05e94.e66068","name":"RED sent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":70,"wires":[]},{"id":"b1261f7c8bbfd9ec","type":"switch","z":"aad05e94.e66068","name":"","property":"green","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":1055,"y":110,"wires":[["02072a97ee82277e"]],"l":false},{"id":"02072a97ee82277e","type":"debug","z":"aad05e94.e66068","name":"GREEN sent","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1160,"y":110,"wires":[]},{"id":"ed98e13865cb8bd3","type":"switch","z":"aad05e94.e66068","name":"","property":"blue","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":1055,"y":150,"wires":[["604d314712cea41f"]],"l":false},{"id":"604d314712cea41f","type":"debug","z":"aad05e94.e66068","name":"BLUE sent","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1160,"y":150,"wires":[]}]

The unnamed debug is the important one.

But it is not getting the stored values.
It is getting the LED value for the RGB values.

Stuck how I fix this.

Oh, you may need to set flow context.

Piccie may help

Once you utilize a function node, there should be no need to make it that complicated:

[
    {
        "id": "1102b9a47de6f467",
        "type": "tab",
        "label": "Flow 25",
        "disabled": false,
        "info": "",
        "env": [],
        "_mcu": {
            "mcu": false
        }
    },
    {
        "id": "35372c65.4d2b44",
        "type": "inject",
        "z": "1102b9a47de6f467",
        "name": "R",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "_mcu": {
            "mcu": false
        },
        "x": 110,
        "y": 180,
        "wires": [
            [
                "c82221dd5aa252d3"
            ]
        ]
    },
    {
        "id": "0e626f214207980d",
        "type": "inject",
        "z": "1102b9a47de6f467",
        "name": "G",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2",
        "payloadType": "num",
        "_mcu": {
            "mcu": false
        },
        "x": 110,
        "y": 240,
        "wires": [
            [
                "87aa021b210538b4"
            ]
        ]
    },
    {
        "id": "a6471d5af6829c96",
        "type": "inject",
        "z": "1102b9a47de6f467",
        "name": "B",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "3",
        "payloadType": "num",
        "_mcu": {
            "mcu": false
        },
        "x": 110,
        "y": 300,
        "wires": [
            [
                "31cf07bd46e0efdd"
            ]
        ]
    },
    {
        "id": "80710d1c90418e74",
        "type": "function",
        "z": "1102b9a47de6f467",
        "name": "make join",
        "func": "const led = flow.get(\"led\") || 0;\nconst red = flow.get(\"red\") || 0;\nconst green = flow.get(\"green\") || 0;\nconst blue = flow.get(\"blue\") || 0;\n\nlet p = `rgb, ${led}, ${red}, ${green}, ${blue}`;\n\nmsg.payload = p;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "_mcu": {
            "mcu": false
        },
        "x": 520,
        "y": 240,
        "wires": [
            [
                "5519e5f0.fe798c"
            ]
        ]
    },
    {
        "id": "5519e5f0.fe798c",
        "type": "debug",
        "z": "1102b9a47de6f467",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 710,
        "y": 240,
        "wires": []
    },
    {
        "id": "c82221dd5aa252d3",
        "type": "change",
        "z": "1102b9a47de6f467",
        "name": "red",
        "rules": [
            {
                "t": "set",
                "p": "red",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "_mcu": {
            "mcu": false
        },
        "x": 310,
        "y": 180,
        "wires": [
            [
                "80710d1c90418e74"
            ]
        ]
    },
    {
        "id": "87aa021b210538b4",
        "type": "change",
        "z": "1102b9a47de6f467",
        "name": "green",
        "rules": [
            {
                "t": "set",
                "p": "green",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "_mcu": {
            "mcu": false
        },
        "x": 290,
        "y": 240,
        "wires": [
            [
                "80710d1c90418e74"
            ]
        ]
    },
    {
        "id": "31cf07bd46e0efdd",
        "type": "change",
        "z": "1102b9a47de6f467",
        "name": "blue",
        "rules": [
            {
                "t": "set",
                "p": "blue",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "_mcu": {
            "mcu": false
        },
        "x": 310,
        "y": 300,
        "wires": [
            [
                "80710d1c90418e74"
            ]
        ]
    }
]
1 Like

(Sorry, I was eating.)

Ok.

So..... but the red, green and blue are arrays in context. But that is a different level of the things working.

I'm not seeing how it works just now.

I'll import it and see if I can make more sense of it.

I fear something was lost in translation.

Each time I pressed the inject node/s I get these weird outputs.

Concentrating on the function node:

let led = flow.get("led");
if (led == undefined)
{
    led = 8;
}

//==
let red = flow.get("red");
let redvalue = red[led] || 0;

let green = flow.get("green");
let greenvalue = green[led] || 0;

let blue = flow.get("blue");
let bluevalue = blue[led] || 0;
//==


//const redx = flow.get("red") || 0;
//const greenx = flow.get("green") || 0;
//const bluex = flow.get("blue") || 0;

let p = `rgb, ${led}, ${redvalue}, ${greenvalue}, ${bluevalue}`;

msg.payload = p;
return msg;

Better?

Worked it out.

Got it working and did a bit of other tweaking.
Found a few problems too and fixed them. :slight_smile:
Thanks again for the help and time.

1 Like

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