Folks, this is a problem which has been hiding for a while.
I just found/noticed it now.
code:
// get existing states
const alarm = flow.get("alarm") || null
const error = flow.get("error") || null
const ip = flow.get("ip") || null
const ntp = flow.get("ntp") || null
const reboot = flow.get("reboot") || null
const wap = flow.get("wap") || null
const count = msg.count
const led = "6"
let wipe = context.get("wipe") || 0
let part = ""
switch(count)
{
case 0:
//
msg.payload = alarm
part = "alarm"
break
case 1:
//
msg.payload = error
part = "error"
break
case 2:
//
msg.payload = ip
part = "ip"
break
case 3:
//
msg.payload = ntp
part = "ntp"
break
case 4:
//
msg.payload = reboot
part = "reboot"
break
case 5:
//
msg.payload = wap
part = "wap"
break
default:
return
}
if (msg.payload.includes("0,0,0"))
{
if (wipe == 1)
{
//
context.set("wipe",0)
msg.wipe = "WIPE"
}
}
else
{
//
node.warn("message detected")
context.set("wipe",1)
}
let text = count + " " + part + " " + wipe + " " + msg.payload
node.status({ text: text })
return msg;
If any of the *OTHER& 4 are set, it works.
But if the reboot is active, it doesn't work.
Here is a cut down version of the flow.
[{"id":"6c644fb52af9437a","type":"inject","z":"d622397fb0e54309","name":"Test OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"X","payloadType":"str","x":120,"y":880,"wires":[["004b052c72742461"]]},{"id":"adfb96554e03f95e","type":"inject","z":"d622397fb0e54309","name":"Test ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":920,"wires":[["004b052c72742461"]]},{"id":"a0454de4be3f0dca","type":"inject","z":"d622397fb0e54309","name":"Test OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"X","payloadType":"str","x":120,"y":1010,"wires":[["7f7bbb7a67565916"]]},{"id":"8b0c3ffcde3482ae","type":"inject","z":"d622397fb0e54309","name":"Test ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":1050,"wires":[["7f7bbb7a67565916"]]},{"id":"7f7bbb7a67565916","type":"junction","z":"d622397fb0e54309","x":210,"y":1030,"wires":[["0e5f69d208054b61"]]},{"id":"004b052c72742461","type":"junction","z":"d622397fb0e54309","x":210,"y":900,"wires":[["98f004ae12845fbf"]]},{"id":"98f004ae12845fbf","type":"function","z":"d622397fb0e54309","name":"Set error Orange","func":"const led = 6;\n//node.warn(\"Message received\");\nconst ic = msg.payload;\nif (ic == \"X\")\n{\n // Clear.\n flow.set(\"error\",\"rgb,\" + led + \",0,0,0\")\n msg.payload = \"OFF\"\n return msg\n}\nelse\n{\n // Set.\n flow.set(\"error\",\"rgb,\" + led + \",42,4,1\")\n msg.payload = \"ON\"\n return msg\n}","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":260,"wires":[[]]},{"id":"0e5f69d208054b61","type":"function","z":"d622397fb0e54309","name":"Set reboot Red","func":"const led = 6;\nconst ic = msg.payload;\nif (ic == \"X\")\n{\n // Clear.\n flow.set(\"reboot\", \"rgb,\" + led + \",0,0,0\");\n msg.payload = \"OFF\";\n return msg;\n}\nelse\n{\n // Set.\n flow.set(\"reboot\", \"rgb,\" + led +\",40,0,0\");\n msg.payload = \"ON\";\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":360,"wires":[[]]},{"id":"7e140af4599b3259","type":"function","z":"d622397fb0e54309","name":"New attempt","func":"// get existing states\nconst alarm = flow.get(\"alarm\") || null\nconst error = flow.get(\"error\") || null\nconst ip = flow.get(\"ip\") || null\nconst ntp = flow.get(\"ntp\") || null\nconst reboot = flow.get(\"reboot\") || null\nconst wap = flow.get(\"wap\") || null\n\nconst count = msg.count\n\nconst led = \"6\"\n\nlet wipe = context.get(\"wipe\") || 0\nlet part = \"\"\n\nswitch(count)\n{\n case 0:\n //\n msg.payload = alarm\n part = \"alarm\"\n break\n case 1:\n //\n msg.payload = error\n part = \"error\"\n break\n case 2:\n //\n msg.payload = ip\n part = \"ip\"\n break\n case 3:\n //\n msg.payload = ntp\n part = \"ntp\"\n break\n case 4:\n //\n msg.payload = reboot\n part = \"reboot\"\n break\n case 5:\n //\n msg.payload = wap\n part = \"wap\"\n break\n default:\n return\n}\n\nif (msg.payload.includes(\"0,0,0\"))\n{\n if (wipe == 1)\n {\n //\n context.set(\"wipe\",0)\n msg.wipe = \"WIPE\"\n }\n}\nelse\n{\n //\n node.warn(\"message detected\")\n context.set(\"wipe\",1)\n}\n\nlet text = count + \" \" + part + \" \" + wipe + \" \" + msg.payload\nnode.status({ text: text })\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":890,"wires":[["7dd9c3cc63903cd9","324b56bd7ba0d8b1"]]},{"id":"67e4989f1980e536","type":"link in","z":"d622397fb0e54309","name":"link in 23","links":["7b81ef3a34ef3473"],"x":1155,"y":890,"wires":[["7e140af4599b3259"]]},{"id":"7b81ef3a34ef3473","type":"link out","z":"d622397fb0e54309","g":"2c3e298e36a52bde","name":"link out 17","mode":"link","links":["67e4989f1980e536"],"x":985,"y":1300,"wires":[]},{"id":"5d523e1e97e92b14","type":"change","z":"d622397fb0e54309","g":"2c3e298e36a52bde","name":"","rules":[{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":915,"y":1380,"wires":[["7b81ef3a34ef3473","8738884cb4ae17a5"]],"l":false},{"id":"eb6045154ed7906c","type":"counter","z":"d622397fb0e54309","g":"2c3e298e36a52bde","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":1,"x":810,"y":1380,"wires":[["5f0f59d5300191ee","5d523e1e97e92b14"]]},{"id":"253a01d8ca28c862","type":"inject","z":"d622397fb0e54309","g":"2c3e298e36a52bde","name":"Step","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":660,"y":1330,"wires":[["eb6045154ed7906c"]]}]
node-red-contrib-counte is used.
overview:
5 conditions exist.
alarm
error
ip
ntp
wap
Every second the count is incremented and that condition is reflected in the message sent out.
0,0,0 is turning the LED off.
But it doesn't quite work that way and a msg.wipe needs to be set also.
But having it constantly set has its own set of problems.
Which is where:
if (msg.payload.includes("0,0,0"))
{
if (wipe == 1)
{
//
context.set("wipe",0)
msg.wipe = "WIPE"
}
}
else
{
//
node.warn("message detected")
context.set("wipe",1)
}
Comes into play.
(there is a bit more, but.....)
So if a real message is sent through it sets the wipe flag.
then if the message contains 0,0,0 AND the wipe flag is set, it adds the msg.wipe part.
THIS WORKS ON ALL EXCEPT the reboot part - and I don't know why.
Yes, the structure may need refining. Two tests and ONE else.
But if it was a problem: why are the other 4 working?
Someone - please.