Check if specific time is "now"?

Hi,

I would like to compare the actual time with a timestamp I get from a database.
The timestamp is in the format "00:10" and it is stored in a global variable "ExecutionTime" as a string.

How do I compare this value in a function node with the actual system time ?

IF (global.get("ExecutionTime") == ???) ... do cool stuff ...

All I tried so far failed :roll_eyes:

BR
Gawan

What are you trying to achieve?

Are you polling every second / every minute until it matches a time set in a a database - then when it matches, you do something?

Is this "time" supposed to be like a daily schedule or a one time thing?

the plan is to compare the actual time with the "ExecutionTime" every 30sec and if it fits and an additional specific parameter is set , I will trigger a MQTT Message

The "ExecutionTime" is a timestamp that will be calculated in a SQL database outside NodeRed once a day to optimize the best trigger time for this specific event

So essentially you will perform 2880 polls per day to look for a "time"

Since Node-RED is event based, a better solution is, when reading the database, generate a cron-plus schedule that will execute exactly at the right time.

Demo

look maa, no poling what-so-ever
chrome_BFf9YpkqeD

[{"id":"60d8d1917dfe26e4","type":"cronplus","z":"c2233fc1d8cc5c3b","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","defaultLocation":"","defaultLocationType":"default","outputs":1,"options":[],"x":1340,"y":700,"wires":[["2c6cb603de9c263c"]]},{"id":"9c72718894e9fbc4","type":"inject","z":"c2233fc1d8cc5c3b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1235,"y":580,"wires":[["e854ed1d3954a62d"]],"l":false},{"id":"e854ed1d3954a62d","type":"function","z":"c2233fc1d8cc5c3b","name":"fake database call (12:25)","func":"msg.payload = [\n    { executionTime: '12:25' }\n]\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1390,"y":580,"wires":[["4e65d37261007173"]]},{"id":"fcdcccf1f8dbcffd","type":"inject","z":"c2233fc1d8cc5c3b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1235,"y":620,"wires":[["12948bb6ae62e0e7"]],"l":false},{"id":"12948bb6ae62e0e7","type":"function","z":"c2233fc1d8cc5c3b","name":"fake database call (17:12)","func":"msg.payload = [\n    { executionTime: '17:12' }\n]\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1390,"y":620,"wires":[["4e65d37261007173"]]},{"id":"4e65d37261007173","type":"function","z":"c2233fc1d8cc5c3b","name":"generate schedule","func":"\nconst time = msg.payload[0]?.executionTime\n\nif (time) {\n    const timeParts = time.split(':')\n    if (timeParts.length < 2) {\n        node.warn('invalid time value');\n        return\n    }\n    const hour = timeParts[0]\n    const minute = timeParts[1]\n    const seconds = (timeParts.length > 2) ? timeParts[2] : 0\n\n    msg.payload = [\n        {\"command\": \"remove-all\" },\n        {\n            \"command\": \"add\",\n            \"name\": `at ${time}`,\n            \"expression\": `${seconds} ${minute} ${hour} * * * *`,\n            \"expressionType\": \"cron\",\n            \"payloadType\": \"default\",\n            \"limit\": 3 \n        }\n    ]\n    return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1650,"y":600,"wires":[["60d8d1917dfe26e4"]]},{"id":"2c6cb603de9c263c","type":"debug","z":"c2233fc1d8cc5c3b","name":"--> do cool stuff at specified time","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1590,"y":700,"wires":[]}]

that looks really nice - thats exactly the solution I was looking for, but due to insufficient knowledge I tried to realize my version :laughing:

but there seem to be a mistake in your code ?
image

No, it is valid code as of Node v14.

you are either using an old Node-RED (pre v2) - OR - have Monaco editor turned off (the old - soon to be removed ACE editor does not understand optional chaining

and/or NodeJS V < 14


What version of Node-RED are you using? what version of NodeJS is it running on?

I am using 2.2.2
... bit afraid to upgrade as there was an issue with Node JS Version and SQL Database Node compatibility last time ...

2.2.2 does support monaco editor but you need to enable it is settings.js

What version of NodeJS (you didnt say)?

you can change that line of code to NOT use optional chaining.

This is the alternative code line

const time = msg.payload[0] && msg.payload[0].executionTime

it was this thread and as a result I postponed the update of my local node-red to ... somewhen in the fare future

--> Node-red-contrib-mssql-plus fails to install because of version mismatch?

That is resolved now. Installing mssql-plus will work with node20

But, you could have simply installed node v18 anyhow.

after upgrading to the latest versions (without any problems or issues) your code works fine and setting dynamic trigger timestamp works fine as well
thanks a lot for your support ! :+1: :grinning: :grinning:

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