Use today as filter

Hi.

I got several payloads that looks like attached picture with different start dates. Can I somehow just let the messengers pass with todays start date? My idea for now is to start with adjust the format on the date and maybe use a swith node with property payload.start_date and an expression to filter out today.

I am stuck and tried google a lot to solve this without success :slight_smile:

[{"id":"bacc35bea7d25bee","type":"inject","z":"efbc49d0a7529b05","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":100,"wires":[["4bc1d3788c2f0e97"]]},{"id":"a0b3732a8e570fe9","type":"debug","z":"efbc49d0a7529b05","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":100,"wires":[]},{"id":"4bc1d3788c2f0e97","type":"template","z":"efbc49d0a7529b05","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\"id\":[\"ID:397_207781203_14756\"],\"name\":[\"test\"],\"start_date\":[\"2023-03-24 19:00:00\"],\"end_date\":[\"2023-03-24 22:30:00\"]}","output":"json","x":540,"y":100,"wires":[["a0b3732a8e570fe9"]]}]

For sure you can do this with some jsonata $moment expression in Switch node ..
i took the javascript road if you are interested here is an example flow

[{"id":"bacc35bea7d25bee","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":2040,"wires":[["4bc1d3788c2f0e97"]]},{"id":"a0b3732a8e570fe9","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":2080,"wires":[]},{"id":"4bc1d3788c2f0e97","type":"template","z":"54efb553244c241f","name":"2023-03-24 19:00:00","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n    \"id\": [\n        \"ID:397_207781203_14756\"\n    ],\n    \"name\": [\n        \"test\"\n    ],\n    \"start_date\": [\n        \"2023-03-24 19:00:00\"\n    ],\n    \"end_date\": [\n        \"2023-03-24 22:30:00\"\n    ]\n}","output":"json","x":420,"y":2040,"wires":[["49d6dd8b4b7c3cd0"]]},{"id":"49d6dd8b4b7c3cd0","type":"function","z":"54efb553244c241f","name":"filter today","func":"let startdate = msg.payload.start_date[0].split(\" \")[0]\n// node.warn(startdate);\nlet today = new Date().toISOString().split(\"T\")[0]\n// node.warn(today);\n\nif (startdate === today) {\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":2080,"wires":[["a0b3732a8e570fe9"]]},{"id":"1d47e11a1a9696ba","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":2120,"wires":[["76df50776619040b"]]},{"id":"76df50776619040b","type":"template","z":"54efb553244c241f","name":"2023-03-22 11:00:00","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n    \"id\": [\n        \"ID:397_207781203_14756\"\n    ],\n    \"name\": [\n        \"test\"\n    ],\n    \"start_date\": [\n        \"2023-03-22 11:00:00\"\n    ],\n    \"end_date\": [\n        \"2023-03-24 22:30:00\"\n    ]\n}","output":"json","x":420,"y":2120,"wires":[["49d6dd8b4b7c3cd0"]]}]

Using a switch node and JSONata expressions

[{"id":"bacc35bea7d25bee","type":"inject","z":"8be23377afc3327e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":[\"ID:397_207781203_14756\"],\"name\":[\"test\"],\"start_date\":[\"2023-03-24 19:00:00\"],\"end_date\":[\"2023-03-24 22:30:00\"]}","payloadType":"json","x":390,"y":320,"wires":[["c61d25aedb3fac25"]]},{"id":"c61d25aedb3fac25","type":"switch","z":"8be23377afc3327e","name":"","property":"$split($$.payload.start_date[0], \" \")[0]","propertyType":"jsonata","rules":[{"t":"eq","v":"$moment().format(\"YYYY-MM-DD\")","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":570,"y":320,"wires":[["a0b3732a8e570fe9"]]},{"id":"d2e9e594e61d83f9","type":"inject","z":"8be23377afc3327e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":[\"ID:397_207781203_14756\"],\"name\":[\"test\"],\"start_date\":[\"2023-03-22 19:00:00\"],\"end_date\":[\"2023-03-22 22:30:00\"]}","payloadType":"json","x":390,"y":360,"wires":[["c61d25aedb3fac25"]]},{"id":"a0b3732a8e570fe9","type":"debug","z":"8be23377afc3327e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":320,"wires":[]}]

1 Like

Thank you! You guys are really awsome :+1:t2:
Exactly what i was looking for.

But i can’t get this to work. Got nothing in debug when importing your examples. Am i running a to old version of node red. V2.2

Best regards

Not wery good in JSONata expressions. But after the split function it is missplaced in the array so it cant´t find the payload in payload.start_date anymore. Can this be the problem?

clip

Thanks again!! Got it to work :grinning:

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