Help with function node (Set button properties)

Hi. Im trying to set different properties to a button. But I am completely stuck.
I have succeeded to change background color and icon depending if msg.payload is true or false but I also want to change the label from an other path in the payload but I can't figure how to get this work in one function node.

Hope you under stand what I mean. Hard to explain.
Path to label is "payload.batch.event[0].argument[1].$.value" and I have an example output in the flow

Best regards Niclas

[{"id":"7fc9db75.09d914","type":"function","z":"d62f0d9d.6268a","name":"","func":"msg.payload = msg.payload.batch.event[0].$.name\n//msg.payload = msg.payload.batch.event[0].argument[1].$.value\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":1880,"wires":[["197dc0f3.f91fbf"]]},{"id":"197dc0f3.f91fbf","type":"change","z":"d62f0d9d.6268a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"controller.door.motorlock.locked","fromt":"str","to":"false","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"controller.door.motorlock.unlocked","fromt":"str","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":1880,"wires":[["84f3d7c1.6b2868","5a9f44c6.c6395c"]]},{"id":"84f3d7c1.6b2868","type":"function","z":"d62f0d9d.6268a","name":"Gate","func":"if (msg.payload === \"true\")node.send({ui_control:{icon:\"fa-2x fa-lock\"},bgcolor:\"green\",color:\"black\",label : msg.label,payload:msg.payload});\nif (msg.payload === \"false\")node.send({ui_control:{icon:\"fa-2x fa-unlock\"},bgcolor:\"red\",color:\"black\",label : msg.label,payload:msg.payload});\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1310,"y":1880,"wires":[["9efa0135.8264a"]]},{"id":"9efa0135.8264a","type":"ui_button","z":"d62f0d9d.6268a","name":"button","group":"d1877cae.519f1","order":11,"width":6,"height":1,"passthru":false,"label":"{{msg.label}}","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.bgcolor}}","icon":"{{msg.icon}}","payload":"pushed","payloadType":"str","topic":"","x":1430,"y":1880,"wires":[[]]},{"id":"d7843691.eb2e98","type":"template","z":"d62f0d9d.6268a","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"{\"batch\":{\"$\":{\"name\":\"controller.door.motorlock.unlocked.batch\"},\"event\":[{\"$\":{\"id\":\"10367816\",\"name\":\"controller.door.motorlock.unlocked\",\"type\":\"controller\",\"timestamp\":\"1602530902000\",\"domain\":\"4\",\"domainName\":\"Great Security KBA Energi gatan 3\"},\"argument\":[{\"$\":{\"value\":\"LCU\",\"type\":\"controller\",\"id\":\"11\"}},{\"_\":\"\\r\\n\\r\\naddress=0\\r\\n\\r\\n\",\"$\":{\"value\":\"Garage\",\"type\":\"dac\",\"id\":\"19\",\"externalId\":\"ID:19_20181001_142049\"}},{\"$\":{\"value\":\"Köksentre\",\"type\":\"door\",\"id\":\"14\"}}]}]}}","output":"json","x":1180,"y":1840,"wires":[["7fc9db75.09d914"]]},{"id":"680b079a.e985d8","type":"inject","z":"d62f0d9d.6268a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":960,"y":1840,"wires":[["d7843691.eb2e98"]]},{"id":"d1877cae.519f1","type":"ui_group","z":"","name":"Motorlås","tab":"8ae3ce41.59647","order":1,"disp":true,"width":6,"collapse":false},{"id":"8ae3ce41.59647","type":"ui_tab","z":"","name":"Maria Åkerberg AB","icon":"dashboard","disabled":false,"hidden":false}]

Not sure if i fully understand.
Try this

[{"id":"cc956680.61d3a8","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"batch\":{\"$\":{\"name\":\"controller.door.motorlock.unlocked.batch\"},\"event\":[{\"$\":{\"id\":\"10367816\",\"name\":\"controller.door.motorlock.unlocked\",\"type\":\"controller\",\"timestamp\":\"1602530902000\",\"domain\":\"4\",\"domainName\":\"Great Security KBA Energi gatan 3\"},\"argument\":[{\"$\":{\"value\":\"LCU\",\"type\":\"controller\",\"id\":\"11\"}},{\"_\":\"\\r\\n\\r\\naddress=0\\r\\n\\r\\n\",\"$\":{\"value\":\"Garage\",\"type\":\"dac\",\"id\":\"19\",\"externalId\":\"ID:19_20181001_142049\"}},{\"$\":{\"value\":\"Köksentre\",\"type\":\"door\",\"id\":\"14\"}}]}]}}","payloadType":"json","x":370,"y":1320,"wires":[["405514f0.5cffec"]]},{"id":"405514f0.5cffec","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"label","pt":"msg","to":"payload.batch.event[0].argument[1].$.value","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1320,"wires":[["ba5121b3.fb08e"]]},{"id":"ba5121b3.fb08e","type":"function","z":"7760f563.ea0324","name":"Gate","func":"if (msg.payload === true)node.send({ui_control:{icon:\"fa-2x fa-lock\"},bgcolor:\"green\",color:\"black\",label: msg.label, payload: msg.payload});\nif (msg.payload === false)node.send({ui_control:{icon:\"fa-2x fa-unlock\"},bgcolor:\"red\",color:\"black\",label: msg.label, payload: msg.payload});\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":1320,"wires":[["f353c7d1.be4218"]]},{"id":"f353c7d1.be4218","type":"ui_button","z":"7760f563.ea0324","name":"","group":"165e1e1a.e752fa","order":11,"width":6,"height":1,"passthru":false,"label":"{{msg.label}}","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.bgcolor}}","icon":"","payload":"pushed","payloadType":"str","topic":"","x":820,"y":1320,"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}]

I have added two inject nodes to simultate the result i want in the end. i think i need to remove the change node and put the conditions in the first function node.

payload.batch.event[0].argument[1]..value from the payload is the label and i want this to be moved to msg.label msg.payload = msg.payload.batch.event[0]..name from the payload is lock status. and if the output from msg.payload = msg.payload.batch.event[0].$.name is controller.door.motorlock.locked i want the msg.payload to be false and if it´s controller.door.motorlock.unlocked i want msg.payload to be true.

Can i do this in one function node??

Best Regards niclas

[{"id":"7fc9db75.09d914","type":"function","z":"d62f0d9d.6268a","name":"","func":"msg.payload = msg.payload.batch.event[0].$.name\n//msg.label = msg.payload.batch.event[0].argument[1].$.value\n//msg.payload = msg.payload.batch.event[0].argument[1].$.value\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":1880,"wires":[["197dc0f3.f91fbf"]]},{"id":"197dc0f3.f91fbf","type":"change","z":"d62f0d9d.6268a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"controller.door.motorlock.locked","fromt":"str","to":"false","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"controller.door.motorlock.unlocked","fromt":"str","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":1880,"wires":[["84f3d7c1.6b2868","5a9f44c6.c6395c"]]},{"id":"84f3d7c1.6b2868","type":"function","z":"d62f0d9d.6268a","name":"Gate","func":"if (msg.payload === \"true\")node.send({ui_control:{icon:\"fa-2x fa-lock\"},bgcolor:\"red\",color:\"black\",label : msg.label,payload:msg.payload});\nif (msg.payload === \"false\")node.send({ui_control:{icon:\"fa-2x fa-unlock\"},bgcolor:\"green\",color:\"black\",label : msg.label,payload:msg.payload});\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1310,"y":1880,"wires":[["9efa0135.8264a"]]},{"id":"9efa0135.8264a","type":"ui_button","z":"d62f0d9d.6268a","name":"button","group":"d1877cae.519f1","order":11,"width":6,"height":1,"passthru":false,"label":"{{msg.label}}","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.bgcolor}}","icon":"{{msg.icon}}","payload":"pushed","payloadType":"str","topic":"","x":1430,"y":1880,"wires":[[]]},{"id":"d7843691.eb2e98","type":"template","z":"d62f0d9d.6268a","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"{\"batch\":{\"$\":{\"name\":\"controller.door.motorlock.unlocked.batch\"},\"event\":[{\"$\":{\"id\":\"10367816\",\"name\":\"controller.door.motorlock.unlocked\",\"type\":\"controller\",\"timestamp\":\"1602530902000\",\"domain\":\"4\",\"domainName\":\"Great Security KBA Energi gatan 3\"},\"argument\":[{\"$\":{\"value\":\"LCU\",\"type\":\"controller\",\"id\":\"11\"}},{\"_\":\"\\r\\n\\r\\naddress=0\\r\\n\\r\\n\",\"$\":{\"value\":\"Garage\",\"type\":\"dac\",\"id\":\"19\",\"externalId\":\"ID:19_20181001_142049\"}},{\"$\":{\"value\":\"Köksentre\",\"type\":\"door\",\"id\":\"14\"}}]}]}}","output":"json","x":1100,"y":1840,"wires":[["7fc9db75.09d914"]]},{"id":"680b079a.e985d8","type":"inject","z":"d62f0d9d.6268a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":900,"y":1840,"wires":[["d7843691.eb2e98"]]},{"id":"4e18b86e.6d4508","type":"inject","z":"d62f0d9d.6268a","name":"Locked","props":[{"p":"payload"},{"p":"label","v":"Locked","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"str","x":1650,"y":1820,"wires":[["84f3d7c1.6b2868"]]},{"id":"a9a08944.f0a3b8","type":"inject","z":"d62f0d9d.6268a","name":"Unlocked","props":[{"p":"payload"},{"p":"label","v":"Unlocked","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"str","x":1820,"y":1820,"wires":[["84f3d7c1.6b2868"]]},{"id":"d1877cae.519f1","type":"ui_group","z":"","name":"Motorlås","tab":"8ae3ce41.59647","order":1,"disp":true,"width":6,"collapse":false},{"id":"8ae3ce41.59647","type":"ui_tab","z":"","name":"Maria Åkerberg AB","icon":"dashboard","disabled":false,"hidden":false}]

If i understand you correctly

[{"id":"cc956680.61d3a8","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"batch\":{\"$\":{\"name\":\"controller.door.motorlock.unlocked.batch\"},\"event\":[{\"$\":{\"id\":\"10367816\",\"name\":\"controller.door.motorlock.unlocked\",\"type\":\"controller\",\"timestamp\":\"1602530902000\",\"domain\":\"4\",\"domainName\":\"Great Security KBA Energi gatan 3\"},\"argument\":[{\"$\":{\"value\":\"LCU\",\"type\":\"controller\",\"id\":\"11\"}},{\"_\":\"\\r\\n\\r\\naddress=0\\r\\n\\r\\n\",\"$\":{\"value\":\"Garage\",\"type\":\"dac\",\"id\":\"19\",\"externalId\":\"ID:19_20181001_142049\"}},{\"$\":{\"value\":\"Köksentre\",\"type\":\"door\",\"id\":\"14\"}}]}]}}","payloadType":"json","x":370,"y":1420,"wires":[["53141e04.f354a8","2eab5727.a113f"]]},{"id":"db00c0ef.967858","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"batch\":{\"$\":{\"name\":\"controller.door.motorlock.unlocked.batch\"},\"event\":[{\"$\":{\"id\":\"10367816\",\"name\":\"controller.door.motorlock.locked\",\"type\":\"controller\",\"timestamp\":\"1602530902000\",\"domain\":\"4\",\"domainName\":\"Great Security KBA Energi gatan 3\"},\"argument\":[{\"$\":{\"value\":\"LCU\",\"type\":\"controller\",\"id\":\"11\"}},{\"_\":\"\\r\\n\\r\\naddress=0\\r\\n\\r\\n\",\"$\":{\"value\":\"Garage\",\"type\":\"dac\",\"id\":\"19\",\"externalId\":\"ID:19_20181001_142049\"}},{\"$\":{\"value\":\"Köksentre\",\"type\":\"door\",\"id\":\"14\"}}]}]}}","payloadType":"json","x":370,"y":1480,"wires":[["53141e04.f354a8"]]},{"id":"53141e04.f354a8","type":"function","z":"7760f563.ea0324","name":"Gate","func":"msg.label =msg.payload.batch.event[0].argument[1].$.value;\nif (msg.payload.batch.event[0].$.name === \"controller.door.motorlock.locked\") {\n    node.send({ui_control:{icon:\"fa-2x fa-lock\"},bgcolor:\"red\",color:\"black\",label : msg.label + \" Locked\",payload:msg.payload})\n}else{\n    node.send({ui_control:{icon:\"fa-2x fa-unlock\"},bgcolor:\"green\",color:\"black\",label : msg.label + \" Unlocked\",payload:msg.payload});\n}\nreturn;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":1440,"wires":[["c4f65f57.2a65c"]]},{"id":"c4f65f57.2a65c","type":"ui_button","z":"7760f563.ea0324","name":"button","group":"32666898.eac15","order":11,"width":6,"height":1,"passthru":false,"label":"{{msg.label}}","tooltip":"","color":"{{msg.color}}","bgcolor":"{{msg.bgcolor}}","icon":"{{msg.icon}}","payload":"pushed","payloadType":"str","topic":"","x":680,"y":1440,"wires":[["d6d6a27b.5b5538"]]},{"id":"d6d6a27b.5b5538","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":1440,"wires":[]},{"id":"32666898.eac15","type":"ui_group","z":"","name":"Motorlås","tab":"a54c3d54.22c4f8","order":1,"disp":true,"width":6,"collapse":false},{"id":"a54c3d54.22c4f8","type":"ui_tab","z":"","name":"Maria Åkerberg AB","icon":"dashboard","disabled":false,"hidden":false}]

Hi, Could you please tell me what your ui_control looks like ?

Sorry i do not understand your question

Could you explain the references to ui_control in your code ?

node.send({ui_control:{icon:"fa-2x fa-lock"},bgcolor:"red",color:"black",label : msg.label + " Locked",payload:msg.payload})

It is a reference to an icon.

from
The Icon can be defined, as either a Material Design icon (e.g. 'check', 'close') or a Font Awesome icon (e.g. 'fa-fire') , or a Weather icon. You can use the full set of google material icons if you add 'mi-' to the icon name. e.g. 'mi-videogame_asset'.

Thank you. Does the ui_control exist in your flow and if so could you show the code ?

The reason I ask is because I am trying to understand the details of mvc message flow. I am also trying to make ui_template buttons with push/latching/radio and repeat functions.

no there is no ui_control node.

Thank you. Got it.

Thanks!!
This was exaktly what i was looking for!!

Best Regards Niclas

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