Send 1st msg, delay from value in context, send second msg

Hi all,

I am new in visual or real programming, so while I learning java script, I would appreciate some help from your side.
I need function node, which will say:

  1. send 1st message immediately when arrive to input
    • then
  2. delay second message with value from context (if 2nd message never arrive go to 1st step after delay countdown)
    • after
  3. release second message and set delay with new value from context (if 3th message never arrive go to 1st step after delay countdown)
  • repeat above if messages still arriving

Background:
I would like to control roller shutters with relay board (one relay for up and next one for down). Main problem, which I didn't solve yet is situation when I would like to open shutter and after close it too soon --> factory reset of shutter. I don t want that, because after shutter dont know where are its up or down limits!

Instead of UP, DOWN and STOP buttons I would like to have slider, thats why I have this issue for which I dont find any existing node (or if there any, I dont understand them (yet)). I tried with all nodes with word "blinds", "garage doors", "sun position" or all general nodes in Node red.

Below is flow, which I created till now, I would place function node after slider or before trigger nodes 01+// & 02+// which turning relays for up and down.

All comments related below flow are welcome!
Thanks in advance.

[{"id":"d0cfe6595181753a","type":"subflow","name":"Difference (2)","info":"","category":"","in":[{"x":60,"y":120,"wires":[{"id":"e7c96ebf36d88b80"},{"id":"b5aad626ba68fcd5"}]}],"out":[{"x":805,"y":283.5000305175781,"wires":[{"id":"23c577fdb8563eee","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","outputLabels":["reading"]},{"id":"b5aad626ba68fcd5","type":"function","z":"d0cfe6595181753a","name":"Current Reading","func":"//set a topic to join later\nmsg.topic = 'curr'; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":180,"wires":[["6112457c89e9cbfc"]]},{"id":"e7c96ebf36d88b80","type":"delay","z":"d0cfe6595181753a","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"outputs":1,"x":205.75,"y":327.0000305175781,"wires":[["f88e21ced7037eeb"]]},{"id":"f88e21ced7037eeb","type":"function","z":"d0cfe6595181753a","name":"Previous Reading","func":"//set a topic to join later\nmsg.topic = 'prev'; \n\nreturn msg;","outputs":1,"noerr":0,"x":527,"y":326.00006103515625,"wires":[["6112457c89e9cbfc"]]},{"id":"6112457c89e9cbfc","type":"join","z":"d0cfe6595181753a","name":"Join each topic","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":580,"y":179.25003051757812,"wires":[["23c577fdb8563eee"]]},{"id":"23c577fdb8563eee","type":"function","z":"d0cfe6595181753a","name":"Difference","func":"var c = msg.payload.curr - msg.payload.prev;\n\n// Calculate\nif (msg.topic == 'curr'){\n    msg.payload = +c;\n    return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":828.75,"y":179.00003051757812,"wires":[[]]},{"id":"17aed9c3c0ded0eb","type":"comment","z":"d0cfe6595181753a","name":"input","info":"Connect your Reading as msg.payload","x":70,"y":80,"wires":[]},{"id":"4a29f8a77959a52a","type":"comment","z":"d0cfe6595181753a","name":"Delay 1msg / sec","info":"to be able to get previous value \none message later","x":207.5,"y":367.7500305175781,"wires":[]},{"id":"756a6ff28c2f96f1","type":"comment","z":"d0cfe6595181753a","name":"Add Topic","info":"","x":556.25,"y":364.00006103515625,"wires":[]},{"id":"592befa21166723f","type":"comment","z":"d0cfe6595181753a","name":"Add Topic","info":"","x":240,"y":137.75003051757812,"wires":[]},{"id":"560e6b26529343c6","type":"comment","z":"d0cfe6595181753a","name":"Calculate the Diff","info":"","x":840,"y":137.75003051757812,"wires":[]},{"id":"0c636e540311667c","type":"comment","z":"d0cfe6595181753a","name":"Join / Combine msgs","info":"","x":580,"y":137.75003051757812,"wires":[]},{"id":"498292a7bd6248ce","type":"comment","z":"d0cfe6595181753a","name":"1st Out is Difference","info":"","x":930,"y":284.7500305175781,"wires":[]},{"id":"b14e10184073c43b","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b1e6ddcea3c1d650","type":"ui_slider","z":"b14e10184073c43b","name":"","label":"Roller Shutter","tooltip":"","group":"9279c889.8c3428","order":5,"width":"2","height":"7","passthru":false,"outs":"end","topic":"delay","topicType":"msg","min":0,"max":"100","step":"20","className":"","x":420,"y":280,"wires":[["fe5337ceb7b050ed","3856469dc25d7b3c"]]},{"id":"db19c3fbc1c8e9bf","type":"subflow:d0cfe6595181753a","z":"b14e10184073c43b","name":"Diff","env":[],"x":750,"y":280,"wires":[["075e99c4072715a0"]]},{"id":"075e99c4072715a0","type":"switch","z":"b14e10184073c43b","name":"- / +","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"3000","v2t":"num"},{"t":"cont","v":"-","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":280,"wires":[["a3efdae5d397e8b8"],["89fd4dca7cd0e356"]]},{"id":"89fd4dca7cd0e356","type":"range","z":"b14e10184073c43b","minin":"0","maxin":"100","minout":"0","maxout":"16","action":"scale","round":false,"property":"payload","name":"0-16","x":1030,"y":300,"wires":[["18f00d0a91b22472"]]},{"id":"18f00d0a91b22472","type":"calculator","z":"b14e10184073c43b","name":"x1000","inputMsgField":"payload","outputMsgField":"payload","operation":"mult","constant":"1000","round":false,"decimals":"0","x":1150,"y":300,"wires":[["87362f0c192f89dc"]]},{"id":"87362f0c192f89dc","type":"calculator","z":"b14e10184073c43b","name":"- to +","inputMsgField":"payload","outputMsgField":"payload","operation":"abs","constant":"","round":false,"decimals":"0","x":1270,"y":300,"wires":[["d632d3788fae41c4","835a6e899385a6a0"]]},{"id":"a3efdae5d397e8b8","type":"range","z":"b14e10184073c43b","minin":"0","maxin":"100","minout":"0","maxout":"15","action":"scale","round":false,"property":"payload","name":"0-15","x":1030,"y":260,"wires":[["f0fc9edefd65c533"]]},{"id":"f0fc9edefd65c533","type":"calculator","z":"b14e10184073c43b","name":"x1000","inputMsgField":"payload","outputMsgField":"payload","operation":"mult","constant":"1000","round":false,"decimals":"0","x":1150,"y":260,"wires":[["3d6b54c47a0445af","e8b2e28847d7fbe8"]]},{"id":"d632d3788fae41c4","type":"change","z":"b14e10184073c43b","name":"msg.delay","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1430,"y":340,"wires":[["54af849774736819"]]},{"id":"54af849774736819","type":"trigger","z":"b14e10184073c43b","name":"Down (stop) 02-//","op1":"","op2":"02-//","op1type":"nul","op2type":"str","duration":"1","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1650,"y":340,"wires":[["232405e2e8547540"]]},{"id":"835a6e899385a6a0","type":"trigger","z":"b14e10184073c43b","name":"Down (start) 02+//","op1":"02+//","op2":"","op1type":"str","op2type":"nul","duration":"1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1660,"y":300,"wires":[["232405e2e8547540"]]},{"id":"3d6b54c47a0445af","type":"change","z":"b14e10184073c43b","name":"msg.delay","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1430,"y":220,"wires":[["e3d72330d352f687"]]},{"id":"e8b2e28847d7fbe8","type":"trigger","z":"b14e10184073c43b","name":"Up (start) 01+//","op1":"01+//","op2":"","op1type":"str","op2type":"nul","duration":"1","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1640,"y":260,"wires":[["232405e2e8547540"]]},{"id":"fe5337ceb7b050ed","type":"change","z":"b14e10184073c43b","name":"","rules":[{"t":"set","p":"rollerShutter","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":200,"wires":[[]]},{"id":"ba4f19501a2ebda4","type":"inject","z":"b14e10184073c43b","name":"ContextBedroom","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"rollerShutter","payloadType":"flow","x":230,"y":280,"wires":[["b1e6ddcea3c1d650"]]},{"id":"3856469dc25d7b3c","type":"switch","z":"b14e10184073c43b","name":"0-100","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"100","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":280,"wires":[["db19c3fbc1c8e9bf"]]},{"id":"e3d72330d352f687","type":"trigger","z":"b14e10184073c43b","name":"Up (stop) 01-//","op1":"","op2":"01-//","op1type":"nul","op2type":"str","duration":"1","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1650,"y":220,"wires":[["232405e2e8547540"]]},{"id":"232405e2e8547540","type":"debug","z":"b14e10184073c43b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1950,"y":260,"wires":[]},{"id":"00d9b8d765449dd4","type":"comment","z":"b14e10184073c43b","name":"Value of slider in case, if Node-red is restarted by some reason","info":"Value of slider in case, if Node-red is restarted by some reason","x":220,"y":160,"wires":[]},{"id":"0172ccc739975f04","type":"comment","z":"b14e10184073c43b","name":"For some reason up and down time is different.","info":"","x":1040,"y":340,"wires":[]},{"id":"3568b61a72688abb","type":"comment","z":"b14e10184073c43b","name":"Diff node: Calculation of difference of time (for example, seconds are needed from 20% to 40%)","info":"","x":770,"y":200,"wires":[]},{"id":"0bb53a723f4d87d5","type":"comment","z":"b14e10184073c43b","name":"Plan is to take context message from below switch nodes.","info":"","x":1430,"y":160,"wires":[]},{"id":"9279c889.8c3428","type":"ui_group","name":"Rolete1","tab":"fdc7999227dec6fd","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"fdc7999227dec6fd","type":"ui_tab","name":"Rolete","icon":"dashboard","disabled":false,"hidden":false}]