Comparing timestamp with payload to work out if older than 5 minutes

I have a time in a payload, and I want to set a flow variable depending on if the timestamp is older than 5 minutes or current. I could use 2 different timestamps to compare with the current time
obsTimeUtc: "2023-04-23T13:44:29Z"
obsTimeLocal: "2023-04-23 14:44:29"

they are both in the same message, so I could use whichever is easier to use. I have tried using a time-comp node from the palette node-red-contrib-sun-position but cant get it to work.

As long as I can work out how to determine if the timestamp in either obsTimeUtc or obsTimeLocal is older than 5 minutes, thats what I need

You can use $monent().diff() in a change node.

[{"id":"1e367b4a4fcb63fb","type":"inject","z":"bf622e623503117a","name":"config","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"2","topic":"","payload":"{\"obsTimeUtc\":\"2023-04-23T13:44:29Z\",\"obsTimeLocal\":\"2023-04-23 14:44:29\"}","payloadType":"json","x":118,"y":920.0000610351562,"wires":[["8885e1d87c774eb3"]]},{"id":"8885e1d87c774eb3","type":"change","z":"bf622e623503117a","name":"get with variable","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.payload.obsTimeLocal).diff($moment(), \"minutes\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":920,"wires":[["e28f0de5eb5a8d25"]]},{"id":"e28f0de5eb5a8d25","type":"debug","z":"bf622e623503117a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":641,"y":939,"wires":[]}]

You can also use JSONata in switch node property input.

I have got it to work, but there might be a easier way if I could use just switch node.
Using your flow, I get the minutes as a negative number. So I am using a function node with the ABS function to convert that into a positive number. Then I am using a switch node, one output is >5, the other set to otherwise.

[{"id":"12c35aa054a83055","type":"change","z":"e32b69aa25b6467b","name":"time diff","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.payload.observations[0].obsTimeLocal).diff($moment(), \"minutes\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":420,"wires":[["52daccb7fbc2d5c2"]]},{"id":"52daccb7fbc2d5c2","type":"function","z":"e32b69aa25b6467b","name":"ABS","func":"let value = msg.payload\nmsg.payload = Math.abs(value);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1530,"y":420,"wires":[["4ac46f514b29fea9"]]},{"id":"4ac46f514b29fea9","type":"switch","z":"e32b69aa25b6467b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1650,"y":420,"wires":[["f315b03298efee59"],["2df43aa4481ce51a"]]}]

Why? JSonata has $abs(), so $abs($moment().dff($moment(), "minutes")) Or reverse the moments to get positive number
$moment().diff($moment($$.payload.obsTimeLocal), "minutes")

$moment($$.payload.obsTimeLocal).diff($moment(), "minutes") < -5 will return true or false in a switch node property, then the rule would be is True or is False

Or using OG expression you could set the rule as < -5.

1 Like

As I thought. I was overcomplicating it. I only have 1 switch node now, and no need for the abs either. I tried reversing the moments before, I did not have the $ moment before the ($$payload so didnt work. Now it works either way

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