Noob set flow variable question

Accroding to the documentation it should work to set variables from a function node to the flow like that.

if (flow.get("bot_command")!==undefined){
    flow.set("bot_command", "c0,0");
}
msg.payload = flow.get('bot_command');
return msg;

Also that didn't work
ChangeNode

do I have to prepare something before using flow variables ?

I think the expression should be
== 'undefined'

1 Like

@gerry That did actually work, thanks.
Would you know how to do the same in a change node ?

You cannot do a 'set if not defined' type of rule in the Change node, but aside from that, you can set a flow context value. In the screenshot you've shared, I can see you were using msg.paylod and not msg.payload.

That says that if it is not undefined then set it. Perhaps you meant if it is undefined then set it.

if (flow.get("bot_command")===undefined){

that syntax is frowned on, however, as the name undefined can be set to a value. An alternative is

if (typeof(flow.get("bot_command")) === "undefined"){

or, if know that the variable should never be zero or false or an empty string then you can use

msg.payload = flow.get("bot_command")  ||  "c0,0"
flow.set("bot_command",msg.payload)
return msg

@knolleary thanks changing it to payload did work....sorry posting a typo
it would be nice if that is possible
@Colin thanks for the hints