# Time range node recommendation

**URL:** <https://discourse.nodered.org/t/time-range-node-recommendation/34651>\
**Category:** General\
**Created:** [21 October 2020 20:40 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651 "2020-10-21T20:40:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [21 October 2020 20:40 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/1 "2020-10-21T20:40:41Z")

</div>

Hi all

I have been playing around with Node-Red for a little while and really enjoying it a lot.

I have a Philip Hue Motion and wanted to take more control over it in NR.

So i wanted a Node that would allow me to say, only do motion action the current time is within a  
time range. for me that is starts after Sunset and End time at 2am.

One i found and i am using is

> **[node-red-contrib-time-range-switch](https://flows.nodered.org/node/node-red-contrib-time-range-switch)**
>
> A simple Node-RED node that routes messages depending on the time. If the current time falls within the range specified in the node configuration, the message is routed to output 1. Otherwise the message is routed to output 2.

this works great however, it range is 00:01 to 23:59 so it wont let me cross over days,  
ie 18:00 (current sunset time) to 02:00 (2am next morning).

as anyone know of different that can do it..

Currently i have to create two copies.. one that says 18:00 to 23:59 and another that is 00:01 to 02:00

this works fine.. but it is messy and i like to keep things more simple 🙂

 ![Screenshot 2020-10-21 214010](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c185fa1934fab86f52a855aa81a31837d531d7d.png)

thanks, Bru.

---

<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:** [21 October 2020 23:03 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/2 "2020-10-21T23:03:03Z")

</div>

If you use the cron plus node, you can send a message through it that will output current solar state (including dusk, dawn, golden hour etc) (see the built in demos/examples and built in documentation for the describe command).

Using these solar properties & a switch node or function node you can decide what to do.

---

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [22 October 2020 14:26 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/3 "2020-10-22T14:26:13Z")

</div>

Thanks.. i had a quick look around it and its not really what i am after..

I can see i can set a SUNSET Start.. but can't see who i can create a range so it ends at a certain time.

Also seem like not sure how this would fit with my existing flow..

Must be a node that can just take current time and say is it within a range.. that can cross into the next day?

I'm sure a function could be written it not really a route i wanted to go down just yet as i i figure this would have been a fairly standard built in request. ☹

oh well.. ta..

---

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [22 October 2020 15:16 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/4 "2020-10-22T15:16:37Z")

</div>

having a look at the flow library and

> **[thingzi-logic-timers](https://flows.nodered.org/node/thingzi-logic-timers)**
>
> Easy to use time based nodes with clean design

seems to be nice and simple and looks like it does do a Start to End Range. so going to try testing it out.

the CronPlus i think it good so im sure i might need it from when i need something trigging or testing every x min/day/months type to checks.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [22 October 2020 16:09 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/5 "2020-10-22T16:09:28Z")

</div>

Also worth a try are these nodes:

> **[node-red-contrib-chronos](https://flows.nodered.org/node/node-red-contrib-chronos)**
>
> Time-based Node-RED scheduling, queueing, routing and filtering nodes

---

<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:** [22 October 2020 17:37 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/6 "2020-10-22T17:37:24Z")

</div>

> [@Kazzy](#):
>
> the CronPlus i think it good so im sure i might need it from when i need something

No worries.

The reason I suggested it was because you want a msg to pass IF time was `sunset` to 02:00.  
The cron-plus node can be queried for sunset time / solar status - so you only needed to check (in a function or switch+JSONata) that the time was BEFORE 02:00

e.g....

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/2163aa45f77c8b120a128ab663d6e39181e9ff0a.png)

```auto
[{"id":"426a978b.442178","type":"cronplus","z":"462180de.0f254","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[],"x":260,"y":480,"wires":[["fc656b97.6cd0e8","ee078b8e.73d938"]]},{"id":"83aea31b.5cd13","type":"inject","z":"462180de.0f254","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"command\":\"describe\",\"expressionType\":\"solar\",\"location\":\"54.9992500,-1.4170300\",\"solarType\":\"selected\",\"solarEvents\":\"sunset\",\"timeZone\":\"Europe/London\"}","payloadType":"json","x":130,"y":480,"wires":[["426a978b.442178"]]},{"id":"292746f0.fbd64a","type":"debug","z":"462180de.0f254","name":"is sunset ~ 2am","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":720,"y":540,"wires":[]},{"id":"fc656b97.6cd0e8","type":"switch","z":"462180de.0f254","name":"Is Sunset","property":"(payload.result.solarState.twilight = true)\tor\t(payload.result.solarState.night = true)\t\t \t\t\t\t","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"false","repair":false,"outputs":1,"x":420,"y":480,"wires":[["cb34562e.c5a4d8","5fd56c59.289da4"]]},{"id":"cb34562e.c5a4d8","type":"function","z":"462180de.0f254","name":"Is time < 2am?","func":"var hrs = new Date().getHours();\n\n//since it is unlikely to be dusk at 2pm, a simple check\n//for HOURS being >= 14 OR < 2am is enough\nif(hrs >= 14 || hrs < 2)\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":480,"wires":[["292746f0.fbd64a"]]},{"id":"ee078b8e.73d938","type":"debug","z":"462180de.0f254","name":"cron","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":290,"y":540,"wires":[]},{"id":"5fd56c59.289da4","type":"debug","z":"462180de.0f254","name":"is sunset?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":540,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [22 October 2020 18:09 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/7 "2020-10-22T18:09:55Z")

</div>

Hello,

I think this subflow timerange switch I build a while back would also do this and work across midnight:

> [@Two subflows I made: a timerange switch and a scheduler / dashboard integratable](https://discourse.nodered.org/t/two-subflows-i-made-a-timerange-switch-and-a-scheduler-dashboard-integratable/24586/3):
>
> small update to the time range switch. It now shows weekday abbreviations in the info instead of numbers [{"id":"7cc13ada.b6712c","type":"subflow","name":"timerange","info":"Lets through or blocks a payload\nbased on a time range. This can\neither be configured through the\nenviroment variables in the node ui\nor as described below with a message\nthat has an override topic.\nIf in range the msg will be passed\nto the first output and otherwise\nto the second.\nThe start and stop time needs\nto…

Of course as @Steve-Mcl suggested cron plus would be a lot more flexible and sophisticated.

Johannes

---

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [22 October 2020 18:29 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/8 "2020-10-22T18:29:06Z")

</div>

Thanks Steve-Mcl

That's an interesting approach.. i couldnt think how to the the end range with cron+

But it adding more Nodes to the solution where i like to try and do it all as a single node, so the current way i am doing is no better and no worse off 🙂

I did try Chronos & Thingzi but i think they two also fail to allow for the end time to be anything past 23:59... they make the assumption is a 24Hour clock so starts at 00:00

I used [https://www.epochconverter.com](https://www.epochconverter.com) to inject a timestamp,. saves me having to be awake at 2am to see if any these would work 🙂

Both allowed the flow to pass through with times like 4am being sent to it..

@JGKK just seen yours so will give yours a test as well thanks.

lots of examples so just a case to blend them into what works best ..

thanks chaps.

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [26 October 2020 21:07 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/9 "2020-10-26T21:07:34Z")

</div>

Hi,

I wrote the thingzi timer mainly for my own uses but i actually use it as you suggest myself. While it only allows one time range a day this can be wrapped or inverted as required. Eg. Start at 5pm and end at 2am should wrap round just fine. If not though I’m always interested in suggestions.

Thanks,  
Bryan

---

<div class="post-metadata">

**Author:** ![Kazzy](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Kazzy](https://discourse.nodered.org/u/Kazzy)\
**Post date:** [26 October 2020 22:17 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/10 "2020-10-26T22:17:51Z")

</div>

Thanks Bryan,

what i failed to understand when testing Thingz and Chronos was that it calculates the current time  
when it receives a msg and not me trying to fake a time and passing it to the node. oops.

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [26 October 2020 23:05 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/11 "2020-10-26T23:05:34Z")

</div>

Yeah it doesn’t do that 😀 the time check node is the one I use in this instance though. So given any motion event pass it on, but only if the current time is within the valid range. Much more useful if something else is the trigger and you need a time constraint applied.

---

<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:** [25 December 2020 23:05 UTC](https://discourse.nodered.org/t/time-range-node-recommendation/34651/12 "2020-12-25T23:05:40Z")

</div>

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