Output and delay until condition

Hi guys - looking for help - something I can't get my head around - can anyone help...

I've an OPPLE switch which sends "HOLD" as payload then when you let go of the button, sends "RELEASE".

I want an NR function that once it gets HOLD payload will iterate an output until it gets a RELEASE in the input - lets say 250ms delay each time.

So on receiving "hold" it might output "red",and after 250ms send "green", third time "blue" and keep on going until the button sends a "release. It is for colour control of lamps.... seems easy but can't quite get my head into it.

Something like this should get you moving...


[{"id":"bcc8bbbf106ec10f","type":"function","z":"f542fe9c.732af","name":"colour cycle","func":"/**** colour list - update as required *****/\nconst colours = [ \n    \"red\", \n    \"blue\", \n    \"green\",\n];\n\n/**** dont change *****/\nlet colour = context.get(\"colour\"); //restore last colour\ncolour = colour == null ? 0 : colour + 1;//inc colour\nif(colour >= colours.length) {\n    colour = 0;\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1970,"y":780,"wires":[["fc3d23dad0eccca5"]]},{"id":"fc3d23dad0eccca5","type":"debug","z":"f542fe9c.732af","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1970,"y":820,"wires":[]},{"id":"9678bb8c7ae13572","type":"inject","z":"f542fe9c.732af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"HOLD","payloadType":"str","x":1590,"y":660,"wires":[["743b3f71deec01b4"]]},{"id":"be4e10b6f93b781e","type":"inject","z":"f542fe9c.732af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"RELEASE","payloadType":"str","x":1600,"y":700,"wires":[["743b3f71deec01b4"]]},{"id":"d91bbe63f60e0448","type":"inject","z":"f542fe9c.732af","name":"250ms","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.25","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1600,"y":760,"wires":[["a5d7fc6c3ed6c6d6"]]},{"id":"a5d7fc6c3ed6c6d6","type":"switch","z":"f542fe9c.732af","name":"HOLD yes/no?","property":"HOLD","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1780,"y":760,"wires":[[],["bcc8bbbf106ec10f"]]},{"id":"743b3f71deec01b4","type":"function","z":"f542fe9c.732af","name":"set flow.HOLD","func":"if(msg.payload === \"HOLD\") {\n    flow.set(\"HOLD\", true);\n    node.status({fill:\"red\",shape:\"ring\",text:\"HOLD\"});\n} else if (msg.payload === \"RELEASE\") {\n    flow.set(\"HOLD\", false);\n    node.status({fill:\"green\",shape:\"ring\",text:\"RELEASE\"});\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1780,"y":680,"wires":[[]]}]

Sorry, just re-read - you want the opposite :slight_smile:


(just move the wire on the colour cycle function)

If that works you are a star. I have trouble getting my head around async nature of NR. I spent decades programming in C :slight_smile:

I just pasted in your code - looks like it will do it if it has an incrementing number in there - i'll go look

Yup, got to get colours and I want several of these but thats the general idea - thank you.

At the end of the day - many thanks that worked just fine once I changed "0xaaaaaa" to "#aaaaaa" :-)- I just wasted hours on this because if you HOLD the button for several seconds to cycle through colours, the RELEASE on the Aqara buttons does not reliably occur. SUCH a shame as this was going very well. Possibly holding the button for a while kills the battery... not sure.

How about using the HOLD to SET the cycle process so that a single press changes the colour and then HOLD again to RESET. Should probably have a RESET delay so that if a single press does not occur the system resets to normal mode

Well, Buckskin, you Have started me thinking again - but watch - a short (couple of seconds) hold (then release) and a single click..
Did you notice that double-press event (not singl) after the release. Now watch a several seconds hold... then release - then single click.

Did you notice that on BOTH occasions, the button gave off a double at the end and the long one never gave a release. I'd have to accept that double - which prevents using double for anything else... but that MIGHT be an option - trust me I'm not losing the lot here - there is something up with the Aqara button (battery says 100%) - I just hope if this works that it is consistent from device to device and across teh rang.. I'll go back nowto Steve-Mcl's suggestion with this new information.

My thanks to all for coming in with help. What I settled on - using the node-red-contrib-zigbee2mqtt input node, after realising that the AQARA RELEASE was not going to be reliable, was this: lets call the buttons A&B. Single-press A turns lights off, single press B turns them on. HOLD A resets sets colour to white (but turns lights off). HOLD B moves to the next colour in the sequence and if necessary turns the lights on. That seems to be running 100% reliably and isn't a bad compromise. double-click and triple-click seem obvious to me as a techie but not at ALL obvious to my non-techie wife.

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