Time Calculation

Hi All,

I've got a use case I want to try and solve. And that's calculating the time between for a value to change from x to y.

Starting Temperature: 50
Time: 10:00:00
Next value: 51
Time: 10:05:00

So the output will be 5 as that's the time that passed for the value to change from 50 to 51.

Here is a function to calculate the time difference between successive message timestamps.
If the message sent at the trigger temperature does not contain a timestamp, you would have to generate one in the function.
Untitled 1

[{"id":"9648da80c2c77166","type":"inject","z":"ac3483be8df714b3","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":120,"wires":[["28c953b77c5bcf93"]]},{"id":"28c953b77c5bcf93","type":"function","z":"ac3483be8df714b3","name":"function 1","func":"function timeDifference(ts1, ts2) {\n    var millisec = ts1 - ts2;\n    const days = Math.floor(millisec / 1000 / 60 / 60 / 24);\n    millisec -= days * 1000 * 60 * 60 * 24\n    const hrs = Math.floor(millisec / 1000 / 60 / 60);\n    millisec -= hrs * 1000 * 60 * 60\n    const mins = Math.floor(millisec / 1000 / 60);\n    millisec -= mins * 1000 * 60\n    const secs = Math.floor(millisec / 1000);\n    millisec -= secs * 1000\n    return { \"days\": days, \"hrs\": hrs, \"mins\": mins, \"secs\": secs, \"ms\": millisec }\n}\n\nconst previous = flow.get(\"lasttimestamp\") ?? 0\n\nif (previous == 0) { \n    flow.set(\"lasttimestamp\", msg.payload) \n    msg.payload = \"Waiting for second timestamp\"\n}\nelse {\n    flow.set(\"lasttimestamp\", msg.payload)\n    msg.payload = timeDifference(msg.payload, previous)\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":120,"wires":[["a0d70c27a8570170"]]},{"id":"a0d70c27a8570170","type":"debug","z":"ac3483be8df714b3","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":120,"wires":[]}]
{"days":0,"hrs":0,"mins":2,"secs":55,"ms":575}

An alternative to jbudd's offering. Only goes down to seconds (uses same format as Tasmota)

[{"id":"d6896368821fb2e4","type":"function","z":"34ad08af41ba9a25","name":"function 5","func":"        /**\n        *  Description  Converts milliseconds to days T hours:minutes:seconds string in 'locale' format. (Format used by Tasmota)\n        *               Create a Date object with the number of milliseconds, rounding the number to the nearest second\n        *               Get the number of days from the number of milliseconds\n        *               Convert Date to time string using UTC as the time zone to avoid daylight saving errors (or time zone offset)\n        *\n        * @param    {number}    milliSeconds    - Number of milliseconds to convert. Usually as a response to Date.now() or Date.getTime()\n        *\n        * @returns  {string}                    - Timespan string. In UK \"ddThh:mm:ss\"\n        *\n        */\n\n        function msToTimespan(milliSeconds) {\n            if (milliSeconds !== 0) {\n                const date = new Date(milliSeconds)\n                const days = Math.floor(milliSeconds / (3600000 * 24))\n\n                return `${days}T${date.toLocaleTimeString('en-GB', { timeZone: 'UTC' })}`\n\n            }\n\n            return '0T00:00:00'\n\n        } // End Method msToTimespan()\n\nconst lastTime = context.get('lastTime') ?? 0\nconst timeStamp = msg.payload\n\nif (lastTime > 0) {\n    msg.payload = msToTimespan(timeStamp - lastTime)\n\n} else {\n    msg.payload = 0\n\n}\n\ncontext.set('lastTime', timeStamp)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1760,"y":1600,"wires":[["339242f4565ed930"]]},{"id":"339242f4565ed930","type":"debug","z":"34ad08af41ba9a25","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2020,"y":1600,"wires":[]},{"id":"e9c08a738775d06e","type":"inject","z":"34ad08af41ba9a25","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1480,"y":1600,"wires":[["d6896368821fb2e4"]]}]

0T00:01:51

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