Injecting user selection into the payload

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.

[{"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}]

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

[{"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}]

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.

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

Sorry - my mistake...

[{"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}]
1 Like

Thank you! That works a treat!