# Check if specific time is "now"?

**URL:** <https://discourse.nodered.org/t/check-if-specific-time-is-now/84391>\
**Category:** General\
**Tags:** http-request, function-node\
**Created:** [8 January 2024 13:04 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391 "2024-01-08T13:04:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [8 January 2024 13:04 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/1 "2024-01-08T13:04:24Z")

</div>

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 🙄

BR  
Gawan

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:06 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/2 "2024-01-08T13:06:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [8 January 2024 13:07 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/3 "2024-01-08T13:07:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:23 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/4 "2024-01-08T13:23:01Z")

</div>

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](https://flows.nodered.org/node/node-red-contrib-cron-plus) schedule that will execute exactly at the right time.

### Demo

_look maa, no poling what-so-ever_  
 ![chrome_BFf9YpkqeD](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abd73d46009b446de9bf623c03a46bca110f04c2.gif)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [8 January 2024 13:29 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/5 "2024-01-08T13:29:03Z")

</div>

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

but there seem to be a mistake in your code ?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb4aeca03499b82d7652a963dd74301ae0a9f5d5.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:31 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/6 "2024-01-08T13:31:52Z")

</div>

> [@Gawan](#):
>
> but there seem to be a mistake in your code ?  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb4aeca03499b82d7652a963dd74301ae0a9f5d5.png)

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)

and/or NodeJS V \< 14

* * *

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [8 January 2024 13:33 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/7 "2024-01-08T13:33:54Z")

</div>

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 ...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:34 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/8 "2024-01-08T13:34:43Z")

</div>

> [@Gawan](#):
>
> I am using 2.2.2

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

What version of NodeJS (you didnt say)?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:35 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/9 "2024-01-08T13:35:52Z")

</div>

> [@Steve-Mcl](#):
>
> > [@Gawan](#):
> >
> > but there seem to be a mistake in your code ?  
> > ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb4aeca03499b82d7652a963dd74301ae0a9f5d5.png)
> 
> No, it is valid code as of Node v14.

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

This is the alternative code line

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

```

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [8 January 2024 13:42 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/10 "2024-01-08T13:42:36Z")

</div>

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?](https://discourse.nodered.org/t/node-red-contrib-mssql-plus-fails-to-install-because-of-version-mismatch/83040)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 January 2024 13:50 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/11 "2024-01-08T13:50:13Z")

</div>

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

But, you could have simply installed node v18 anyhow.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [18 January 2024 16:17 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/12 "2024-01-18T16:17:35Z")

</div>

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 ! 👍 😀 😀

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 February 2024 16:18 UTC](https://discourse.nodered.org/t/check-if-specific-time-is-now/84391/13 "2024-02-01T16:18:01Z")

</div>

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