Haxiboy / node-red-contrib-looptimer-advanced

Node-Red Advanced Looptimer
https://porteleki.hu
2 stars 1 forks source link

Problem overwriting settings with msg.loop #1

Closed classic-audio closed 3 years ago

classic-audio commented 3 years ago

I have tried to use the functions: msg.loop.maxloops and msg.loop.duration

I have tried a few variants, but can not get it working. I get this: "TypeError: Cannot set property 'maxloops' of undefined"

Could you please give an exampel?

Haxiboy commented 3 years ago

Hello! Can you please send me your flow that causing the problem? You could try with this example please.

[ { "id": "d8c9e1f5.911c4", "type": "tab", "label": "Flow 1", "disabled": false, "info": "" }, { "id": "ba1ab29a.50fcd", "type": "inject", "z": "d8c9e1f5.911c4", "name": "Stop With This Inject", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "stop", "payloadType": "str", "x": 230, "y": 220, "wires": [ [ "4c6cf5c2.6d098c" ] ] }, { "id": "23b91d6e.5c9d52", "type": "inject", "z": "d8c9e1f5.911c4", "name": "Override Maxloops 5 times", "props": [ { "p": "payload" }, { "p": "loop.maxloops", "v": "5", "vt": "num" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "You should see this message every 1 sec 5 times + Once the original!!", "payloadType": "str", "x": 230, "y": 60, "wires": [ [ "4c6cf5c2.6d098c" ] ] }, { "id": "7689ebde.dd87f4", "type": "debug", "z": "d8c9e1f5.911c4", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 670, "y": 160, "wires": [] }, { "id": "aed40de3.b0de", "type": "inject", "z": "d8c9e1f5.911c4", "name": "No Override", "props": [ { "p": "payload" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "You should see this message 10 times every 1 second + once original.", "payloadType": "str", "x": 250, "y": 140, "wires": [ [ "4c6cf5c2.6d098c" ] ] }, { "id": "4c6cf5c2.6d098c", "type": "looptimer-advanced", "z": "d8c9e1f5.911c4", "duration": "1", "units": "Second", "maxloops": "10", "maxtimeout": "20", "maxtimeoutunits": "Second", "name": "", "x": 470, "y": 160, "wires": [ [ "7689ebde.dd87f4" ], [] ] }, { "id": "25c40bff.6168f4", "type": "inject", "z": "d8c9e1f5.911c4", "name": "Override Duration 2 sec", "props": [ { "p": "payload" }, { "p": "loop.duration", "v": "2", "vt": "num" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "You should see this message every 2 sec 10 times + Once the original!!", "payloadType": "str", "x": 220, "y": 100, "wires": [ [ "4c6cf5c2.6d098c" ] ] } ]

classic-audio commented 3 years ago

Thank you for your reply. I will see if I can implement this in my flow, however I am looking for a little different approach.

My problem is: Get & Set Loop time and max loops (function node)

[{"id":"293b3f46.3c9c5","type":"tab","label":"Graph","disabled":false,"info":""},{"id":"b279c6d3.087bb8","type":"ui_button","z":"293b3f46.3c9c5","name":"Update","group":"1be1e979.ed2837","order":5,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"grey","icon":"","payload":"1","payloadType":"num","topic":"","x":180,"y":620,"wires":[["9ad1ba07.bb0f78"]]},{"id":"fea4838a.0bcc5","type":"function","z":"293b3f46.3c9c5","name":"Gem max / min","func":"if(msg.topic == 'High'){\n flow.set('max',msg.payload);\n}\n\nif(msg.topic == 'Low'){\n flow.set('min',msg.payload);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":180,"wires":[[]],"icon":"node-red/leveldb.png"},{"id":"d050b445.96bc28","type":"ui_numeric","z":"293b3f46.3c9c5","name":"Set max","label":"Max","tooltip":"","group":"1be1e979.ed2837","order":3,"width":3,"height":1,"wrap":false,"passthru":true,"topic":"High","format":"{{value}}","min":0,"max":"120","step":"5","x":220,"y":160,"wires":[["fea4838a.0bcc5"]]},{"id":"6eb1251e.bc653c","type":"ui_numeric","z":"293b3f46.3c9c5","name":"Set min","label":"Min","tooltip":"","group":"1be1e979.ed2837","order":1,"width":3,"height":1,"wrap":false,"passthru":true,"topic":"Low","format":"{{value}}","min":0,"max":"120","step":"5","x":220,"y":200,"wires":[["fea4838a.0bcc5"]]},{"id":"4fb1ac15.670664","type":"ui_level","z":"293b3f46.3c9c5","group":"1be1e979.ed2837","order":4,"width":7,"height":1,"name":"","label":"Niveau","colorHi":"#ff9900","colorWarn":"#ff9900","colorNormal":"#ff9900","colorOff":"#595959","min":0,"max":"200","segWarn":"","segHigh":"","unit":"step","layout":"sh","channelA":"","channelB":"","decimals":0,"animations":"soft","shape":"1","colorschema":"valuedriven","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":false,"hideValue":false,"tickmode":"off","peakmode":false,"property":"payload","peaktime":3000,"x":690,"y":680,"wires":[]},{"id":"9ad1ba07.bb0f78","type":"function","z":"293b3f46.3c9c5","name":"Calculate graph settings","func":"var hi = flow.get('max');\nvar lo = flow.get('min');\n\nmsg.control = {min:lo, max:hi}\nmsg.payload = Math.floor(Math.random()*100)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":680,"wires":[["4fb1ac15.670664"]]},{"id":"19dfb004.2cd1","type":"inject","z":"293b3f46.3c9c5","name":"Start setting","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":210,"y":680,"wires":[["9ad1ba07.bb0f78"]]},{"id":"24977ac3.c016c6","type":"ui_button","z":"293b3f46.3c9c5","name":"Start","group":"1be1e979.ed2837","order":7,"width":3,"height":1,"passthru":false,"label":"Start","tooltip":"","color":"","bgcolor":"","icon":"","payload":"start","payloadType":"str","topic":"control","x":230,"y":280,"wires":[["19454753.10b359"]]},{"id":"3071faf2.5f9ae6","type":"ui_button","z":"293b3f46.3c9c5","name":"Stop","group":"1be1e979.ed2837","order":9,"width":3,"height":1,"passthru":false,"label":"Stop","tooltip":"","color":"","bgcolor":"","icon":"","payload":"stop","payloadType":"str","topic":"control","x":230,"y":320,"wires":[["19454753.10b359"]]},{"id":"6c6a4a72.e3cd94","type":"debug","z":"293b3f46.3c9c5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":300,"wires":[]},{"id":"d1de2347.9d958","type":"function","z":"293b3f46.3c9c5","name":"Gem loop time - max loops","func":"if(msg.topic == 'LoopT'){\n flow.set('LoopTime',msg.payload);\n}\n\nif(msg.topic == 'LoopM'){\n flow.set('LoopMax',msg.payload);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1000,"y":180,"wires":[[]],"icon":"node-red/leveldb.png"},{"id":"a5ad130c.fa753","type":"ui_numeric","z":"293b3f46.3c9c5","name":"Set loop time [sec]","label":"Loop Time [sec]","tooltip":"","group":"1be1e979.ed2837","order":12,"width":3,"height":1,"wrap":false,"passthru":true,"topic":"LoopT","format":"{{value}}","min":"1","max":"60","step":"1","x":710,"y":140,"wires":[["d1de2347.9d958"]]},{"id":"59a699db.333cd8","type":"ui_numeric","z":"293b3f46.3c9c5","name":"Set max loops","label":"Set max loops","tooltip":"","group":"1be1e979.ed2837","order":10,"width":3,"height":1,"wrap":false,"passthru":true,"topic":"LoopM","format":"{{value}}","min":"1","max":"100","step":"1","x":700,"y":200,"wires":[["d1de2347.9d958"]]},{"id":"bfdc4dc0.6511a","type":"function","z":"293b3f46.3c9c5","name":"Get & Set Loop time and max loops","func":"var LT = flow.get('LoopTime');\nvar LM = flow.get('LoopMax');\n\nmsg.loop = {maxloops:LM, duration:LT}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":460,"wires":[["da1ab069.2bf76","a8bec674.d4a158"]],"icon":"node-red/leveldb.png"},{"id":"f5a57ae4.b87898","type":"complete","z":"293b3f46.3c9c5","name":"","scope":["d1de2347.9d958"],"uncaught":false,"x":210,"y":460,"wires":[["bfdc4dc0.6511a"]]},{"id":"da1ab069.2bf76","type":"looptimer-advanced","z":"293b3f46.3c9c5","duration":"5","units":"Second","maxloops":"100","maxtimeout":"1","maxtimeoutunits":"Hour","name":"","x":760,"y":300,"wires":[["6c6a4a72.e3cd94"],["9b2ec1b.c1ed14","73c7c28c.bc7c0c"]]},{"id":"54bbc98f.6aaa18","type":"mqtt in","z":"293b3f46.3c9c5","name":"Temperatur","topic":"sensor/temp","qos":"2","datatype":"auto","broker":"645f638c.ee976c","x":210,"y":380,"wires":[["19454753.10b359"]]},{"id":"9b2ec1b.c1ed14","type":"debug","z":"293b3f46.3c9c5","name":"End data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1000,"y":340,"wires":[]},{"id":"19454753.10b359","type":"gate","z":"293b3f46.3c9c5","name":"Data gate","controlTopic":"control","defaultState":"closed","openCmd":"start","closeCmd":"stop","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":540,"y":300,"wires":[["da1ab069.2bf76"]]},{"id":"73c7c28c.bc7c0c","type":"function","z":"293b3f46.3c9c5","name":"Decode stop","func":"if(msg.payload == 'max loops reached'){msg.payload = 'stop'}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":380,"wires":[["19454753.10b359"]]},{"id":"a8bec674.d4a158","type":"debug","z":"293b3f46.3c9c5","name":"Settings","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":460,"wires":[]},{"id":"1be1e979.ed2837","type":"ui_group","name":"Graph","tab":"7ba0746f.2da01c","order":2,"disp":true,"width":7,"collapse":false},{"id":"645f638c.ee976c","type":"mqtt-broker","name":"ClassicPi","broker":"classicpi","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"7ba0746f.2da01c","type":"ui_tab","name":"Graph","icon":"dashboard","disabled":false,"hidden":false}]

classic-audio commented 3 years ago

Your excample works. But if I try to use the function node - > problem! This is with the function node (what am I doing wrong?)

[{"id":"d8c9e1f5.911c4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ba1ab29a.50fcd","type":"inject","z":"d8c9e1f5.911c4","name":"Stop With This Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"stop","payloadType":"str","x":470,"y":440,"wires":[["4c6cf5c2.6d098c"]]},{"id":"23b91d6e.5c9d52","type":"inject","z":"d8c9e1f5.911c4","name":"Override Maxloops 5 times","props":[{"p":"payload"},{"p":"loop.maxloops","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Go","payloadType":"str","x":470,"y":280,"wires":[["4c6cf5c2.6d098c"]]},{"id":"7689ebde.dd87f4","type":"debug","z":"d8c9e1f5.911c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":380,"wires":[]},{"id":"aed40de3.b0de","type":"inject","z":"d8c9e1f5.911c4","name":"No Override","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"You should see this message 10 times every 1 second + once original.","payloadType":"str","x":490,"y":360,"wires":[["4c6cf5c2.6d098c"]]},{"id":"4c6cf5c2.6d098c","type":"looptimer-advanced","z":"d8c9e1f5.911c4","duration":"1","units":"Second","maxloops":"10","maxtimeout":"20","maxtimeoutunits":"Second","name":"","x":710,"y":380,"wires":[["7689ebde.dd87f4"],[]]},{"id":"25c40bff.6168f4","type":"inject","z":"d8c9e1f5.911c4","name":"Override Duration 2 sec","props":[{"p":"payload"},{"p":"loop.duration","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"You should see this message every 2 sec 10 times + Once the original!!","payloadType":"str","x":460,"y":320,"wires":[["4c6cf5c2.6d098c"]]},{"id":"69058af5.72e344","type":"function","z":"d8c9e1f5.911c4","name":"","func":"\nmsg.loop.maxloops = '5';\nmsg.payload = \"Go\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":220,"wires":[["4c6cf5c2.6d098c"]]},{"id":"3d6e982c.139848","type":"inject","z":"d8c9e1f5.911c4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":220,"wires":[["69058af5.72e344"]]}]

Haxiboy commented 3 years ago

Hello! The actual problem is with the Function node itself, i think i should create an issue/pull request to solve this. The problem is that msg.loop.maxloops=5; is defined in the function node but msg.loop remains undefined. One of your code contains a function node that solves the problem by the way (where you set the values of the properties by flow variables)

The solution is the following: msg.loop={maxloops:""}; msg.loop.maxloops=5; return msg;

Or a better one that is yours. var LT = flow.get('LoopTime'); var LM = flow.get('LoopMax'); msg.loop = {maxloops:LM, duration:LT} return msg;

classic-audio commented 3 years ago

Hello! One point, it looks like My flow of temperature data which is streaming continiusly - re-starts the loop? What I would be looking for: Setting the loop to e.g. 50 I will be seing 50 payloads from the data stream and then stop.

Haxiboy commented 3 years ago

Yes, new message in the flow resets the loop, if you wish to update values you could use function nodes, or switch nodes with global variables that are updated, and use it in your flow that is triggered when you need that 50 payload. Or you can use a node that halts any new message until the loop is not stopped.

For further assistance please go to Node-Red's slack or discord or any community forum. I'll close the issue now, i'll update the documentation of the node to be clear about new messages while a loop is running.