Variable value as part of name of the another variable

Hi, plese help a newbie which can't even formulate its questions properly :slight_smile: I have two of them:

  1. Lets say i have a switch, which after action sends what was done like a confirmation that task was done: http://...../switch?id=sw1&action=off or ...&action=on - this is my httpIn node.
    I want to store this value in to flow.sw1.action variable. Is it possible to do this with "change" node?

I tried set flow.{{msg.payload.id}}.action to 0, but after i looked into context data tab i found some mess with all this curly brackets inside :frowning:

  1. Before my switch performs any action it does similar call to the same httpIn node to get current state - http://...../switch?id=sw1&action=get
    I want to format response based on another variable flow.sw1.state and return it to my switch.

I tried to use template node and form responce like this {{flow.{{msg.payload.id}}.state}}, but again, result not as i expected. I get .req}}.state}} in the response body.

How can i use variable value as part of name of the another variable?

Regards
PetrasL

Here is a example of writing to context and reading from context,
Hope it helps.

[{"id":"b26327a4.75fa9","type":"inject","z":"5a245aa1.510164","name":"http input on","props":[{"p":"id","v":"sw1","vt":"str"},{"p":"action","v":"on","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":4760,"wires":[["bc0ba1e5.fe905"]]},{"id":"bc0ba1e5.fe905","type":"function","z":"5a245aa1.510164","name":"","func":"flow.set(\"switches.\"+msg.id+\".action\", msg.action)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":4780,"wires":[["f749d945.96ee8"]]},{"id":"f6078b34.02cbe","type":"inject","z":"5a245aa1.510164","name":"http input off","props":[{"p":"id","v":"sw1","vt":"str"},{"p":"action","v":"off","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":180,"y":4800,"wires":[["bc0ba1e5.fe905"]]},{"id":"f749d945.96ee8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$flowContext(\"switches\")","targetType":"jsonata","statusVal":"","statusType":"auto","x":530,"y":4780,"wires":[]},{"id":"ac0f1bce.49ff","type":"inject","z":"5a245aa1.510164","name":"http input","props":[{"p":"payload.id","v":"sw1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":210,"y":4860,"wires":[["a6a0bc6a.f634a"]]},{"id":"a6a0bc6a.f634a","type":"function","z":"5a245aa1.510164","name":"","func":"msg.payload = flow.get(\"switches.\"+msg.payload.id+\".action\") || \"error\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":4860,"wires":[["62c5ab65.e56f9c"]]},{"id":"62c5ab65.e56f9c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":4860,"wires":[]}]

I was busy putting together something very similar to E1 - If you want to store dynamic variable names in the context, that is pretty much the only way to do it, by appending strings together in a function node where you set the values.

Hi, Happy to help.I have gone down the function node path assuming you need to extract the parameters from the httpIn call. I have also assum Are you trying to parse the httpIn request to get the query parameters and save them to be used later. There are many ways to solve this but I have tried to stay close to your path. It may be possible in a change node but will leave it to JSONata experts (I am not)

Add a function node after the httpIn and parsre the queryParams. They are made avaialbe in

msg.req.query

The javascript secret to use a varaible name as part of an object is to enclose in brackets

let queryParams = msg.req.query

let switch_obj = {}
if (queryParams){
     id = queryParams.id ? queryParams.id : null // what do you want to do here. I assume id should be mandatory ?
     action = queryParams.action ? queryParams.action : null
     if (id && action){ 
      switch_obj = {
          id : id,
         action: action
      }
   }   
}

if (switch_obj){
     flow.set([switch_obj.id],switch_obj)
}

To get the switch object you could now do a

let switch_obj =  flow.get("sw1")
let action = switch_obj.action

Does this make sense ?

Thanks, guys! Both solutions works for me. :slight_smile: