No, in addition the node should have Reset the trigger if msg.payload is Off. That is so that the trigger is reset if the Done node sends an Off message.
} else if (data.initial - msg.payload.dispense < msg.payload.weight)
{msg.payload.state = "On"}
else {msg.payload.state = "Off"}
That doesn't work does it? Does it turn the vibrator off when it should? I would have thought it should be
} else if (data.initial - msg.payload.dispense < msg.payload.weight) {
msg = null // don't send a message as it is already on, or has been turned off by the trigger
} else {
msg.payload = "Off" // enogh lost so stop vibrator
}
Also there needs to be an extra wire from the Link In node to topic:state, so that the Off message gets back to the Join node and into the message.
I have realised that it isn't necessary to save the state in the context so that can be simplified a bit.
Finally, the way you have done the test seems odd to me. To me it seems more logical to do, in words, 'if current weight is greater than initial weight - weight to dispense then vibrator on, else off` rather than the the way you have it. However if your brain is happier the way it is then that is fine.
This seems to work
[{"id":"1600c410e9ef7425","type":"inject","z":"84405ff5.25fa6","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Start","payload":"true","payloadType":"bool","x":170,"y":1580,"wires":[["9377b8a60d5d56b2"]]},{"id":"ca7388684c1ed2b7","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"16","payloadType":"num","x":190,"y":1760,"wires":[["d11829039b7b76f6"]]},{"id":"31e3f6d570e61a23","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14","payloadType":"num","x":190,"y":1800,"wires":[["d11829039b7b76f6"]]},{"id":"da6bf3ebe50437fc","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"13","payloadType":"num","x":190,"y":1840,"wires":[["d11829039b7b76f6"]]},{"id":"627ef94e971e9a87","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12","payloadType":"num","x":190,"y":1880,"wires":[["d11829039b7b76f6"]]},{"id":"d11829039b7b76f6","type":"change","z":"84405ff5.25fa6","name":"topic: weight","rules":[{"t":"set","p":"topic","pt":"msg","to":"weight","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":1720,"wires":[["94dc8259f3faa4f3"]]},{"id":"9377b8a60d5d56b2","type":"trigger","z":"84405ff5.25fa6","name":"","op1":"On","op2":"Off","op1type":"str","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"Off","bytopic":"all","topic":"topic","outputs":1,"x":390,"y":1580,"wires":[["5f352a142459f3bb","7b1d0270c339ebb1"]]},{"id":"7b1d0270c339ebb1","type":"debug","z":"84405ff5.25fa6","name":"Vibrator","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":820,"y":1580,"wires":[]},{"id":"783c58f6ad3c2c18","type":"inject","z":"84405ff5.25fa6","name":"Amount to dispense","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"dispense","payload":"4","payloadType":"num","x":210,"y":1640,"wires":[["94dc8259f3faa4f3"]]},{"id":"94dc8259f3faa4f3","type":"join","z":"84405ff5.25fa6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":590,"y":1720,"wires":[["ac0e1c7a83d98968","27a01dc09a72f0ec"]]},{"id":"5f352a142459f3bb","type":"change","z":"84405ff5.25fa6","name":"topic: state","rules":[{"t":"set","p":"topic","pt":"msg","to":"state","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":1640,"wires":[["94dc8259f3faa4f3"]]},{"id":"ac0e1c7a83d98968","type":"function","z":"84405ff5.25fa6","name":"Done?","func":"/* Determines whether vibration is complete and sends \"Off\" when it is\n * msg.payload.state contains vibration state \"On\" or \"Off\"\n * msg.payload.weight contains current weight measurement\n * msg.payload.dispense contains amount to dispense\n * \n*/\nlet initial = context.get(\"initial\") || msg.payload.weight // pick up starting weight from saved value\n// if not running just keep track of current weight\nif (msg.payload.state === \"Off\") {\n initial = msg.payload.weight\n msg = null // so nothing will be sent at the end\n} else if (initial - msg.payload.dispense < msg.payload.weight) {\n msg = null // don't send anything as it is already on, or has been turned off by the Trigger node\n} else \n{\n msg.payload = \"Off\"\n}\n\ncontext.set(\"initial\", initial)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":1720,"wires":[["7b1d0270c339ebb1","9542e62b072701a9"]]},{"id":"9542e62b072701a9","type":"link out","z":"84405ff5.25fa6","name":"Reset trigger","links":["04cacefa8a368a73"],"x":775,"y":1780,"wires":[]},{"id":"04cacefa8a368a73","type":"link in","z":"84405ff5.25fa6","name":"reset","links":["9542e62b072701a9"],"x":315,"y":1520,"wires":[["9377b8a60d5d56b2","5f352a142459f3bb"]]},{"id":"a497014f24d79d22","type":"comment","z":"84405ff5.25fa6","name":"Reset trigger","info":"","x":370,"y":1480,"wires":[]},{"id":"8cdfac5c617f1fe0","type":"comment","z":"84405ff5.25fa6","name":"Values from sensor","info":"","x":190,"y":1720,"wires":[]},{"id":"a9f4b546eeee6b88","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":190,"y":1920,"wires":[["d11829039b7b76f6"]]},{"id":"5e2ece19ca3c21dc","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9","payloadType":"num","x":189.00867462158203,"y":1957.001708984375,"wires":[["d11829039b7b76f6"]]},{"id":"27a01dc09a72f0ec","type":"debug","z":"84405ff5.25fa6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":1840,"wires":[]}]