Queueing messages - need help - can this flow be optimised?

All the nested if's will bite you at some point.

Isn't the goal to store everything and only pass when the state is on ?

1 Like

correct. That is the idea

This is the latest version after a bit of tweaking and bug fixing.

It seems to work as expected.

[{"id":"993cfacc.ee679","type":"function","z":"26262ba1.62dcbc","name":"New version","func":"//  flag: 0 - block 1 - pass.\n//  hold - stored colour when bulb is `off`.\n//  held - retreived name of colour when bulb was `off`.\nvar flag = context.get(\"flag\") || 0;\nvar held = \"\";\nvar x = \"\";\nif (msg.topic == \"control\")\n{\n    //\n    if (msg.payload == \"On\")\n    {\n        context.set(\"flag\",1);\n        node.status({fill:\"green\",shape:\"dot\",text:\"Pass\"});\n        \n        held = context.get(\"hold\") || 0;\n    \n        if (held)\n        {\n            x = context.get(\"hold\") || msg.payload;\n            msg = {payload: \"On\", colour: x};\n            //  Wipe stored value.\n            context.set(\"hold\",0);\n        }\n    }\n    if (msg.payload == \"Off\")\n    {\n        context.set(\"flag\",0)\n        node.status({fill:\"red\",shape:\"dot\",text:\"Block\"});\n        msg.payload = \"Off\";\n    }\n    return msg;\n}\n\n//node.warn(\"Here\");\n\nif (flag)\n{\n    //  Pass message.\n//    node.warn(\"Pass\");\n    \n    //  Get stored values.\n    held = context.get(\"hold\") || 0;\n    if (held)\n    {\n        //  Build message.\n        x = context.get(\"hold\");\n        msg = {payload: \"On\", colour: x};\n        context.set(\"hold\",0);          //  not needed?\n    }\n    else\n    {\n        x = msg.payload;\n        msg = {payload: \"On\", colour: x};\n    }\n    \n    \n    return msg;\n    //\n} else\n{\n    //  Block (store anything).\n    context.set(\"hold\",msg.payload);\n    return;\n}\n","outputs":1,"noerr":0,"x":2450,"y":4390,"wires":[["451d442e.3fbdec"]]},{"id":"451d442e.3fbdec","type":"debug","z":"26262ba1.62dcbc","name":"OUTPUT 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2620,"y":4390,"wires":[]},{"id":"88e2a0a2.e0bc4","type":"inject","z":"26262ba1.62dcbc","name":"Red","topic":"","payload":"red","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2270,"y":4390,"wires":[["993cfacc.ee679"]]},{"id":"a8c208f.2f449f8","type":"change","z":"26262ba1.62dcbc","name":"control","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2280,"y":4340,"wires":[["993cfacc.ee679"]]},{"id":"d6efcd50.bba668","type":"inject","z":"26262ba1.62dcbc","name":"Green","topic":"","payload":"green","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2270,"y":4440,"wires":[["993cfacc.ee679"]]},{"id":"72ebb868.b4fbf8","type":"inject","z":"26262ba1.62dcbc","name":"","topic":"","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2280,"y":4250,"wires":[["a8c208f.2f449f8"]]},{"id":"e6dce07c.c37c88","type":"inject","z":"26262ba1.62dcbc","name":"","topic":"","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2280,"y":4290,"wires":[["a8c208f.2f449f8"]]}]

Until you press the on button messages (colours) won't get through.
When you press the on button an On message will be sent.
If a colour button has been pressed before hand, it will add the colour. Otherwise it will be simply On.

Pressing the colour buttons you get them going through.

Pressing the Off button turns the bulb off. (to say)

If while off the colour is changed, when the on button is pressed an On signal will be sent with the new colour.

Subsequent pressing of the colour button will have them pass through.

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