# Calculate time difference between sunrise and specific time

**URL:** https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523
**Category:** General
**Created:** [12 May 2021 03:18 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523 "2021-05-12T03:18:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pkjacobsen](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pkjacobsen](https://discourse.nodered.org/u/pkjacobsen)
#### Post date: [12 May 2021 03:18 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523/1 "2021-05-12T03:18:52Z")

</div>

Hello all you good people.  
I would like my outdoor motion sensors to turn the lights on at 5.30 am if the sun is below horizon and turn off at sunrise. Since it's spring here in Denmark, the sun is above horizon at 5.30 and the lights should stay off.  
Any suggestions?

---

<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: [12 May 2021 05:22 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523/2 "2021-05-12T05:22:15Z")

</div>

Here is a way using node-red-contrib-cron-plus  
 ![i57EPVnapc](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e42adcb0120a4962e6d270d385da882ea9cfb95b.gif)

How it works...

- the cron plus node has 3 schedules
  - 05:30 - sends a topic of "on"
  - sunrise - sends a topic of "off"
  - 12:00 - sends a topic of "reset"

- the function has some logic that determines
  - if the "on" (5:30) event has occurred first. If so, the `msg` is allowed to leave the function to turn on your light
  - if the "off" (sunrise) event occurs first, it will block further messages until a "reset" (12:00) occurs.
  - if the "off" (sunrise) event occurs after the "on" (5:30), it will first send the `msg` to turn the light off then block further messages until a "reset" (12:00) occurs.

Demo flow...

```auto
[{"id":"c030d341.8e743","type":"cronplus","z":"4b3f21a3.ba434","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule1","topic":"on","payloadType":"bool","payload":"true","expressionType":"cron","expression":"0 30 5 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule2","topic":"off","payloadType":"bool","payload":"true","expressionType":"solar","expression":"","location":"56.30770998133608 9.583854675292969","offset":"0","solarType":"selected","solarEvents":"sunrise"},{"name":"schedule3","topic":"reset","payloadType":"str","payload":"","expressionType":"cron","expression":"0 0 12 * * ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":670,"y":1340,"wires":[["bfda2f20.dfa2e"]]},{"id":"bfda2f20.dfa2e","type":"function","z":"4b3f21a3.ba434","name":"","func":"var block = context.get(\"block\") || false;\nvar state = context.get(\"state\") || \"off\";\nswitch (msg.topic) {\n case \"on\":\n if(block === false) {\n node.send(msg);\n node.status({ fill: \"green\", shape: \"dot\", text: \"on\" });\n }\n state = \"on\";\n break;\n case \"off\":\n if(state == \"on\") {\n node.send(msg);\n }\n node.status({ fill: \"red\", shape: \"ring\", text: \"off\" });\n state = \"off\"\n block = true;\n break;\n case \"reset\":\n node.status({ fill: \"grey\", shape: \"ring\", text: \"\" });\n block = false;\n break;\n}\ncontext.set(\"state\", state);\ncontext.set(\"block\", block);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":1340,"wires":[["7d9987b6.a1d0a8"]]},{"id":"565a8b77.45e2f4","type":"inject","z":"4b3f21a3.ba434","name":"TEST 5:30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"on","payload":"true","payloadType":"bool","x":680,"y":1420,"wires":[["bfda2f20.dfa2e"]]},{"id":"7b4f11d7.2299e","type":"inject","z":"4b3f21a3.ba434","name":"TEST sunrise","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"off","payload":"false","payloadType":"bool","x":690,"y":1460,"wires":[["bfda2f20.dfa2e"]]},{"id":"6599e953.b3b608","type":"inject","z":"4b3f21a3.ba434","name":"TEST 12:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","payload":"","payloadType":"str","x":690,"y":1500,"wires":[["bfda2f20.dfa2e"]]},{"id":"7d9987b6.a1d0a8","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"topic","statusType":"msg","x":1110,"y":1340,"wires":[]}]

```

---

<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: [12 May 2021 08:20 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523/3 "2021-05-12T08:20:05Z")

</div>

I'm just reworking my lights timing flow but I decided to go a slightly different route and keep track of the sun and moon position in MQTT and a global var. So that can be checked when a light is set to switch on and off. I also need to check existing lighting levels on some of my lights so that they only turn on when we aren't sleeping (another time variable that I track) and when the light levels are low. But I don't have light sensors at every location hence needing the sun position as well.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47eeac06dda407177894dce0ccff0427eff12dbd.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f90888abad21da56ed4ce1f29b94ef9010dd92c.png)

---

<div class="post-metadata">

### Author: ![pkjacobsen](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pkjacobsen](https://discourse.nodered.org/u/pkjacobsen)
#### Post date: [12 May 2021 16:19 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523/4 "2021-05-12T16:19:01Z")

</div>

Thanks! It works just like I wanted.

---

<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: [11 July 2021 16:19 UTC](https://discourse.nodered.org/t/calculate-time-difference-between-sunrise-and-specific-time/45523/5 "2021-07-11T16:19:17Z")

</div>

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