I have an instance where msg.payload will not have all the keys defined for items which I want to show in a Dashboard 2.0 Template node. In the example below (greatly simplified from my needs) I will receive a message where msg.payload.x may be defined OR msg.payload.y may be defined, but not both together. This gets more complicated in that I will eventually have (still simplified):
- msg.payload.zone1.x
- msg.payload.zone1.y
- msg.payload.zone2.x
- msg.payload.zone2.y
and want to save them as different object:
- zone1.x
- zone1.y
- zone2.x
- zone2.y
My problem is that when I receive msg.payload.x it clears out any existing msg.payload.y. I am guessing the is a Vue way to handle this but cannot figure it out inside the node. Admittedly, I have never used Vue prior to recent attempts to do things with the Template node.
Below is the template for my basic example. What am I missing?
<template>
<p>X = {{ msg.payload?.x }}</p>
<p>Y = {{ msg.payload?.y }}</p>
<p>X+Y = <div v-html="addValue"></div></p>
<p>Sensor Angle = {{ msg.angle }}</p>
</template>
<script>
export default {
watch: {
//Watch for any changes to msg and act on them.
//This is run when the dashboard is loaded as well
msg: function () {
this.addValue = this.msg.payload.x + this.msg.payload.y;
}
},
}
</script>
Here is a sample flow with inject nodes I have used to test with:
[{"id":"f3564535b8fc6abf","type":"inject","z":"01a9e096a69c7202","name":"","props":[{"p":"payload.x","v":"3","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1590,"y":460,"wires":[["cef846095e925344"]]},{"id":"1cdd9cec45819237","type":"inject","z":"01a9e096a69c7202","name":"","props":[{"p":"payload.y","v":"6","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1590,"y":500,"wires":[["cef846095e925344"]]},{"id":"b9441e0457637205","type":"inject","z":"01a9e096a69c7202","name":"","props":[{"p":"payload.x","v":"5","vt":"num"},{"p":"payload.y","v":"7","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1590,"y":540,"wires":[["cef846095e925344"]]},{"id":"194cdce48b33540a","type":"inject","z":"01a9e096a69c7202","name":"","props":[{"p":"angle","v":"55","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1590,"y":580,"wires":[["cef846095e925344"]]},{"id":"cef846095e925344","type":"ui-template","z":"01a9e096a69c7202","group":"0db8d9f5bdc794eb","page":"","ui":"","name":"Template test 3","order":3,"width":"12","height":"6","head":"","format":"<template>\n <p>X = {{ msg.payload?.x }}</p>\n <p>Y = {{ msg.payload?.y }}</p>\n <p>X+Y = <div v-html=\"addValue\"></div></p>\n <p>Sensor Angle = {{ msg.angle }}</p>\n</template>\n\n<script>\n export default {\n watch: {\n //Watch for any changes to msg and act on them.\n //This is run when the dashboard is loaded as well\n msg: function () {\n this.addValue = this.msg.payload?.x + this.msg.payload?.y;\n }\n },\n }\n</script>\n","storeOutMessages":true,"passthru":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":1840,"y":500,"wires":[[]]},{"id":"0db8d9f5bdc794eb","type":"ui-group","name":"My Group","page":"92d2e5483adf6001","width":"12","height":"6","order":1,"showTitle":true,"className":"","visible":"true","disabled":"false","groupType":"default"},{"id":"92d2e5483adf6001","type":"ui-page","name":"Simulation","ui":"4c2bb00f7172c414","path":"/page7","icon":"home","layout":"grid","theme":"d8f055e104cc1454","breakpoints":[{"name":"Default","px":"0","cols":"3"},{"name":"Tablet","px":"576","cols":"6"},{"name":"Small Desktop","px":"768","cols":"9"},{"name":"Desktop","px":"1024","cols":"12"}],"order":2,"className":"","visible":true,"disabled":"false"},{"id":"4c2bb00f7172c414","type":"ui-base","name":"My 1st 2.0 Dashboard","path":"/dashboard","includeClientData":true,"acceptsClientConfig":["ui-notification","ui-control"],"showPathInSidebar":false,"headerContent":"page","navigationStyle":"default","titleBarStyle":"default","notificationDisplayTime":5},{"id":"d8f055e104cc1454","type":"ui-theme","name":"Default Theme","colors":{"surface":"#ffffff","primary":"#0094CE","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"},"sizes":{"pagePadding":"12px","groupGap":"12px","groupBorderRadius":"4px","widgetGap":"12px"}}]