Anyone got "humble pie"? (Though that may not be the right analogy.)
Ok, here is where/how I shot myself in the foot:
[{"id":"aeec02b6.7b7bb","type":"function","z":"5ebb71d4.83ed7","name":"","func":"//node.warn(msg.payload);\nvar colour = (msg.payload === \"On-line\")?\"lime\":\"brown\";\n//node.warn(\"Selected colour is \" + colour);\nvar last = context.get('last') || \"blank\";\nif (colour !== last)\n{\n var msg2 = { topic:colour,payload:\"\", background:colour, device_ID:\"MusicPi\", state:4};\n node.status({text:colour});\n context.set('last',colour);\n node.status({fill:\"green\",shape:\"dot\",text:\"Passed\"});\n return msg2\n} else\n{\n node.status({fill:\"red\",shape:\"dot\",text:\"Blocked\"});\n}\n","outputs":1,"noerr":0,"x":320,"y":1120,"wires":[["6b4401ea.d9da2"]]},{"id":"115ef8a5.53ca8f","type":"inject","z":"5ebb71d4.83ed7","name":"Loop","topic":"","payload":"{\"topic\":\"192.168.0.82\",\"host\":\"192.168.0.82\",\"_msgid\":\"c5e09a3a.547618\",\"_topic\":\"MusicPi\",\"payload\":\"Off-line\",\"_event\":\"node:14780718.82f8a9\"}","payloadType":"json","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1120,"wires":[["aeec02b6.7b7bb","350a858d.97dec2"]]},{"id":"6b4401ea.d9da2","type":"debug","z":"5ebb71d4.83ed7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":520,"y":1120,"wires":[]},{"id":"dbd95e64.9f3f58","type":"inject","z":"5ebb71d4.83ed7","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1070,"wires":[["aeec02b6.7b7bb","5d74bdf5.e6fdd4"]]},{"id":"5d74bdf5.e6fdd4","type":"debug","z":"5ebb71d4.83ed7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":310,"y":1060,"wires":[]},{"id":"350a858d.97dec2","type":"debug","z":"5ebb71d4.83ed7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":310,"y":1180,"wires":[]}]
The node keeps injecting the message over and over. Every 3 seconds.
The function node allows the first, then blocks.
To simulate a change of the message, I press the inject node, expecting to see that ONE timestamp come out.
It doesn't.
C'mon! The logic in the node isn't rocket science.
Get the colour. (first line)
Get the previous value. (Line 2)
Compare the two (line 3)
If they are different:
Construct a new message payload.
Set the node.status
Save the new value
Return the new message
else
Set the node status to blocked and do NOT send any message.
I know/have heard this is a "non-cocha" way and all nodes should have a "return msg at the end"
But... As NR is asyncronous, the timing of message arrival is unknown.
If the payload doesn't meet the requirements, it is simply dropped.
var colour = (msg.payload === "On-line")?"lime":"brown";
//node.warn("Selected colour is " + colour);
var last = context.get('last') || "blank";
if (colour !== last)
{
var msg2 = { topic:colour,payload:"", background:colour, device_ID:"MusicPi", state:4};
node.status({text:colour});
context.set('last',colour);
node.status({fill:"green",shape:"dot",text:"Passed"});
return msg2
} else
{
node.status({fill:"red",shape:"dot",text:"Blocked"});
}