# Sunset-Sunrise time period detection

**URL:** https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579
**Category:** General
**Created:** [3 November 2023 13:31 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579 "2023-11-03T13:31:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)
#### Post date: [3 November 2023 13:31 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/1 "2023-11-03T13:31:58Z")

</div>

I currently have a process that has a random event that turns on a lamp, but I want that lamp only to turn on between sunset & sunrise.

Ideally I would prefer a node that would pass an input to the output between sunrise & sunset, but have not found a single node that has that capability,  
I have found the following nodes; cronplus & suncalc.

cronplus seems overly complicated for what I need. Two additional issues, even though I specified my time-zone, at the bottom of the node it displays a time that's not related to anything, and I don't know what the node input does (an inject node does not produce any output).

suncalc appears to function; as it can produce a 0 or 1 output every minute depending on sunrise/sunset, which I could use to set a global var, but I tried accessing it directly from a function-node with the following code:

> var suncalc = global.get('suncalc');  
> node.error(suncalc)  
> var location = { latitude: 40.392, longitude: -74.1843 };  
> var times = suncalc.getTimes(new Date(), location.latitude, location.longitude);  
> var sunrise = times.sunrise.getTime();  
> var sunset = times.sunset.getTime();  
> var now = new Date().getTime();

The code returned this error:

> function : (error)  
> "TypeError: Cannot read properties of undefined (reading 'getTimes')";

and node.error(suncalc) returned "" empty for var suncalc.

Any suggestions would be appreciated,  
X.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2023 13:57 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/2 "2023-11-03T13:57:50Z")

</div>

This example should work for you, the module is install using [functionExternalModule](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

```auto
[{"id":"aa4c8a427a7ebd4f","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":800,"wires":[["b14f07a1326ae9bf"]]},{"id":"b14f07a1326ae9bf","type":"function","z":"d1395164b4eec73e","name":"function 144","func":"\n\nvar location = { latitude: 40.392, longitude: -74.1843 };\nvar times = suncalc.getTimes(new Date(), location.latitude, location.longitude);\nmsg = {\n sunrise: times.sunrise.getTime(),\n sunset: times.sunset.getTime(),\n now: new Date().getTime()\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"suncalc","module":"suncalc"}],"x":390,"y":780,"wires":[["d995a939c332ca86"]]},{"id":"d995a939c332ca86","type":"debug","z":"d1395164b4eec73e","name":"debug 2452","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":780,"wires":[]}]

```

 ![chrome_screenshot_1699019995285](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3aeee54d3d68f7167b7d43762a5b3be125609272.jpeg)

---

<div class="post-metadata">

### Author: ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)
#### Post date: [3 November 2023 14:04 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/3 "2023-11-03T14:04:08Z")

</div>

Thanks E1cid,

I copied your code into a new flow, and it functions. This implies that suncalc is inherently global; is that true?

X.

UPDATE: I just saw that you specified the module in the setup -got it.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2023 14:07 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/4 "2023-11-03T14:07:57Z")

</div>

You would need to add `suncalc` in the function setup tab of each function node you wish to use it in.

---

<div class="post-metadata">

### Author: ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)
#### Post date: [3 November 2023 14:12 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/5 "2023-11-03T14:12:21Z")

</div>

Got it; thanks looks like it will do the job.

BTW: The issues I referred to regarding cronplus; do you have any familiarity with that node?

- what is the input for?
- even with the correct time-zone specified, why does it display some unrelated time below the node?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [3 November 2023 14:15 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/6 "2023-11-03T14:15:11Z")

</div>

> [@xytsrm](#):
>
> BTW: The issues I referred to regarding cronplus; do you have any familiarity with that node?

Not familiar with the node. Start a new topic as there are many here that are familiar with it. Best to keep topics and answer separate, as future readers can then search topic with more ease.

[edit]

> [@xytsrm](#):
>
> Ideally I would prefer a node that would pass an input to the output between sunrise & sunset, but have not found a single node that has that capability,

You could make the function node do it for you.

---

<div class="post-metadata">

### Author: ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)
#### Post date: [3 November 2023 14:16 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/7 "2023-11-03T14:16:18Z")

</div>

Ok, thanks again 😃

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [3 November 2023 15:27 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/8 "2023-11-03T15:27:59Z")

</div>

> [@xytsrm](#):
>
> Any suggestions would be appreciated,

I use suncalc and I have a simple flow that is triggered every 1 minute:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/9559c9082f3c180de37c14d461843d26c31bf8eb.png)

That outputs data to MQTT so that updates can be used in other flows.

The first node actually outputs a bunch of stuff:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dddfdfdf67bef851d1cdd42102e523ef09fd8ae5.png)

---

<div class="post-metadata">

### Author: ![FireWizard52](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/firewizard52/32/34508_2.png) [@FireWizard52](https://discourse.nodered.org/u/FireWizard52)
#### Post date: [3 November 2023 18:02 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/9 "2023-11-03T18:02:06Z")

</div>

I used to use: [node-red-contrib-today-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.

---

<div class="post-metadata">

### Author: ![xytsrm](https://avatars.discourse-cdn.com/v4/letter/x/6de8d8/32.png) [@xytsrm](https://discourse.nodered.org/u/xytsrm)
#### Post date: [3 November 2023 21:35 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/10 "2023-11-03T21:35:56Z")

</div>

Thanks for the good suggestions guys; but E1cid's Function-Node import of suncalc works the best for my application.

---

<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: [2 January 2024 21:36 UTC](https://discourse.nodered.org/t/sunset-sunrise-time-period-detection/82579/11 "2024-01-02T21:36:19Z")

</div>

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