bartbutenaers / node-red-contrib-interval-length

A Node Red node to measure the interval length between successive messages
Apache License 2.0
5 stars 2 forks source link

A question not Issues #14

Open WojtekWro opened 1 year ago

WojtekWro commented 1 year ago

Hi @bartbutenaers I don't know if you work on this project, but maybe you could advice me. I want to measure time between two messages but in that specific way. A msg with payload 1 always starts measurement, and msg with payload 0 always stops this measurement. I don't need to know what is next until, message with payload 1 arrives. Then another measurement begins.

Is this possible to do with your node?

bartbutenaers commented 1 year ago

Hello @WojtekWro,

No that is currently not possible with this node. But you can use a very short code snippet in a function node to do something like that:

image

[{"id":"5ba18690e6e3b9a5","type":"function","z":"5623aa0bb38bbf0e","name":"Measure time between 1 and 0","func":"debugger\n\nif (msg.payload === 1) {\n    // Start timing by storing the current timestamp on context memory\n    context.set(\"start_time\", Date.now());\n\n    node.status({fill:\"blue\",shape:\"dot\",text:\"timer started\"});\n} \nelse if (msg.payload === 0) {\n    // Stop timing \n    var start_time = context.get(\"start_time\");\n\n    // Clear the start_time in context memory\n    context.set(\"start_time\", undefined);\n\n    if (start_time !== undefined) {\n        var elapsed_time = Date.now() - start_time;\n\n        msg.payload = elapsed_time;\n        node.send(msg);\n    }\n    \n    node.status({fill:\"yellow\",shape:\"ring\",text:\"timer stopped\"});\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"_mcu":{"mcu":false},"x":750,"y":2200,"wires":[["4b4d6408261b1f5d"]]},{"id":"4b4d6408261b1f5d","type":"debug","z":"5623aa0bb38bbf0e","name":"Elapsed time (msec)","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","_mcu":{"mcu":false},"x":1040,"y":2200,"wires":[]},{"id":"538c3c3348ead577","type":"inject","z":"5623aa0bb38bbf0e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","_mcu":{"mcu":false},"x":510,"y":2200,"wires":[["5ba18690e6e3b9a5"]]},{"id":"4758650bf59a4ec6","type":"inject","z":"5623aa0bb38bbf0e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","_mcu":{"mcu":false},"x":510,"y":2240,"wires":[["5ba18690e6e3b9a5"]]},{"id":"6dbf76ae53a4fc63","type":"inject","z":"5623aa0bb38bbf0e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","_mcu":{"mcu":false},"x":510,"y":2280,"wires":[["5ba18690e6e3b9a5"]]}]

Bart

WojtekWro commented 1 year ago

Hello @bartbutenaers Thank you for your support and advice. I am going to simplify my flows with your example :) Thank you again.