`on start` tab confused how to use

Folks, I'm writing/debugging a subflow. (My favorite thing)

But I'm stuck on the on start tab.

Reading a link I see this as an example:

if (context.get("counter") === undefined) {
    context.set("counter", 0)
}

Which is fair enough, but alas it doesn't quite cover my needs.

Subflows invariable (ok, maybe not) need variables.
So I'm trying to set up the function node to read the variables and use them.

If they aren't set, I want it to use default values.

So....

// Code added here will be run once
// whenever the node is started.
if (env.get("topic") == undefined)
{
    context.set("topic","topic");
}

Is ideally what I want to do, but this opens a can of worms what if env.get("topic") IS set?

Without posting the whole subflow - which I don't mind doing - what is the way out for me so if the topic isn't defined it is set to .... a default?
Thanks in advance.

Hi @Trying_to_learn

I may have this wrong, but can you not do in your on start?:

const value = env.get("topic") || "Your Default"
context.set("topic", value);

This way context.topic is iether

  • What was provided OR
  • A default that you decide

Well, what I posted won't work because it is setting a context depending on an env variable.

Which leaves the stated problem that if I test if the env is (or not) set, and then set the context the code in the main part of the node won't work correctly.

I'll give that a go though as I am not getting the result I want.

Sorry, I just got caught with this 2-3 hours ago when something else went wrong and I was debugging it.

I seem to live in a world of worms (from open cans) and am trying to get this one resolved ASAP.

I'll post back soon.

ALAS

No, sorry that doesn't work.

This is what I get:

Blank (empty) topic.
Not Your default. I left it as that only for testing.
But it doesn't seem to work.

Mind if I post the subflow?

This is the subflow as is at this date:

[{"id":"5b2f1c1e34d60f19","type":"subflow","name":"3 Press button","info":"2024 01 07\n==\n\nHuge bug found and fixed.\n\nStill have a bug with code if the\n`topic` is not set.\n\nBut now better than what it was.","category":"","in":[{"x":70,"y":190,"wires":[{"id":"ec5080f153f43d22"}]}],"out":[{"x":840,"y":190,"wires":[{"id":"ee1c20a8875e36cc","port":0}]},{"x":840,"y":270,"wires":[{"id":"10d2bd1bfc555db3","port":0}]}],"env":[{"name":"startat","type":"num","value":"","ui":{"label":{"en-US":"start at"},"type":"input","opts":{"types":["num"]}}},{"name":"hold","type":"bool","value":"true","ui":{"type":"input","opts":{"types":["bool"]}}},{"name":"topic","type":"str","value":"","ui":{"type":"input","opts":{"types":["str"]}}},{"name":"Message 1","type":"str","value":""},{"name":"Message 2","type":"str","value":""},{"name":"Message 3","type":"str","value":""},{"name":"colour 1","type":"str","value":""},{"name":"colour 2","type":"str","value":""},{"name":"colour 3","type":"str","value":""},{"name":"fontclr 1","type":"str","value":""},{"name":"fontclr 2","type":"str","value":""},{"name":"fontclr 3","type":"str","value":""}],"meta":{},"color":"#DDAA99","inputLabels":["Input"],"outputLabels":["Output","Loop back to button node"],"icon":"node-red-dashboard/ui_button.png"},{"id":"9c628490f5a7ab36","type":"switch","z":"5b2f1c1e34d60f19","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":230,"y":270,"wires":[["625edd1859414ac8"],["2ed89392aefd3ac3"],["75984bf53702c1ac"]]},{"id":"625edd1859414ac8","type":"change","z":"5b2f1c1e34d60f19","name":"1","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 1","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 1","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 1","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":230,"wires":[["10d2bd1bfc555db3"]]},{"id":"2ed89392aefd3ac3","type":"change","z":"5b2f1c1e34d60f19","name":"2","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 2","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 2","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 2","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":270,"wires":[["10d2bd1bfc555db3"]]},{"id":"75984bf53702c1ac","type":"change","z":"5b2f1c1e34d60f19","name":"3","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 3","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 3","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 3","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":310,"wires":[["10d2bd1bfc555db3"]]},{"id":"ec5080f153f43d22","type":"function","z":"5b2f1c1e34d60f19","name":"","func":"node.warn(\"ENV topic \" + env.get(\"topic\"));\nnode.warn(\"context topic \" + context.get(\"topic\"));\n\n//  Get \"start value\"\nif (msg.topic == \"startup\")\n{\n    context.set(\"previous\",msg.payload-1);\n}\n\nif (msg.payload == \"reset\")\n{\n    context.set(\"previous\",0);\n    return;\n}\n\nvar max = 4;\nvar previous = context.get(\"previous\") || 0;\nprevious = (previous + 1) % max;\ncontext.set(\"previous\",previous);\n\nif (previous == 0)\n{\n    previous = 1;\n    context.set(\"previous\",1);\n}\nmsg.payload = previous;\nmsg.topic = env.get(\"topic\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nconst value = env.get(\"topic\") || \"Your Default\"\ncontext.set(\"topic\", value);\n","finalize":"","libs":[],"x":390,"y":190,"wires":[["9c628490f5a7ab36","ee1c20a8875e36cc"]]},{"id":"ee1c20a8875e36cc","type":"trigger","z":"5b2f1c1e34d60f19","name":"Delay","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"350","extend":true,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":570,"y":190,"wires":[["20347974efa46acb"]]},{"id":"20347974efa46acb","type":"change","z":"5b2f1c1e34d60f19","name":"$hold","rules":[{"t":"set","p":"hold","pt":"msg","to":"hold","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":535,"y":150,"wires":[["52f023f6a52afa5b"]],"l":false},{"id":"52f023f6a52afa5b","type":"switch","z":"5b2f1c1e34d60f19","name":"if FALSE","property":"hold","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":585,"y":150,"wires":[["7d8c55ef2950c72a"]],"l":false},{"id":"7d8c55ef2950c72a","type":"change","z":"5b2f1c1e34d60f19","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":635,"y":150,"wires":[["ec5080f153f43d22"]],"l":false},{"id":"10d2bd1bfc555db3","type":"junction","z":"5b2f1c1e34d60f19","x":550,"y":270,"wires":[[]]},{"id":"d87556ee78f63d9c","type":"subflow:5b2f1c1e34d60f19","z":"88de1ac2.8b8c38","name":"","env":[{"name":"startat","value":"0","type":"num"},{"name":"Message 1","value":"ALL","type":"str"},{"name":"Message 2","value":"BLOCK","type":"str"},{"name":"Message 3","value":"ONE","type":"str"},{"name":"colour 1","value":"limegreen","type":"str"},{"name":"colour 2","value":"violet","type":"str"},{"name":"colour 3","value":"fuchsia","type":"str"},{"name":"fontclr 1","value":"black","type":"str"},{"name":"fontclr 2","value":"black","type":"str"},{"name":"fontclr 3","value":"black","type":"str"}],"x":1640,"y":280,"wires":[["e20a43ccb6ec319a"],[]]}]

My understanding is, you can't set an env in code ( I could be wrong here)

So you (instead) need to use an area that can be set during runtime ( i.e context) therefore use env as the possible value - to be used

  1. in On Start set context to the provided (env value) OR use a default of your choice
const value = env.get("topic") || "Your Default"
context.set("topic", value);
  1. then in On Message
context.get("topic") // what was provided as an `env` or you chosen default (as decided in On Start)

Close.

It isn't I CAN'T but more as a catch if I don't set it.

The problem - as I now see it - is that I have specified the env.topic as a STRING in the subflow.
Alas that means it can't be null or undefined.

So I need to catch the problem if the length is <1 (or 0)

Does that help?

in on start, (or at any point) you can check its undefined state.


if(!env.get("topic")){
    node.error("Not Set")
}

a blank string is considered falsely

Yes, but as I just said: It isn't saying NOT SET.
It is simply giving me an empty string.

So I need to detect if the length is <1 (to say)

As I don't know - off the back of my hand - the command to do this I am really stuck.
I am trying to look, but I am not getting results that help me as they are pure JS code and when I try to enter it in the function node, it errors.

This?

if ("".length < 1) {}

I didn't understand the whole point :thinking:

1 Like

you can do it on message also (no need for context)

if(!env.get("topic")){
   throw new Error("Topic not set")
}

(Nor do I - to be honest)

I wrote a subflow and didn't do my homework.

I didn't set one of the values and it worked only by luck.

I found ANOTHER problem with the subflow and am fixing all the problems - I hope.

So I need to catch any undefined env names and set them to something.

But I can't talk the talk of JS and so despite my searches can't find the command/trick to detect the unset value.
Partly because if you specify a TYPE for the env you don't get a null or undefined when you check it.

This is what I have now and I think it works.
(am about to test it)

// Code added here will be run once
// whenever the node is started.
let value = env.get("topic")
if (value.length < 1)
{
    value = "DEFAULT";
    node.warn("CAUGHT")
}
context.set("topic", value);

I put the node.warn in there to indicate it is catching it and it seems to be doing that.

I'll get back to this in a few seconds.

Ok, only to stop the older posts being edited:

What I now have works.

Code:
on start

// Code added here will be run once
// whenever the node is started.
let value = env.get("topic")
if (value.length < 1)
{
    value = "DEFAULT";
    node.warn("CAUGHT")
}
node.warn("Value being set " + value)
context.set("topic", value);

and in the main code, I set the msg.topic to context.get("topic") and I get the right topic out.

ignore that :laughing:

I wasn't quick enough to see what you said there. :wink:

No problems.

Ah, just saw your next post.

All good.

So to get a second set of eyes on it:

That will work - yes?

Just to share the new subflow:

[{"id":"5b2f1c1e34d60f19","type":"subflow","name":"3 Press button","info":"2024 01 07\n==\n\nHuge bug found and fixed.\n\nStill have a bug with code if the\n`topic` is not set.\n\nBut now better than what it was.","category":"","in":[{"x":70,"y":190,"wires":[{"id":"ec5080f153f43d22"}]}],"out":[{"x":840,"y":190,"wires":[{"id":"ee1c20a8875e36cc","port":0}]},{"x":840,"y":270,"wires":[{"id":"10d2bd1bfc555db3","port":0}]}],"env":[{"name":"startat","type":"num","value":"","ui":{"label":{"en-US":"start at"},"type":"input","opts":{"types":["num"]}}},{"name":"hold","type":"bool","value":"true","ui":{"type":"input","opts":{"types":["bool"]}}},{"name":"topic","type":"str","value":"","ui":{"type":"input","opts":{"types":["str"]}}},{"name":"Message 1","type":"str","value":""},{"name":"Message 2","type":"str","value":""},{"name":"Message 3","type":"str","value":""},{"name":"colour 1","type":"str","value":""},{"name":"colour 2","type":"str","value":""},{"name":"colour 3","type":"str","value":""},{"name":"fontclr 1","type":"str","value":""},{"name":"fontclr 2","type":"str","value":""},{"name":"fontclr 3","type":"str","value":""}],"meta":{},"color":"#DDAA99","inputLabels":["Input"],"outputLabels":["Output","Loop back to button node"],"icon":"node-red-dashboard/ui_button.png"},{"id":"9c628490f5a7ab36","type":"switch","z":"5b2f1c1e34d60f19","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":3,"x":230,"y":270,"wires":[["625edd1859414ac8"],["2ed89392aefd3ac3"],["75984bf53702c1ac"]]},{"id":"625edd1859414ac8","type":"change","z":"5b2f1c1e34d60f19","name":"1","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 1","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 1","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 1","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":230,"wires":[["10d2bd1bfc555db3"]]},{"id":"2ed89392aefd3ac3","type":"change","z":"5b2f1c1e34d60f19","name":"2","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 2","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 2","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 2","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":270,"wires":[["10d2bd1bfc555db3"]]},{"id":"75984bf53702c1ac","type":"change","z":"5b2f1c1e34d60f19","name":"3","rules":[{"t":"set","p":"text","pt":"msg","to":"Message 3","tot":"env"},{"t":"set","p":"bgc","pt":"msg","to":"colour 3","tot":"env"},{"t":"set","p":"fontclr","pt":"msg","to":"fontclr 3","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":310,"wires":[["10d2bd1bfc555db3"]]},{"id":"ec5080f153f43d22","type":"function","z":"5b2f1c1e34d60f19","name":"","func":"//  Get \"start value\"\nif (msg.topic == \"startup\")\n{\n    context.set(\"previous\",msg.payload-1);\n}\n\nif (msg.payload == \"reset\")\n{\n    context.set(\"previous\",0);\n    return;\n}\n\nvar max = 4;\nvar previous = context.get(\"previous\") || 0;\nprevious = (previous + 1) % max;\ncontext.set(\"previous\",previous);\n\nif (previous == 0)\n{\n    previous = 1;\n    context.set(\"previous\",1);\n}\nmsg.payload = previous;\nmsg.topic = context.get(\"topic\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nlet value = env.get(\"topic\")\nif (value.length < 1)\n{\n    value = \"DEFAULT\";\n}\ncontext.set(\"topic\", value);\n","finalize":"","libs":[],"x":390,"y":190,"wires":[["9c628490f5a7ab36","ee1c20a8875e36cc"]]},{"id":"ee1c20a8875e36cc","type":"trigger","z":"5b2f1c1e34d60f19","name":"Delay","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"350","extend":true,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":570,"y":190,"wires":[["20347974efa46acb"]]},{"id":"20347974efa46acb","type":"change","z":"5b2f1c1e34d60f19","name":"$hold","rules":[{"t":"set","p":"hold","pt":"msg","to":"hold","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":535,"y":150,"wires":[["52f023f6a52afa5b"]],"l":false},{"id":"52f023f6a52afa5b","type":"switch","z":"5b2f1c1e34d60f19","name":"if FALSE","property":"hold","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":585,"y":150,"wires":[["7d8c55ef2950c72a"]],"l":false},{"id":"7d8c55ef2950c72a","type":"change","z":"5b2f1c1e34d60f19","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":635,"y":150,"wires":[["ec5080f153f43d22"]],"l":false},{"id":"10d2bd1bfc555db3","type":"junction","z":"5b2f1c1e34d60f19","x":550,"y":270,"wires":[[]]},{"id":"d87556ee78f63d9c","type":"subflow:5b2f1c1e34d60f19","z":"88de1ac2.8b8c38","name":"","env":[{"name":"startat","value":"0","type":"num"},{"name":"topic","value":"somethingelse","type":"str"},{"name":"Message 1","value":"ALL","type":"str"},{"name":"Message 2","value":"BLOCK","type":"str"},{"name":"Message 3","value":"ONE","type":"str"},{"name":"colour 1","value":"limegreen","type":"str"},{"name":"colour 2","value":"violet","type":"str"},{"name":"colour 3","value":"fuchsia","type":"str"},{"name":"fontclr 1","value":"black","type":"str"},{"name":"fontclr 2","value":"black","type":"str"},{"name":"fontclr 3","value":"black","type":"str"}],"x":1640,"y":280,"wires":[["e20a43ccb6ec319a"],[]]}]

This works - if you don't mind checking.

This is where I was going.

const getValue = () => {
    if (env.get("topic")) {
            return env.get("topic");
        } else {
            node.warn("Using Default")
            return "DEFAULT"
        }
    }
}

context.set("topic", getValue());

Sorry but

const getValue = () => {
    if (env.get("topic") {
            return env.get("topic");
        } else {
            node.warn("Using Default")
            return "DEFAULT"
        }
    }
}

context.set("topic", getValue());

Is above me.

and I am also confused by it being a const......
(What can I say? I never really dealt with this kind of language or structure in my life. This is still at the coal face for me.)

Does that go in the at start tab?

(Now wanting to step on your toes while you are replying but...)
Isn't this easier?

// Code added here will be run once
// whenever the node is started.
let value = env.get("topic")
if (value.length < 1)
{
    value = "DEFAULT";
}
context.set("topic", value);

Correct.

The const is an arrow function

that returns your env or your default

1 Like

Again: an arrow function.

WAY over my head of knowledge.

Not sure what I'm going to say but if you want to add (because you forgot) a value to env.topic why not do it directly in the subflow def?

{
  "name": "topic",
  "type": "str",
  "value": "",
  "ui": {
    "type": "input",
    "opts": {
      "types": [
        "str"
      ]
    }
  }
}