Epoch payload to Number

Hello,

I try to measure time between to 2 Messages.

I use epoch for each timestamp and substract it to the difference .
So timestamp A - timestamp B = Steptime.

So far so good.

I send with 1200ms so i get a Steptime around 1200ms.

But after i calculate with Steptime the result gets back to epoch values.

I tried to get Steptime to a number Datatype to let it "Forget" its epoch origin with parseInt() and Number() but Nothing works.

in the latest test i am uing the node simpletime, before that i used Date.now() to get the epoch time.

Is there a better solution to do that or get the time difference to a clear number ?

the part of the Code:

[
    {
        "id": "f0e77489.9c5de8",
        "type": "simpletime",
        "z": "9e82b6f8.933568",
        "name": "",
        "x": 1660,
        "y": 450,
        "wires": [
            [
                "5c359cda.ff5734",
                "1e05b3d8.60ea2c"
            ]
        ]
    },
    {
        "id": "5c359cda.ff5734",
        "type": "function",
        "z": "9e82b6f8.933568",
        "name": "Steptime Berechnung",
        "func": "var Time = msg.myepoch;                // Neu reinkommender Wert\nvar Merker = context.get('Merker')||0;        // Zum Wert merken\nvar Steptime = 0;                          // Ergebnis\n\nSteptime = Number(Time) - Number(Merker);\n\ncontext.set('Merker',Time);\nmsg.payload.Steptime = parseInt(Steptime);\n\n\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1880,
        "y": 450,
        "wires": [
            [
                "efba4bd8.e07f18",
                "9151d0bc.c6eb4"
            ]
        ]
    },
    {
        "id": "a98f220b.11a92",
        "type": "function",
        "z": "9e82b6f8.933568",
        "name": "Nur Bh",
        "func": "var BhZeitSummems;// = context.get('BhZeitSummems');\n\n//var Bhschrieb = global.get(msg.payload.Item+\"Getriebe_Betriebsstunden\")\n//var doppelchecker = context.get('doppelchecker',doppelchecker);\n\n\n\n/*\nif(msg.payload.Aktiv == \"True\"){ \n    var hms = global.get('W16Bhreaded');   \n    //var hms = \"111:22:33\";\n    var a = hms.split(':');\n        if (a[2] >= 1){\n    \n            }else{\n            a[2] = 30;\n            }\n    var ReadInMs = (parseInt((+a[0]) * 60 * 60 ) + parseInt((a[1]) * 60) + parseInt(a[2]));\n}\n*/\nvar ReadInMs;\n//if (isNaN(Number(global.get(msg.payload.Item+'Bhreaded')))){\nif (isNaN(Number(global.get('BhZeitSummems')))){\nReadInMs = 0;\n}else{\n//ReadInMs = global.get(msg.payload.Item+'Bhreaded');  \nReadInMs = global.get('BhZeitSummems');  \n}\n\n//Zeitumrechner von Sekunden zum Zeitformat HHMMSS\nString.prototype.toHHMMSS = function () {\n    var sec_num = parseInt(this, 10); \n    var hours   = Math.floor(sec_num / 3600);\n    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);\n    var seconds = sec_num - (hours * 3600) - (minutes * 60);\n\n    if (hours   < 10) {hours   = \"0\"+hours;}\n    if (minutes < 10) {minutes = \"0\"+minutes;}\n    if (seconds < 10) {seconds = \"0\"+seconds;}\n    return hours+':'+minutes+':'+seconds;\n}\n\n\n\n\nif (msg.payload.ZMessungaktiv == 1)\n        {\n        //BhZeitSummems =  Number(ReadInMs) + Number(msg.payload.Steptime) ; // Warum ist hier der Präfix der Zeitmessungen drin ? Zeitmessknoten nehmen!!!!!????????\n        BhZeitSummems =  (Number(ReadInMs) + Number(msg.payload.Steptime)); // Warum ist hier der Präfix der Zeitmessungen drin ? Zeitmessknoten nehmen!!!!!????????\n        msg.payload.Betriebsstunden    = ((BhZeitSummems/1000).toString().toHHMMSS());   //Zeit umgewandelt\n        }else{\n        BhZeitSummems =  Number(ReadInMs);  \n        }\n\n\n//if(msg.payload.Betriebsstunden == context.get('doppelchecker')){\n//    msg.payload.doppelte =  msg.payload.doppelte + 1;\n//}\n\n\n//context.set('doppelchecker',msg.payload.Betriebsstunden);\nmsg.payload.BhZeitSummems                   = BhZeitSummems;\n\n\nmsg.payload.ReadInMs = ReadInMs;\n\n//msg.payload.Bhschrieb = Bhschrieb;\n\nglobal.set('BhZeitSummems',BhZeitSummems);\n//global.set('BhZeitSummems',0);\n\n\n\nmsg.payload.BhZeitSummemsglobalalt = global.get('ReadInMs');\nmsg.payload.BhZeitSummemsglobalgesetzt = global.get('BhZeitSummems');\n//msg.payload.mymillis = msg.mymillis;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 2550,
        "y": 450,
        "wires": [
            [
                "a66c1b3c.2b8a4"
            ]
        ]
    },
    {
        "id": "1e05b3d8.60ea2c",
        "type": "debug",
        "z": "9e82b6f8.933568",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1820,
        "y": 420,
        "wires": []
    },
    {
        "id": "efba4bd8.e07f18",
        "type": "change",
        "z": "9e82b6f8.933568",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload.Steptime",
                "pt": "msg",
                "to": "payload.Steptime",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2230,
        "y": 450,
        "wires": [
            [
                "a98f220b.11a92",
                "878712d5.17249"
            ]
        ]
    },
    {
        "id": "a66c1b3c.2b8a4",
        "type": "debug",
        "z": "9e82b6f8.933568",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 2720,
        "y": 450,
        "wires": []
    },
    {
        "id": "9151d0bc.c6eb4",
        "type": "debug",
        "z": "9e82b6f8.933568",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 2150,
        "y": 420,
        "wires": []
    },
    {
        "id": "878712d5.17249",
        "type": "debug",
        "z": "9e82b6f8.933568",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 2540,
        "y": 420,
        "wires": []
    }
]

Overview:

Greetings
Chorum

Set msg.payload = {}; at the beginning of your function. That should remove the errors.

Hey,
i dont have any Errors only false values. instead of Steptime + X = 1200 + X i get 1200 + epoch ….

Not sure i am getting you.
This works correctly i think. what else do you want, can you explain a bit clearer.

[{"id":"682f9e8c.230778","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"myepoch","v":"$toMillis($now())","vt":"jsonata"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":100,"y":2680,"wires":[["484cf18b.56be1","8272e1e1.6298d8"]]},{"id":"484cf18b.56be1","type":"function","z":"8d22ae29.7df6d","name":"Steptime Berechnung","func":"var Time = msg.myepoch; \nmsg.payload ={};\n// Neu reinkommender Wert\nvar Merker = context.get('Merker')||0;        // Zum Wert merken\nvar Steptime = 0;                          // Ergebnis\n\nSteptime = Number(Time) - Number(Merker);\n\ncontext.set('Merker',Time);\nmsg.payload.Steptime = parseInt(Steptime);\n\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":2680,"wires":[["be0c5a52.c22a08","d582741f.a26a"]]},{"id":"8272e1e1.6298d8","type":"debug","z":"8d22ae29.7df6d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":280,"y":2650,"wires":[]},{"id":"d582741f.a26a","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":2650,"wires":[]}]

if i add Steptime with an other variable Steptime changes from 1200 to epoch+1200 in some way.
Instead 1200 the sum its like 1604309997....

Which function are you talking about?

BhZeitSummems = (Number(ReadInMs)+ Number(msg.payload.Steptime));

in that case Steptime is bringing Epoch + its value

What is the value of
global.get('BhZeitSummems')
in the global context?

thats the summed up time that i Need. Should be = time (unil now) + Steptime (the time between the last to mesages) , normaly i would write that to a database but for testing i use a global.

i tried today again with follwoing Code:

var Startmessung = 0;
var Altmessung = context.get('Altmessung')||0;
var Steptime



//Zeitmessung------------------------------------------------------------------


    if (msg.payload.ZMessungaktiv == 1)
        {
            Startmessung = new Date().getTime(); //Startzeit gesetzt
            Steptime = (Startmessung - Altmessung);//+global.get('W16Bhreaded');      //Zeitdifferenz messen
            Altmessung = Startmessung;           // Alte Zeit merken

        }

context.set('Altmessung',Altmessung);//Betriebsstundenmerker
msg.payload.Steptime = Steptime;
return msg;

The Value of Steptime is fine until i calculate with that.
Before calculation around 1200 After the calculation the Result Shows that Steptime brought the whole epoch in the calculation….

I found an example with a Little bit different Code and that is working.

So i would go on with that, Maybe stombeling about that Problem later again :frowning:

I asked what the value was in context storage, When you run the code look at the context storage. As you add this value to steptime.

I am still not sure what you time difference you are trying to meassure.
You want the time difference between the two function nodes, or the time since the flow last ran.

If you have a solution go with it. You can always comeback to this later.

thx for the help!

I was about measuring the time between 2 Messages in ms.

You seem to be over complicating it. the below function will output the time in ms since last message.

[{"id":"7055ff71.b552e8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"timestamp","v":"","vt":"date"},{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":160,"y":2720,"wires":[["832b8299.4f544"]]},{"id":"832b8299.4f544","type":"function","z":"8d22ae29.7df6d","name":"","func":"let lastTimestamp = flow.get(\"lastTimestamp\") || msg.timestamp;\nmsg.timegap = msg.timestamp - lastTimestamp;\nflow.set(\"lastTimestamp\", msg.timestamp)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":2720,"wires":[["b8d373a5.049a4"]]},{"id":"b8d373a5.049a4","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"timegap","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":2720,"wires":[]}]

Edit/ and to get the gap to next block do not write back to context.
e.g.

[{"id":"7055ff71.b552e8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"timestamp","v":"","vt":"date"},{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":370,"y":2780,"wires":[["832b8299.4f544"]]},{"id":"832b8299.4f544","type":"function","z":"8d22ae29.7df6d","name":"","func":"let lastTimestamp = flow.get(\"lastTimestamp\") || msg.timestamp;\nmsg.timegap = msg.timestamp - lastTimestamp;\nflow.set(\"lastTimestamp\", msg.timestamp)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":2720,"wires":[["9bb5d95b.170c8","197cd668.4cfa8a"]]},{"id":"9bb5d95b.170c8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"timegap","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":2640,"wires":[]},{"id":"197cd668.4cfa8a","type":"delay","z":"8d22ae29.7df6d","name":"","pauseType":"delay","timeout":"1500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":590,"y":2720,"wires":[["b0316f7f.871cc"]]},{"id":"b0316f7f.871cc","type":"function","z":"8d22ae29.7df6d","name":"","func":"let lastTimestamp = flow.get(\"lastTimestamp\") || msg.timestamp;\nmsg.timegap = new Date() - lastTimestamp;\n//flow.set(\"lastTimestamp\", msg.timestamp)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":2780,"wires":[["b8d373a5.049a4"]]},{"id":"b8d373a5.049a4","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"timegap","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":2720,"wires":[]}]

For the Moment i will stay with my current solution until i have more time to check yours.

Thank You!