Calculate timeinterval between two signals

Hello everyone

I have following problem, I want to calculate an interval between two different signals.
For example I want save timestamp from a first signal and compare it with timestamp from second signal.
But i don't know how do it. The biggest problem is for me where can I save first timestamp?
Has anyone an Idea?
I'm total beginner.

Are both signals coming from the same place?
How do you know which is first and which is second?

You can use context storage Working with context : Node-RED
Or you can use the batch node and join node.
e.g.

[{"id":"33b84309.734ebc","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":140,"y":340,"wires":[["40b22621.120db"]]},{"id":"40b22621.120db","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.value","tot":"msg"},{"t":"set","p":"payload.time","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":340,"wires":[["31cc349.be7b9cc"]]},{"id":"31cc349.be7b9cc","type":"batch","z":"bf9e1e33.030598","name":"","mode":"count","count":"2","overlap":"1","interval":10,"allowEmptySequence":false,"topics":[],"x":500,"y":340,"wires":[["69a3e8d6.41db"]]},{"id":"69a3e8d6.41db","type":"join","z":"bf9e1e33.030598","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":340,"wires":[["ad79ab58.e0d1c8"]]},{"id":"ad79ab58.e0d1c8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"time","pt":"msg","to":"$$.payload[1].time -$$.payload[0].time","tot":"jsonata"},{"t":"move","p":"payload[1].value","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":400,"wires":[["585bb59b.81d18c"]]},{"id":"585bb59b.81d18c","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":400,"wires":[]}]

@zenofmud
Yes, they are from the same Sensor.
Signals arrive with one hour interval.
I want know if second signal come latter or he comes not at all.

How can i join payloads if they come in a diffierent time interval?
If a Signal go lost i want to know how many signals goes lost.
I would read over context Node.

Considering only this requirement:

If you just need a timeout - no message was received for more than an hour - you can use a trigger node

And if the messages are different - message 1 must be followed within an hour by message 2

[{"id":"c1e2e1ea9de26a62","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"ff81b6d8b26429b2","type":"group","z":"c1e2e1ea9de26a62","name":"Two different signals - detect if second is late","style":{"label":true},"nodes":["5f9b62032528b82b","c44727629b8960f7","1f27eb655b3cbb9a","1a78f9ed409a9c7b","75a13d7fb077e563"],"x":94,"y":119,"w":792,"h":142},{"id":"46dd4f681b4cae52","type":"group","z":"c1e2e1ea9de26a62","name":"Simple timeout","style":{"label":true},"nodes":["273a00a7cadd67ac","43e9ba9cb768624e","36e487df5c501d3b"],"x":94,"y":19,"w":792,"h":82},{"id":"5f9b62032528b82b","type":"inject","z":"c1e2e1ea9de26a62","g":"ff81b6d8b26429b2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"signal 1","payloadType":"str","x":190,"y":160,"wires":[["1f27eb655b3cbb9a"]]},{"id":"c44727629b8960f7","type":"inject","z":"c1e2e1ea9de26a62","g":"ff81b6d8b26429b2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"signal 2","payloadType":"str","x":190,"y":220,"wires":[["1a78f9ed409a9c7b"]]},{"id":"1f27eb655b3cbb9a","type":"trigger","z":"c1e2e1ea9de26a62","g":"ff81b6d8b26429b2","name":"","op1":"","op2":"message two not received","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":560,"y":160,"wires":[["75a13d7fb077e563"]]},{"id":"1a78f9ed409a9c7b","type":"change","z":"c1e2e1ea9de26a62","g":"ff81b6d8b26429b2","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":220,"wires":[["1f27eb655b3cbb9a"]]},{"id":"75a13d7fb077e563","type":"debug","z":"c1e2e1ea9de26a62","g":"ff81b6d8b26429b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":160,"wires":[]},{"id":"273a00a7cadd67ac","type":"trigger","z":"c1e2e1ea9de26a62","g":"46dd4f681b4cae52","name":"","op1":"","op2":"No message arrived","op1type":"nul","op2type":"str","duration":"5","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":560,"y":60,"wires":[["36e487df5c501d3b"]]},{"id":"43e9ba9cb768624e","type":"inject","z":"c1e2e1ea9de26a62","g":"46dd4f681b4cae52","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"signal 1","payloadType":"str","x":190,"y":60,"wires":[["273a00a7cadd67ac"]]},{"id":"36e487df5c501d3b","type":"debug","z":"c1e2e1ea9de26a62","g":"46dd4f681b4cae52","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":60,"wires":[]}]

@jbudd thanks for response.

the messages are different but the are from the same sensor. They would have different timestamp and different sequence number. but i don't know if message goes lost would sequence number go lost or sequence number is added from gateway.
I trie your second approach.

hmm signal one and two have the same entrence

This all depends on the message properties, can you show us the complete two messages?
Also will these messages overlap, i.e can two or more initial signals happen before the second signal messages?

@E1cid
This is a message structure i change rawPayload and id. for data privacy.
it onli first massage second would be the same. The massage is from friday.

cisco/00147A777710FE73/data : msg.payload : string[513]
"{"payloadType":"TELEMETRY","version":1,"timestamp":1649416376555,"entityType":"SENSOR","name":"temp_Sensor","id":"86b4456a-927e-4231-94a2-a9110b6da3e1","model":"AV200","serialNumber":"00147A107700FE73","telemetry":{"temperature":{"value":8.86,"unit":"centigrade"},"humidity":{"value":59.51,"unit":"percentage"},"battery":{"value":3,"unit":"volt"}},"loraParameters":{"fPort":6,"rawPayload":"00147A777710FE73000000","rssi":{"value":-95,"unit":"dBm"},"snr":{"value":12,"unit":"dB"},"fCnt":100},"asset":{}}"

Is the second message exactly the same including rawPayload and id?

I ask again do/can the messages overlap?

rawPayload is not same

It seems that you have two possible error conditions:

1 This message is the same type (identified by msg.payload.loraParameters.rawPayload) as the last message.
2 This message arrived more than 1 hour since the last message.

So you could use two context variables context.lastmessagetype and context.lastmessagetime

Something like this in a function node. There is probably a way to do it without JS code.
(pseudocode, NOT real javascript!)

if ( msg.payload.loraParameters.rawPayload  == context.get("lastmessagetype")) {
  // ERROR 1
  do something;
}
if (context.get(lastmessagetime) < now - 1 hour) {
// ERROR 2
  do something
}
context.set("lastmessagetype", msg.payload.loraParameters.rawPayload);
context.set("lastmessagetime", now);

And you will have to handle if the context variables don't exist - eg after deployment.

This flow will output a count of messages that has not received 2 signals, or the time difference between two signal.

The time can be set in the trigger, at moment set to 5 seconds for testing

[{"id":"33b84309.734ebc","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payloadType\":\"TELEMETRY\",\"version\":1,\"timestamp\":1649416376555,\"entityType\":\"SENSOR\",\"name\":\"temp_Sensor\",\"id\":\"86b4456a-927e-4231-94a2-a9110b6da3e1\",\"model\":\"AV200\",\"serialNumber\":\"00147A107700FE73\",\"telemetry\":{\"temperature\":{\"value\":8.86,\"unit\":\"centigrade\"},\"humidity\":{\"value\":59.51,\"unit\":\"percentage\"},\"battery\":{\"value\":3,\"unit\":\"volt\"}},\"loraParameters\":{\"fPort\":6,\"rawPayload\":\"00147A777710FE73000000\",\"rssi\":{\"value\":-95,\"unit\":\"dBm\"},\"snr\":{\"value\":12,\"unit\":\"dB\"},\"fCnt\":100},\"asset\":{}}","payloadType":"json","x":110,"y":280,"wires":[["40b22621.120db"]]},{"id":"40b22621.120db","type":"change","z":"bf9e1e33.030598","name":"simulate incoming msg","rules":[],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":280,"wires":[["cf041f61.2f768"]]},{"id":"cf041f61.2f768","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":340,"wires":[["11b19158.6b52ef","e63fc3d0.c1c64"]]},{"id":"11b19158.6b52ef","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"sensor_count[msg.topic]","pt":"flow","to":"$append(\t    $exists($flowContext('sensor_count[\"' & $$.topic & '\"]')) ?\t        $flowContext('sensor_count[\"' & $$.topic & '\"]') :\t        [],\t    \t    $millis()\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":340,"wires":[["7f9c978c.052cd"]]},{"id":"e63fc3d0.c1c64","type":"trigger","z":"bf9e1e33.030598","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"topic","topic":"topic","outputs":1,"x":330,"y":400,"wires":[["3d6c4e60.401112"]]},{"id":"7f9c978c.052cd","type":"switch","z":"bf9e1e33.030598","name":"","property":"$count($flowContext('sensor_count[\"' & $$.topic & '\"]'))","propertyType":"jsonata","rules":[{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":340,"wires":[["93d00904.0c735"]]},{"id":"3d6c4e60.401112","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"sensor_count[msg.topic]","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":400,"wires":[["47d9fc1.6fc7204"]]},{"id":"93d00904.0c735","type":"change","z":"bf9e1e33.030598","name":"set reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":340,"wires":[["e63fc3d0.c1c64","9a915b56.f2c9c8"]]},{"id":"47d9fc1.6fc7204","type":"switch","z":"bf9e1e33.030598","name":"","property":"$count(msg.payload) ","propertyType":"jsonata","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":700,"y":400,"wires":[["9d5aff10.3606a"]]},{"id":"9a915b56.f2c9c8","type":"change","z":"bf9e1e33.030598","name":"2 messgaes received","rules":[{"t":"set","p":"payload","pt":"msg","to":"sensor_count[msg.topic]","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"($$.payload[1] - $$.payload[0]) / 1000","tot":"jsonata"},{"t":"delete","p":"sensor_count[msg.topic]","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":340,"wires":[["b1c6df8.dd1112"]]},{"id":"9d5aff10.3606a","type":"change","z":"bf9e1e33.030598","name":"1 message received","rules":[{"t":"set","p":"sensor_count.signal_missed","pt":"flow","to":"($exists($flowContext(\"sensor_count.signal_missed\")) ?\t    $flowContext(\"sensor_count.signal_missed\") :\t    0) + 1","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"sensor_count.signal_missed","tot":"flow"},{"t":"delete","p":"sensor_count[msg.topic]","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":400,"wires":[["27d744d5.78b004"]]},{"id":"b1c6df8.dd1112","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1550,"y":340,"wires":[]},{"id":"27d744d5.78b004","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":400,"wires":[]},{"id":"46b445c4.48d02c","type":"inject","z":"bf9e1e33.030598","name":"reset count","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":480,"wires":[["9954ba69.3aae68"]]},{"id":"9954ba69.3aae68","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"sensor_count.signal_missed","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":480,"wires":[[]]}]

The flow also clears up the context store after trigger time.
[edit] changed so output for two messages is immediate.

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