# Injecting user selection into the payload

**URL:** <https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522>\
**Category:** General\
**Created:** [25 April 2019 03:04 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522 "2019-04-25T03:04:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djvj1](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@djvj1](https://discourse.nodered.org/u/djvj1)\
**Post date:** [25 April 2019 03:04 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/1 "2019-04-25T03:04:05Z")

</div>

I'm trying to make a Celsius / Fahrenheit dropdown setting which then changes the gauge. I'm still learning here but I understand objects, just not necessarily JScript.

What I have now I cannot get the setting properly in the function node to use it.

This is what I have so far. The first gauge works fine reading from my temp sensor. The test one I wanted to change based on the setting.

```auto
[{"id":"514ce859.e01408","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"e7cca8d4.2a1318","type":"debug","z":"514ce859.e01408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":160,"wires":[]},{"id":"6573bd44.1cf1d4","type":"function","z":"514ce859.e01408","name":"Convert C to F","func":"var tempc = msg.payload;\nvar tempSetting = msg.options;\nif (tempSetting == \"f\") {\n tempf = tempc * 9/5 + 32;\n tempf = Math.round(tempf * 10) / 10;\n msg.payload = tempf;\n}\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":100,"wires":[["85a28a0e.b61b28","e7cca8d4.2a1318"]]},{"id":"85a28a0e.b61b28","type":"ui_gauge","z":"514ce859.e01408","name":"","group":"2695801d.e533b","order":5,"width":0,"height":0,"gtype":"gage","title":"Test Gauge","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":670,"y":100,"wires":[]},{"id":"4523fb4c.a05494","type":"change","z":"514ce859.e01408","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":100,"wires":[["6573bd44.1cf1d4"]]},{"id":"358289ea.d1af66","type":"rpi-ds18b20","z":"514ce859.e01408","topic":"","array":false,"name":"","x":270,"y":40,"wires":[["a438fcbb.e47e1","6573bd44.1cf1d4"]]},{"id":"4c274079.bfedc","type":"ui_dropdown","z":"514ce859.e01408","name":"","label":"Temperature","tooltip":"","place":"Fahrenheit","group":"eec24d39.3cbf7","order":1,"width":0,"height":0,"passthru":true,"options":[{"label":"Fahrenheit","value":"f","type":"str"},{"label":"Celcius","value":"c","type":"str"}],"payload":"","topic":"","x":90,"y":100,"wires":[["4523fb4c.a05494"]]},{"id":"b5db9b99.4701f8","type":"inject","z":"514ce859.e01408","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":40,"wires":[["358289ea.d1af66"]]},{"id":"a438fcbb.e47e1","type":"function","z":"514ce859.e01408","name":"Convert C to F","func":"var tempc = msg.payload;\ntempf = tempc * 9/5 + 32;\ntempf = Math.round(tempf * 10) / 10;\nmsg.payload = tempf;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":40,"wires":[["ffa4fbdc.8f8348"]]},{"id":"ffa4fbdc.8f8348","type":"ui_gauge","z":"514ce859.e01408","name":"","group":"2695801d.e533b","order":5,"width":0,"height":0,"gtype":"gage","title":"Air Temp","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":660,"y":40,"wires":[]},{"id":"2695801d.e533b","type":"ui_group","z":"","name":"Temps","tab":"95e58647.89a308","disp":true,"width":"4","collapse":false},{"id":"eec24d39.3cbf7","type":"ui_group","z":"","name":"Temperature","tab":"d3fb8df3.f85c8","order":1,"disp":false,"width":"6","collapse":false},{"id":"95e58647.89a308","type":"ui_tab","z":"","name":"Monitor & Control","icon":"pool","disabled":false,"hidden":false},{"id":"d3fb8df3.f85c8","type":"ui_tab","z":"","name":"Settings","icon":"settings","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 April 2019 07:17 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/2 "2019-04-25T07:17:08Z")

</div>

Hi,

really close :-)... as each event is separate you need to store the "other" value in context while you wait for the value to arrive... - so for example store the f/c select - then use it when the temperature arrives...

here is my simple version of yours, without the ds18b20 node which I don't have... but you will get the picture

```auto
[{"id":"d8607ebb.1c175","type":"debug","z":"9bd2044e.77fac8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":790,"y":620,"wires":[]},{"id":"8d19ff4f.d97cd","type":"function","z":"9bd2044e.77fac8","name":"Convert C to F","func":"var tempc = msg.payload;\nvar tempSetting = context.flow.get(\"options\");\nnode.warn(tempSetting)\nif (tempSetting === \"f\") {\n tempf = tempc * 9/5 + 32;\n tempf = Math.round(tempf * 10) / 10;\n msg.payload = tempf;\n}\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":560,"wires":[["15933e82.bf9891","d8607ebb.1c175"]]},{"id":"15933e82.bf9891","type":"ui_gauge","z":"9bd2044e.77fac8","name":"","group":"d1e95016.4377c","order":1,"width":0,"height":0,"gtype":"gage","title":"Test Gauge","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":810,"y":560,"wires":[]},{"id":"27e1577f.afd488","type":"change","z":"9bd2044e.77fac8","name":"","rules":[{"t":"set","p":"options","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":600,"wires":[[]]},{"id":"57e2e6cb.c94918","type":"ui_dropdown","z":"9bd2044e.77fac8","name":"","label":"Temperature","tooltip":"","place":"Fahrenheit","group":"d1e95016.4377c","order":3,"width":0,"height":0,"passthru":true,"options":[{"label":"Fahrenheit","value":"f","type":"str"},{"label":"Celcius","value":"c","type":"str"}],"payload":"","topic":"","x":230,"y":600,"wires":[["27e1577f.afd488"]]},{"id":"8f5831ff.28188","type":"inject","z":"9bd2044e.77fac8","name":"","topic":"","payload":"20","payloadType":"num","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":500,"wires":[["2cae81e8.fbc6be","8d19ff4f.d97cd"]]},{"id":"2cae81e8.fbc6be","type":"function","z":"9bd2044e.77fac8","name":"Convert C to F","func":"var tempc = msg.payload;\ntempf = tempc * 9/5 + 32;\ntempf = Math.round(tempf * 10) / 10;\nmsg.payload = tempf;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":500,"wires":[["e8b88a96.b41e58"]]},{"id":"e8b88a96.b41e58","type":"ui_gauge","z":"9bd2044e.77fac8","name":"","group":"d1e95016.4377c","order":2,"width":0,"height":0,"gtype":"gage","title":"Air Temp","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":800,"y":500,"wires":[]},{"id":"d1e95016.4377c","type":"ui_group","z":"","name":"Temps","tab":"38da6833.b15da8","disp":true,"width":"4","collapse":false},{"id":"38da6833.b15da8","type":"ui_tab","z":"","name":"Monitor & Control","icon":"pool","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![djvj1](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@djvj1](https://discourse.nodered.org/u/djvj1)\
**Post date:** [25 April 2019 08:46 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/3 "2019-04-25T08:46:33Z")

</div>

Thank you for the help.

I tested your solution but it doesn't change the test gauge as it's still showing the Celsius value even when injecting "20" again when set to Fahrenheit.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [25 April 2019 09:16 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/4 "2019-04-25T09:16:23Z")

</div>

The function that @dceejay provided you, showed you how to do the bit you were stuck on.

I would hope that you can do the other part of the function where the temperature isn't set to f  
[https://www.w3schools.com/js/js\_if\_else.asp](https://www.w3schools.com/js/js_if_else.asp)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 April 2019 10:47 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/5 "2019-04-25T10:47:32Z")

</div>

Sorry - my mistake...

```auto
[{"id":"d8607ebb.1c175","type":"debug","z":"9bd2044e.77fac8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":790,"y":620,"wires":[]},{"id":"8d19ff4f.d97cd","type":"function","z":"9bd2044e.77fac8","name":"Convert C to F","func":"var tempc = msg.payload;\nvar tempSetting = context.flow.get(\"options\");\nnode.warn(tempSetting)\nif (tempSetting === \"f\") {\n tempf = tempc * 9/5 + 32;\n tempf = Math.round(tempf * 10) / 10;\n msg.payload = tempf;\n}\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":560,"wires":[["15933e82.bf9891","d8607ebb.1c175"]]},{"id":"15933e82.bf9891","type":"ui_gauge","z":"9bd2044e.77fac8","name":"","group":"d1e95016.4377c","order":1,"width":0,"height":0,"gtype":"gage","title":"Test Gauge","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":810,"y":560,"wires":[]},{"id":"27e1577f.afd488","type":"change","z":"9bd2044e.77fac8","name":"","rules":[{"t":"set","p":"options","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":600,"wires":[[]]},{"id":"57e2e6cb.c94918","type":"ui_dropdown","z":"9bd2044e.77fac8","name":"","label":"Temperature","tooltip":"","place":"Fahrenheit","group":"d1e95016.4377c","order":3,"width":0,"height":0,"passthru":true,"options":[{"label":"Fahrenheit","value":"f","type":"str"},{"label":"Celcius","value":"c","type":"str"}],"payload":"","topic":"","x":230,"y":600,"wires":[["27e1577f.afd488"]]},{"id":"8f5831ff.28188","type":"inject","z":"9bd2044e.77fac8","name":"","topic":"","payload":"20","payloadType":"num","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":500,"wires":[["2cae81e8.fbc6be","8d19ff4f.d97cd"]]},{"id":"2cae81e8.fbc6be","type":"function","z":"9bd2044e.77fac8","name":"Convert C to F","func":"var tempc = msg.payload;\ntempf = tempc * 9/5 + 32;\ntempf = Math.round(tempf * 10) / 10;\nmsg.payload = tempf;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":500,"wires":[["e8b88a96.b41e58"]]},{"id":"e8b88a96.b41e58","type":"ui_gauge","z":"9bd2044e.77fac8","name":"","group":"d1e95016.4377c","order":2,"width":0,"height":0,"gtype":"gage","title":"Air Temp","label":"units","format":"{{value}}","min":0,"max":"120","colors":["#00b500","#e6e600","#ca3838"],"seg1":"34","seg2":"70","x":800,"y":500,"wires":[]},{"id":"d1e95016.4377c","type":"ui_group","z":"","name":"Temps","tab":"38da6833.b15da8","disp":true,"width":"4","collapse":false},{"id":"38da6833.b15da8","type":"ui_tab","z":"","name":"Monitor & Control","icon":"pool","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![djvj1](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@djvj1](https://discourse.nodered.org/u/djvj1)\
**Post date:** [25 April 2019 21:30 UTC](https://discourse.nodered.org/t/injecting-user-selection-into-the-payload/10522/6 "2019-04-25T21:30:05Z")

</div>

Thank you! That works a treat!
