How to set data in a separate functions with undefined activation sequences?

Hi,
I've been trying to wrap my head around the get and set functions but quite confused on setting the msg into a different function with a second activation point. The output always delivers 'undefined'.

JSON Clipboard

[{"id":"d69a424d.8fbba","type":"debug","z":"904d1240103e356e","name":"","active":true,"console":"false","complete":"true","x":970,"y":500,"wires":[]},{"id":"16df74bf.d5080b","type":"function","z":"904d1240103e356e","name":"","func":"var a = context.get(\"a\")\na = msg.work\ncontext.set(\"a\")\nmsg.wow = a\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":500,"wires":[["d69a424d.8fbba"]]},{"id":"ea001454.a084e8","type":"function","z":"904d1240103e356e","name":"","func":"msg.work = 1 \nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":580,"wires":[["16df74bf.d5080b"]]},{"id":"a98b42aa.ff2ce","type":"inject","z":"904d1240103e356e","name":"","repeat":"","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":500,"y":520,"wires":[["16df74bf.d5080b"]]},{"id":"945e45b0.160728","type":"inject","z":"904d1240103e356e","name":"","repeat":"","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":485.98956298828125,"y":618.8194885253906,"wires":[["ea001454.a084e8"]]}]
var a = context.get("a")
a = msg.work
context.set("a")
msg.wow = a
return msg;
  1. I don't see the point of getting the context variable a since you immediately replace it with msg.work, even if msg.work is undefined. Can you explain what you are trying to do?
  2. This is the right way to set a context variable in function code:
    context.set("a", a)

Guessing at what you are trying to do, I suspect that you want something like

const a = msg.work ?? context.get("a")
context.set("a", a)
msg.wow = a
return msg;

That will set the variable a to msg.work, if it is present in the message, otherwise it will fetch the previous value from context. In either case it saves the value in context.

I'm trying to put it in a scenario of a user inputting a value that repeats constantly within the other input button, until the user puts a different number in and it will display a different value.

I am struggling to understand you Chris.

If you inject a value and store it in a context variable, exactly as @Colin showed you, it is available whenever you need to use it. That is not quite the same thing as "repeats constantly"

Is there a Node-red dashboard involved here or do your "button" and "displayed" refer solely to the editor?

Perhaps if you give us more information on what you are trying to achieve it would help.
Real world description "I am trying to automate the control rod position in my nuclear reactor" is less confusing than "I want to input a number and be able to change it" :smile: :volcano:

Sorry if this was confusing and appreciate the assistance. Hope this clarifies much better.

[{"id":"fe1c6065.f5814","type":"ui_text_input","z":"904d1240103e356e","name":"","label":"","tooltip":"","group":"545cc5fa.03029c","order":0,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"","sendOnBlur":true,"className":"","topicType":"str","x":710,"y":1100,"wires":[["206fa729.627278"]]},{"id":"206fa729.627278","type":"change","z":"904d1240103e356e","name":"","rules":[{"t":"set","p":"wow","pt":"msg","to":"msg.payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":1140,"wires":[["8a3d38cf.09cc48"]]},{"id":"1c114f08.2571d1","type":"debug","z":"904d1240103e356e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1380,"y":1220,"wires":[]},{"id":"8a3d38cf.09cc48","type":"function","z":"904d1240103e356e","name":"","func":"msg.wow\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":1220,"wires":[["1c114f08.2571d1"]]},{"id":"69c0362f.ee91f8","type":"inject","z":"904d1240103e356e","name":"","repeat":"","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":980,"y":1260,"wires":[["8a3d38cf.09cc48"]]},{"id":"545cc5fa.03029c","type":"ui_group","name":"Planned Production Time ","tab":"fa542dd5.68ce8","order":1,"disp":true,"width":"6","collapse":false},{"id":"fa542dd5.68ce8","type":"ui_tab","name":"Performance Dashboard ","icon":"dashboard"}]

Essentially having the the user input a value and the timestamp to repeat the value that the user has inputted. The issue is that whenever the set message has the number the other activation button doesn't display the value. Hope this clarifies much better.

Well it's still rather abstract but maybe other people will grasp it more easily than me.

Your function node now has this:

msg.wow
return msg;

It does absolutely nothing with msg.wow, in fact it does nothing at all.
What were you expecting it to do?

Here is an example of using context variables to save an input and to retrieve it.
I really have no idea if it is relevant to your issue.

[{"id":"dee12c22f8d1b130","type":"group","z":"a28582204ac0cbe9","name":"Choose a favourite volcano. Save as flow.volcano","style":{"label":true},"nodes":["206fa729.627278","69c0362f.ee91f8","ad906f0717fa10cc"],"x":34,"y":19,"w":632,"h":122},{"id":"206fa729.627278","type":"change","z":"a28582204ac0cbe9","g":"dee12c22f8d1b130","name":"","rules":[{"t":"set","p":"volcano","pt":"flow","to":"msg.payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":80,"wires":[[]]},{"id":"69c0362f.ee91f8","type":"inject","z":"a28582204ac0cbe9","g":"dee12c22f8d1b130","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"Popacatapetl","payloadType":"str","x":150,"y":100,"wires":[["206fa729.627278"]]},{"id":"ad906f0717fa10cc","type":"inject","z":"a28582204ac0cbe9","g":"dee12c22f8d1b130","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Aconcagua","payloadType":"str","x":160,"y":60,"wires":[["206fa729.627278"]]},{"id":"06ad53cbb091a55f","type":"group","z":"a28582204ac0cbe9","name":"If inject has msg.work, print that. Otherwise print flow.volcano","style":{"label":true},"nodes":["1c114f08.2571d1","8a3d38cf.09cc48","c06d4acc85f80222","bbc8da46a6989d8c","a491b25559df1c89"],"x":34,"y":159,"w":632,"h":162},{"id":"1c114f08.2571d1","type":"debug","z":"a28582204ac0cbe9","g":"06ad53cbb091a55f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":240,"wires":[]},{"id":"8a3d38cf.09cc48","type":"function","z":"a28582204ac0cbe9","g":"06ad53cbb091a55f","name":"Chosen Volcano","func":"const volcano = msg.work ?? flow.get(\"volcano\")\nmsg.payload = \"My favourite volcano is \" + volcano\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":240,"wires":[["1c114f08.2571d1"]]},{"id":"c06d4acc85f80222","type":"inject","z":"a28582204ac0cbe9","g":"06ad53cbb091a55f","name":"No choice","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":200,"wires":[["8a3d38cf.09cc48"]]},{"id":"bbc8da46a6989d8c","type":"inject","z":"a28582204ac0cbe9","g":"06ad53cbb091a55f","name":"Anak Krakatao","props":[{"p":"payload"},{"p":"work","v":"Anak Krakatao","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":240,"wires":[["8a3d38cf.09cc48"]]},{"id":"a491b25559df1c89","type":"inject","z":"a28582204ac0cbe9","g":"06ad53cbb091a55f","name":"Eyjafjallajökull","props":[{"p":"payload"},{"p":"work","v":"Eyjafjallajökull","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":280,"wires":[["8a3d38cf.09cc48"]]}]
1 Like

That's perfect! Thanks and sorry again if my terms and explanation are confusing.

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