Elegant way to build the code visually

Folks,

I would like some help, please.

I have the following code below:

//Assign to a variable for the expression to be small.
var pMaxima = global.get("pMaxima");
var varSupervisor = global.get("varSupervisor");
var varMergulhador01 = global.get("varMergulhador01");
var varMergulhador02 = global.get("varMergulhador02");
var varMergulhador03 = global.get("varMergulhador03");
var tempoTNR = global.get("tempoTNR");
var tempoM = global.get("tempoM");
var tempoTTF = global.get("tempoTTF");

var vNumRom = global.get("varNumROM");
var vNumMergulho = global.get("varNumMergulho");
var vEmbarcacao = global.get("varEmbarcacao");
var vLocal = global.get("varLocal");

//--------COMEÇA UMA CONFERENCIA SE OS DADOS FORAM PLANEJADOS
    var varPlanFeito = global.get("prepDados");
    if (pMaxima == '') {varPlanFeito = false };
    if (varSupervisor == '') { varPlanFeito = false };
    if (varMergulhador01 == '') { varPlanFeito = false };
    if (varMergulhador02 == '') { varPlanFeito = false };
    if (varMergulhador03 == '') { varPlanFeito = false };
    if (tempoM == '') { varPlanFeito = false };
    if (tempoTTF == '') { varPlanFeito = false };

// ---PERGUNTAR PARA O CLIENTE SE ESSES DADOS SÃO OPCIONAIS -------------//
    if (vNumRom == '') { varPlanFeito = false };
    if (vNumMergulho == '') { varPlanFeito = false };
    if (vEmbarcacao == '') { varPlanFeito = false };
    if (vLocal == '') { varPlanFeito = false };

node.warn(varPlanFeito);    

if (varPlanFeito) { global.set("prepDados", true) } else { global.set("prepDados", false) }


msg.column_name = ["Supervisor", "Mergulhador01", "Mergulhador02", "Mergulhador03", "ProfundidadeMaxima_plan", "TNR_plan", "TempoMergulho_plan", "TTF_plan", "NumROM", "NumMergulho", "Embarcacao", "Local"];

msg.column_sort = [varSupervisor, varMergulhador01, varMergulhador02, varMergulhador03, pMaxima, tempoTNR, tempoM, tempoTTF, vNumRom, vNumMergulho, vEmbarcacao, vLocal];

msg.database = "Mergulho";

msg.command = "insert";

But I would like to build it visually.

Because I'm training my personal ability to do everything no-code!!

I have 11 global variables that I need to check if the user has filled it in or not...

And I can already do this efficiently within a function.

But how I want to get wild in NODE-RED and be totally no-CODE.

What is the suggestion to do this completely with the NODES?

Change nodes can be used to get or set your global vars, and switch nodes can do the "IF" logic.

Not sure it would be elegant however, I would most likley do this sort of thing in a function node as well.

would also use

ìf(exp){set var};
else if (exp){set var}

As you only need 1 false if statement, once you have one false there is no need to check any more.
Or
use

if (exp || exp || exp){ set value false}

no other if's needed
[edit]
To OG Topic, Change nodes and switch nodes.
Elegance is in the eye of the beholder here though.

What do you think of this flow?

It's working logically as I want.

But is it efficient? Under the optics of processing?

[{"id":"cb71584bfa6ceb7e","type":"inject","z":"eec9ba25159a523e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"<Font Size=5 color=red><b>MERGULHO NÃO PLANEJADO","payloadType":"str","x":130,"y":140,"wires":[["f500769d445c3af2"]]},{"id":"4ec7f41d24532bca","type":"switch","z":"eec9ba25159a523e","name":"varSupervisor","property":"varSupervisor","propertyType":"global","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":660,"y":200,"wires":[["c096ac3fe9bb3cf6"],["9408f31c36607d12"]]},{"id":"f500769d445c3af2","type":"change","z":"eec9ba25159a523e","name":"PrepDados = True","rules":[{"t":"set","p":"prepDados","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":200,"wires":[["4ec7f41d24532bca","16976c99a67360d2","0ce0a17f22edfefc","40c71817087b5060"]]},{"id":"9408f31c36607d12","type":"change","z":"eec9ba25159a523e","name":"PrepDados = False","rules":[{"t":"set","p":"prepDados","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":200,"wires":[["4ec7f41d24532bca"]]},{"id":"c096ac3fe9bb3cf6","type":"change","z":"eec9ba25159a523e","name":"PrepDados = true","rules":[{"t":"set","p":"prepDados","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":140,"wires":[["4ec7f41d24532bca"]]},{"id":"16976c99a67360d2","type":"switch","z":"eec9ba25159a523e","name":"varMergulhador01","property":"varMergulhador01","propertyType":"global","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":320,"wires":[["e884ef4d7d61c2f9"],["6442f97925ce71d1"]]},{"id":"6442f97925ce71d1","type":"change","z":"eec9ba25159a523e","name":"PrepDados = False","rules":[{"t":"set","p":"prepDados","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":320,"wires":[["16976c99a67360d2"]]},{"id":"e884ef4d7d61c2f9","type":"change","z":"eec9ba25159a523e","name":"PrepDados = true","rules":[{"t":"set","p":"prepDados","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":260,"wires":[["16976c99a67360d2"]]},{"id":"0ce0a17f22edfefc","type":"switch","z":"eec9ba25159a523e","name":"varMergulhador02","property":"varMergulhador02","propertyType":"global","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":440,"wires":[["336f2a15be7f728d"],["2d99da08f9ed7dde"]]},{"id":"2d99da08f9ed7dde","type":"change","z":"eec9ba25159a523e","name":"PrepDados = False","rules":[{"t":"set","p":"prepDados","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":440,"wires":[["0ce0a17f22edfefc"]]},{"id":"336f2a15be7f728d","type":"change","z":"eec9ba25159a523e","name":"PrepDados = true","rules":[{"t":"set","p":"prepDados","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":380,"wires":[["0ce0a17f22edfefc"]]},{"id":"40c71817087b5060","type":"switch","z":"eec9ba25159a523e","name":"varMergulhador03","property":"varMergulhador03","propertyType":"global","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":560,"wires":[["096086713eb4d12a"],["0182690bf21e77fc"]]},{"id":"0182690bf21e77fc","type":"change","z":"eec9ba25159a523e","name":"PrepDados = False","rules":[{"t":"set","p":"prepDados","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":560,"wires":[["40c71817087b5060"]]},{"id":"096086713eb4d12a","type":"change","z":"eec9ba25159a523e","name":"PrepDados = true","rules":[{"t":"set","p":"prepDados","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":500,"wires":[["40c71817087b5060"]]}]

If you cut down to just one global variable like this

{
    "pMaxima": "OK",
    "varSupervisor": "OK",
    "varMergulhador01": "OK",
    "varMergulhador02": "OK",
    "varMergulhador03": "OK",
    "tempoTNR": "OK",
    "tempoM": "OK",
    "tempoTTF": "OK",
    "varNumROM": '',        // <<==  this one is empty
    "varNumMergulho": "OK",
    "varEmbarcacao": "OK",
    "varLocal": "OK"
}

How about this flow?

[{"id":"2bfb980fd0fd7c30","type":"function","z":"44598357780bb6ab","name":"function 29","func":"const allok = {\n    \"pMaxima\": \"OK\",\n    \"varSupervisor\": \"OK\",\n    \"varMergulhador01\": \"OK\",\n    \"varMergulhador02\": \"OK\",\n    \"varMergulhador03\": \"OK\",\n    \"tempoTNR\": \"OK\",\n    \"tempoM\": \"OK\",\n    \"tempoTTF\": \"OK\",\n    \"varNumROM\": \"OK\",\n    \"varNumMergulho\": \"OK\",\n    \"varEmbarcacao\": \"OK\",\n    \"varLocal\": \"OK\"\n}\n\nflow.set(\"myvar\", allok);\nreturn(msg)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":300,"wires":[["4230b21c1ad42682"]]},{"id":"128d151968fb5a7c","type":"inject","z":"44598357780bb6ab","name":"All OK","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":300,"wires":[["2bfb980fd0fd7c30"]]},{"id":"c74d7222235f968a","type":"debug","z":"44598357780bb6ab","name":"debug 248","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.key","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":360,"wires":[]},{"id":"be35bf29832c4748","type":"inject","z":"44598357780bb6ab","name":"Not All OK","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":420,"wires":[["43e2b1c930627028"]]},{"id":"43e2b1c930627028","type":"function","z":"44598357780bb6ab","name":"function 30","func":"const notallok = {\n    \"pMaxima\": \"OK\",\n    \"varSupervisor\": \"OK\",\n    \"varMergulhador01\": \"OK\",\n    \"varMergulhador02\": \"OK\",\n    \"varMergulhador03\": \"OK\",\n    \"tempoTNR\": \"OK\",\n    \"tempoM\": \"OK\",\n    \"tempoTTF\": \"OK\",\n    \"varNumROM\": '',        // <<==  this one is empty\n    \"varNumMergulho\": \"OK\",\n    \"varEmbarcacao\": \"OK\",\n    \"varLocal\": \"OK\"\n}\n\nflow.set(\"myvar\", notallok);\nreturn(msg)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":420,"wires":[["4230b21c1ad42682"]]},{"id":"4230b21c1ad42682","type":"change","z":"44598357780bb6ab","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"myvar","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":140,"y":360,"wires":[["30b3d36b7dbf8ba8"]]},{"id":"30b3d36b7dbf8ba8","type":"split","z":"44598357780bb6ab","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":290,"y":360,"wires":[["610c851e3c923db5"]]},{"id":"610c851e3c923db5","type":"switch","z":"44598357780bb6ab","name":"","property":"payload","propertyType":"msg","rules":[{"t":"empty"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":360,"wires":[["c74d7222235f968a"]]},{"id":"1fbf2f828f7d5519","type":"comment","z":"44598357780bb6ab","name":"Functions only to set up sample data","info":"","x":180,"y":260,"wires":[]}]

If none of the elements of the variable are empty, it produces nothing.
If one or more is empty, it prints the name of those elements
Untitled 2

Seem fraught with infinite loop possibilities.

You could try something like this, obviously you would need to add more to change and switch nodes.

[{"id":"ee8118560cd12e23","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"one","pt":"msg","to":"one","tot":"flow"},{"t":"set","p":"two","pt":"msg","to":"two","tot":"flow"},{"t":"set","p":"three","pt":"msg","to":"three","tot":"flow"},{"t":"set","p":"can_be_global","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":3980,"wires":[["133beb3faecb8545"]]},{"id":"d556411065ad6217","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":3980,"wires":[["ee8118560cd12e23"]]},{"id":"133beb3faecb8545","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"\"\"","propertyType":"jsonata","rules":[{"t":"eq","v":"one","vt":"msg"},{"t":"eq","v":"two","vt":"msg"},{"t":"eq","v":"three","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":4,"x":510,"y":3980,"wires":[["97daa321fd4fc6f6"],["97daa321fd4fc6f6"],["97daa321fd4fc6f6"],["3cd5b9022baea3f5"]]},{"id":"97daa321fd4fc6f6","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"can_be_global","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":3960,"wires":[["3cd5b9022baea3f5"]]},{"id":"3cd5b9022baea3f5","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 227","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"can_be_global","statusType":"msg","x":930,"y":3960,"wires":[]},{"id":"5c8402c38d16ee00","type":"inject","z":"da8a6ef0b3c9a5c8","name":"set none to \"\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"test\",\"tester\",\"testing\"]","payloadType":"json","x":170,"y":3860,"wires":[["a30bbe0dae154200"]]},{"id":"e59f0480cd6b3d88","type":"inject","z":"da8a6ef0b3c9a5c8","name":"set one to \"\"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"test\",\"\",\"testing\"]","payloadType":"json","x":150,"y":3900,"wires":[["a30bbe0dae154200"]]},{"id":"a30bbe0dae154200","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"one","pt":"flow","to":"payload[0]","tot":"msg"},{"t":"set","p":"two","pt":"flow","to":"payload[1]","tot":"msg"},{"t":"set","p":"three","pt":"flow","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":3860,"wires":[[]]}]

This solution turned out to be very elegant.

More how to avoid filling the array via code.

Goal is zero to use the node function.

Because we're trying to change our mindset to something totally codeless.

I need something that makes an infinite loop because if these variables are not filled, it needs to appear on the screen. A message:

Type fill in the fields.

Missing to fill in the fields.

I think goal No 1 should be "Don't use so many context variables".
But goal No 2 "Don't use so many functions" is good too. :grinning:

I use global variables because the fields are filled in different screens and different flows...

And these variables are used at various times in the system.

I really liked this flow, it is also an interesting possibility.

I was just wondering how to do this in an infinite loop.

I really do not see why you need an infinite loop, definitely not the node-red way and as said fraught with dangers.

There must be a way of updating the dashboard when the value changes. Mystery to me as I don't really know the workings of your full flow.

I have to think of something to run this conference.

Without keeping this infinite loop.

You're right, this can be dangerous in terms of processing.

here is a full visual example of your function, I cheated and created sort arry using JSONata, but you could just manually and a list of rules setting each payload property to a sort array element.

[{"id":"97daa321fd4fc6f6","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"pass","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":3960,"wires":[["278ff4e44ff46b06"]]},{"id":"133beb3faecb8545","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"\"\"","propertyType":"jsonata","rules":[{"t":"eq","v":"payload.one","vt":"msg"},{"t":"eq","v":"payload.two","vt":"msg"},{"t":"eq","v":"payload.three","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":4,"x":510,"y":3980,"wires":[["97daa321fd4fc6f6"],["97daa321fd4fc6f6"],["97daa321fd4fc6f6"],["278ff4e44ff46b06"]]},{"id":"278ff4e44ff46b06","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"pass","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":830,"y":4000,"wires":[["e433a4ba07312f48"],["19cdd0789ed9c47f"]]},{"id":"ee8118560cd12e23","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.one","pt":"msg","to":"one","tot":"flow"},{"t":"set","p":"payload.two","pt":"msg","to":"two","tot":"flow"},{"t":"set","p":"payload.three","pt":"msg","to":"three","tot":"flow"},{"t":"set","p":"pass","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":3980,"wires":[["133beb3faecb8545"]]},{"id":"e433a4ba07312f48","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"var","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":3980,"wires":[["eb173d77a4bdd506"]]},{"id":"19cdd0789ed9c47f","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"var","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":4020,"wires":[["eb173d77a4bdd506"]]},{"id":"d556411065ad6217","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":3980,"wires":[["ee8118560cd12e23"]]},{"id":"eb173d77a4bdd506","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"column_name","pt":"msg","to":"[\"one\",\"two\",\"three\"]","tot":"json"},{"t":"set","p":"column_sort","pt":"msg","to":"$$.payload.*","tot":"jsonata"},{"t":"set","p":"database","pt":"msg","to":"database1","tot":"str"},{"t":"set","p":"command","pt":"msg","to":"insert","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1160,"y":4000,"wires":[["3cd5b9022baea3f5"]]},{"id":"3cd5b9022baea3f5","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 227","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"$flowContext(\"var\")","statusType":"jsonata","x":710,"y":4080,"wires":[]}]
1 Like

Now yes, I found real logic with no-code.

VERY VERY THANK YOU

1 Like

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