Reset message arrival on delay and trigger node, timing problem

I need help to fix an unwanted situation in my flow (one button LED RGBW light control).

description for the flow
There is one input button (pressed and released state). if pressed a 1 is send, released a zero is send (trigger after inject node to simulate a button)
under 300ms pressed/active the output has to toggle (on/off and cycle through power states/modes)
over 300ms duration the output has to poll (dim control)

now the problem itself
If pressed duration is near 300ms, the 300ms trigger begins to poll. My toughts goes for message arrival at the 300ms poll trigger (reset message (btn release) arrives polling trigger first and on delay node after it sends the message out of it to polling trigger)

How i can avoid/fix the unwanted reset situation at trigger and delay?
I know that nodered works as it should, so further explanations should not be necessary.

Thanks

[{"id":"16097e45e0489976","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"3d88403e6b2ef8b9","type":"switch","z":"16097e45e0489976","name":"press/release","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":620,"y":240,"wires":[["7f8095b2404aba2f","4bf2504c5881be33","31e1c30598d90681"],["492058a455e0fcaf","1de92b88e8ac97fe","4bf2504c5881be33"]],"outputLabels":["press","release"]},{"id":"492058a455e0fcaf","type":"change","z":"16097e45e0489976","name":"setze msg.reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":360,"wires":[["0806d9cf5ac5e713","31e1c30598d90681"]]},{"id":"9b547850f32b76a6","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":80,"wires":[["c8089b162c1d2813"]]},{"id":"c8089b162c1d2813","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":80,"wires":[["a53acddb8cc3aa57"]]},{"id":"1d7dd4efb9c10b88","type":"debug","z":"16097e45e0489976","name":"polling","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1330,"y":400,"wires":[]},{"id":"7f8095b2404aba2f","type":"function","z":"16097e45e0489976","name":"set timestamp","func":"var input = msg.payload.circuit; // Digitaler Eingang aus msg Objekt\nvar flow_pressed = 'DI_' + input + '_pressed';   // als SubFlow '$parent.DI_' + input + '_pressed'\n\nvar pressed = new Date();\nflow.set(flow_pressed, pressed.getTime());\n\n//return msg;","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":60,"wires":[]},{"id":"c652f2fc7b8de7aa","type":"function","z":"16097e45e0489976","name":"delete timestamp","func":"var input = msg.payload.circuit; // Digitaler Eingang aus msg Objekt\nvar flow_pressed = 'DI_' + input + '_pressed';   // als SubFlow '$parent.DI_' + input + '_pressed'\n\nflow.set(flow_pressed, 0);\nnode.warn(flow_pressed + \" is removed\")\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":180,"wires":[[]]},{"id":"4bf2504c5881be33","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"2","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1060,"y":140,"wires":[["c652f2fc7b8de7aa"]]},{"id":"1de92b88e8ac97fe","type":"function","z":"16097e45e0489976","name":"duration","func":"var input = msg.payload.circuit; // Digitaler Eingang aus msg Objekt\nvar flow_pressed = 'DI_' + input + '_pressed';   // als SubFlow '$parent.DI_' + input + '_pressed'\nvar input_pressed = new Date(flow.get(flow_pressed));\nvar input_released = new Date();\n\nvar pressed_duration = ( input_released.getTime() - input_pressed.getTime() );\n\nif(pressed_duration <= 300) \n{\n    node.warn(\"<=300ms\");\n    return msg;\n}\nelse\n{\n    node.warn(\">300ms\");\n    return {};\n}\n//node.warn(pressed_released_duration + \" Millisekunden\");\n\n// return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":260,"wires":[["e1fdf45980e213fa"]]},{"id":"a53acddb8cc3aa57","type":"change","z":"16097e45e0489976","name":"","rules":[{"t":"set","p":"payload.circuit","pt":"msg","to":"99","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":200,"wires":[["3d88403e6b2ef8b9"]]},{"id":"0806d9cf5ac5e713","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"-300","extend":true,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1110,"y":400,"wires":[["1d7dd4efb9c10b88","4bf2504c5881be33"]]},{"id":"8e169d02cbf94c47","type":"comment","z":"16097e45e0489976","name":"Polling über 300ms Druckdauer","info":"","x":1090,"y":300,"wires":[]},{"id":"20df2298f121dec7","type":"comment","z":"16097e45e0489976","name":"Reset","info":"","x":1010,"y":100,"wires":[]},{"id":"e1fdf45980e213fa","type":"debug","z":"16097e45e0489976","name":"toggle","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":260,"wires":[]},{"id":"31e1c30598d90681","type":"delay","z":"16097e45e0489976","name":"","pauseType":"delay","timeout":"300","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1060,"y":340,"wires":[["0806d9cf5ac5e713"]]},{"id":"8739a41002887189","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":120,"wires":[["de93b8a176fc75b4"]]},{"id":"de93b8a176fc75b4","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"298","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":120,"wires":[["a53acddb8cc3aa57"]]},{"id":"d64142e358388ecb","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":160,"wires":[["f07127d584d119bc"]]},{"id":"f07127d584d119bc","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"299","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":160,"wires":[["a53acddb8cc3aa57"]]},{"id":"e299ff68fe0a222a","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":200,"wires":[["efa4fbf27796ba53"]]},{"id":"efa4fbf27796ba53","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"300","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":200,"wires":[["a53acddb8cc3aa57"]]},{"id":"226aae708156c4c4","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":240,"wires":[["7e839ebf4ad94a87"]]},{"id":"7e839ebf4ad94a87","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"301","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":240,"wires":[["a53acddb8cc3aa57"]]},{"id":"fca7f008d8991685","type":"comment","z":"16097e45e0489976","name":"Toggle","info":"","x":1010,"y":220,"wires":[]},{"id":"1810bae81aaa5ecd","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":280,"wires":[["e8547b874fa672da"]]},{"id":"e8547b874fa672da","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"302","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":280,"wires":[["a53acddb8cc3aa57"]]},{"id":"98057a53c4d4fee1","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":320,"wires":[["d9e246794279c9c9"]]},{"id":"d9e246794279c9c9","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"303","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":320,"wires":[["a53acddb8cc3aa57"]]},{"id":"d085ac0103b5ea3e","type":"inject","z":"16097e45e0489976","name":"1","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":360,"wires":[["6099eebda1991729"]]},{"id":"6099eebda1991729","type":"trigger","z":"16097e45e0489976","name":"","op1":"","op2":"{ \"value\": 0 }","op1type":"pay","op2type":"json","duration":"1000","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":360,"wires":[["a53acddb8cc3aa57"]]}]

I am not 100% sure I get what you mean, but you have to accept that there may be tricky things happening with times as the messages go through nodes. The nodes may take a couple of milliseconds to parse thing and do their job.

I don't get why you have the 300 ms delay node (bottom-ish right-ish).

So ideally if the press/release time is less than 300 you want a toggle output.
If it is longer than 300, you want the output to poll.

I added two extra inject nodes to manually press/release the button so I could get a better picture.
The poll message doesn't change if I press and hold the button.

I simply get the same message (payload part) repeated.

{"value":1,"circuit":"99"}

Re-reading I think I am more confused now than before.

I think that if the press time is less than 300 you want to toggle the message like an ON/OFF signal.

If it is more than 300, you want to send a message to dim the light - yes?

This may do better for you.

It is NOT perfect. You will have to work on the two function nodes to get the right message sent.

I didn't do that because I wanted to keep things separate so you could (I hope) see what I did better at this time.

[{"id":"b873f10b288698fe","type":"switch","z":"16097e45e0489976","name":"press/release","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":620,"y":740,"wires":[["b4c4f7a577994418","1996dd019135964e"],["8f85daf71abf60e5","f231138ea316becb"]],"outputLabels":["press","release"]},{"id":"8c4e83d277d359ec","type":"change","z":"16097e45e0489976","name":"","rules":[{"t":"set","p":"payload.circuit","pt":"msg","to":"99","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":700,"wires":[["b873f10b288698fe"]]},{"id":"b4c4f7a577994418","type":"function","z":"16097e45e0489976","name":"set timestamp","func":"var input = msg.payload.circuit; // Digitaler Eingang aus msg Objekt\nvar flow_pressed = 'DI_' + input + '_pressed';   // als SubFlow '$parent.DI_' + input + '_pressed'\n\nvar pressed = new Date();\nflow.set(flow_pressed, pressed.getTime());\n\n//return msg;","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":650,"wires":[]},{"id":"ccd22127bed7373c","type":"trigger","z":"16097e45e0489976","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-300","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":990,"y":740,"wires":[["9388cebdc1cfd9f7"]]},{"id":"8f85daf71abf60e5","type":"change","z":"16097e45e0489976","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":830,"wires":[["1996dd019135964e","ccd22127bed7373c"]]},{"id":"9388cebdc1cfd9f7","type":"debug","z":"16097e45e0489976","name":"polling","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1180,"y":740,"wires":[]},{"id":"1996dd019135964e","type":"trigger","z":"16097e45e0489976","name":"Delay","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"300","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":810,"y":740,"wires":[["ccd22127bed7373c"]]},{"id":"f231138ea316becb","type":"function","z":"16097e45e0489976","name":"duration","func":"var input = msg.payload.circuit; // Digitaler Eingang aus msg Objekt\nvar flow_pressed = 'DI_' + input + '_pressed';   // als SubFlow '$parent.DI_' + input + '_pressed'\nvar input_pressed = new Date(flow.get(flow_pressed));\nvar input_released = new Date();\n\nvar pressed_duration = ( input_released.getTime() - input_pressed.getTime() );\n\nif(pressed_duration <= 300) \n{\n    node.warn(\"<=300ms\");\n    return msg;\n}\nelse\n{\n    node.warn(\">300ms\");\n    return {};\n}\n//node.warn(pressed_released_duration + \" Millisekunden\");\n\n// return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":690,"wires":[["70e34760a8ee37c1"]]},{"id":"4c63d027d572c9c3","type":"debug","z":"16097e45e0489976","name":"toggle","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1120,"y":690,"wires":[]},{"id":"70e34760a8ee37c1","type":"function","z":"16097e45e0489976","name":"toggle","func":"let x = context.get(\"counter\") || 0;\nif (x == 0)\n{\n    msg.payload = \"A\";\n} else\nif (x == 1)\n{\n    msg.payload = \"B\";\n}\nx = (x + 1) % 2;\ncontext.set(\"counter\", x);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":690,"wires":[["4c63d027d572c9c3"]]}]

There are different things I wanted to reach.

Short press should toggle on/off
If time last release and new press is under 2 seconds, on/off toggle becomes colour changer.
This is realized with this temporary 2 second time range (delayed every time with polling to not delete the timestamp)

The long press acts as an dimmer depending on power state and last (hold) action (up/down)

I'll check your flow later :slightly_smiling_face:

So.... Progress?

Does it help you? :slight_smile:

No time so far
Monday I will look on it

didn't change anything.
I will do the part after toggle and polling output. No need to work there.
The trouble comes from active polling (released button) near 300ms duration
Flow should only do poll OR toggle. not both ways.
As an input there is a wall switch (works as button input)

Under 300ms, toggle
over 300ms, poll

See debug log, for 298ms the Flow polls and toggle (i need to avoid this malfunction [design flaw])
Input for flow comes from digital input, button state (1 pressed, 0 released).
To simulate the button, the inject node (press the button) and trigger (release the button after the time) is used.
All my lights in the house are controlled by nodered, now i need the special function without buggy handling (i know somebody will reach this time range, so i need to avoid/fix it)

31.5.2022, 00:45:49node: 298ms duration
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:49node: 298ms duration
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:49node: duration
function : (warn)
"<=300ms"
31.5.2022, 00:45:49node: toggle
msg : Object
{ _msgid: "7bc8100602abbc63", payload: "A" }
31.5.2022, 00:45:49node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:49node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:49node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:50node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:50node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }
31.5.2022, 00:45:50node: polling
msg : Object
{ _msgid: "7bc8100602abbc63", payload: object }

Strange.

I double checked your code and then mine.

I see a big difference.
Using your inject node and 250ms input your payloads don't change each time I press it. With mine they do.... But....

However, it is your project. I am not going to tell you it works if you say it doesn't.
Good luck.

I think you are also being a bit too worried about what happens around the 300ms mark.

Are you that quick that you can press the button for 299ms and 301ms and want it to work to that fine a detail?

I suggest you have a "dead zone" from maybe 290 to 310ms where it just don't do anything.

But again: Your project.

Your original code

It isn't toggling.