Calculate time difference between sunrise and specific time

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?

Here is a way using node-red-contrib-cron-plus
i57EPVnapc

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

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

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
image

Thanks! It works just like I wanted.

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