Pulling global values and and merging other variables using json

I need to pull a global variable using JSON how do I do that
I was able to pull the global value like this

var ObtainedData = global.get("GlobalRoom");
msg.payload = ObtainedData;
return msg;

based on this video https://www.youtube.com/watch?v=QDFONY4WOHw
but I don't like that way because it requires a few different nodes and does not fully give me what I need

I want with one button click using a trigger node or a dashboard button node using JSON to get the global value global.payload.room convert it to msg.payload.room and also add the value of the button that was pressed example msg.payload.button = button1

"global. payload.room = msg.payload.room and add the value of the button that was pressed"

in this picture, you can see I have a dropdown that outputs global.payload.room
and when I push a button I want the output to be the value of the dropdown and the button in one message

msg.topic= button pressed
msg.payload.room
msg.payload.button

Share your flow, this will make it more likely that you will get help with a solution. In principle you will have to catch the button press message and combine that with a global variable. This will require that the "button" part or component has access to what has been selected from the drop down. WIthout knowing what and how you have done, well impossible to give advice

If the web page above is fully written in a template node using html/javascript you could combine them there. If you have used NR dashboard nodes you have to catch messages and combine them differently

Not sure of your global name as it appears to change in your post.
And not sure what you mean by JSON

But here is my guess on what you may require.

[{"id":"337cc23.e8022be","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":2340,"wires":[["7d1d4dc1.101b1c"]]},{"id":"7d1d4dc1.101b1c","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"label":"button 1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"button 1","payloadType":"str","topic":"","x":250,"y":2300,"wires":[["efb018ff.1c1b7"]]},{"id":"ffd491fb.4123b8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":2220,"wires":[]},{"id":"efb018ff.1c1b7","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.button","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"payload.button","tot":"msg"},{"t":"set","p":"payload.room","pt":"msg","to":"$globalContext(\"GlobalRoom\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":2340,"wires":[["ffd491fb.4123b8"]]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Will need more info if this is not right.
probably best to export t a section of your flow that you would like shorten, and a example of the global.

this is where I plan to put the code

I grouped the nodes so you can see what I am trying to do

https://creditkey.mybluemix.net/red/#flow/23cf197d.f319c6

for the payload, i want to use the built-in script function witch uses JSON code

@krambriw @E1cid

these are the payload variables

the dropdown is
global.payload.room

so i want the button
to pull global.payload.room, convert to msg.payload.room (the output value is 1,2,3,4,5,6)

the button value should go under msg.payload.button = "button1"

and the message topic should be msg.topic= "button pressed"

If i understand, it is not possible to select JSON { } in button and access a global. It would be possible if the button node had access to JSONata library.

Only way is to set the global in the button and a topic then use change node to move items and buttn value., as below

[{"id":"240a26c1.496232","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":2460,"wires":[["bb5afb61.3ebe"]]},{"id":"bb5afb61.3ebe","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"passthru":true,"label":"1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"GlobalRoom","payloadType":"global","topic":"button1","x":240,"y":2460,"wires":[["f08bf02b.ca85c8"]]},{"id":"f08bf02b.ca85c8","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.room","tot":"msg"},{"t":"set","p":"payload.button","pt":"msg","to":"$substring(topic, -1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":2480,"wires":[["b7b4d5d5.c27648"]]},{"id":"8d70060b.ac9808","type":"ui_button","z":"8d22ae29.7df6d","name":"","group":"165e1e1a.e752fa","order":7,"width":0,"height":0,"passthru":true,"label":"2","tooltip":"","color":"","bgcolor":"","icon":"","payload":"GlobalRoom","payloadType":"global","topic":"button2","x":230,"y":2500,"wires":[["f08bf02b.ca85c8"]]},{"id":"b7b4d5d5.c27648","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":2440,"wires":[]},{"id":"3ccef7dc.ffadf8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":2500,"wires":[["8d70060b.ac9808"]]},{"id":"7691e4a9.e46244","type":"ui_dropdown","z":"8d22ae29.7df6d","name":"","label":"","tooltip":"","place":"Select option","group":"165e1e1a.e752fa","order":8,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"room 1","value":"room 1","type":"str"},{"label":"room 2","value":"room 2","type":"str"}],"payload":"","topic":"","x":260,"y":2400,"wires":[["b7b4d5d5.c27648","6c7bc84b.a6499"]]},{"id":"6799842e.87b524","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"room 1","payloadType":"str","x":80,"y":2400,"wires":[["7691e4a9.e46244"]]},{"id":"6c7bc84b.a6499","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"GlobalRoom","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2400,"wires":[[]]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

it does have access look at the pitures i posted

The JSON type does not allow any sort of dynamic construction of the value - it's for fixed values only. The screenshot you've shared shows the JSON editor where you enter the fixed value. It cannot reference any global or msg values.

When a button is clicked you'll get a message with msg.payload set to a number. From the screenshot of the button config you've shared, it looks like it will send the value 1.

To get the message you want, you could use a Function node with the code:

msg.topic = "button pressed";
msg.payload = {
   room: global.get("payload.room"),
   button: msg.payload
}
return msg;

Or you could use a Change node with the following rules:

  1. move msg.payload to msg.button
  2. set msg.payload to (JSON) {}
  3. set msg.payload.room to global.payload.room
  4. move msg.button to msg.payload.button
  5. set msg.topic to "button pressed"

Note the need to move the existing msg.payload to a different property temporarily so that you can construct msg.payload as the object you want.

A different way with the Change node would be the rules:

  1. set msg.topic to "button pressed"
  2. set msg.payload to (Expression) { "room": $globalContext("payload.room"), "button": $.payload}

ok i will try the 3 methods

Your picture shows a JSON input, not a JSONata expression input.
Did you even try my example?

what is the best way to set my dropdown to a global value?

I don't think it is set up right for a global value
because the options you gave me I'm not seeing the global value in the debug node

Screenshot 2020-12-31 024012

you can check out the flow I updated it and grouped it better

i think i got it

now i need to join payload.room and payload.button like this 1.1 first "1" is the room and second 1 is the button value

use a jsonata expression or function to concatenate the values

e.g. in a function

msg.payload.roomButton = msg.payload.room + "." + msg.payload.button;
return msg;

thank you that worked I think I finally got it

tomorrow I can explain what this project is for
i think my project can be a great sorce

@E1cid @knolleary @krambriw please check out my latest topic

@Zbrooklyn please do not tag us into your other threads. There is no need to do that. We will see your posts just like every other post.

ok sorry still getting used to the forum

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