Sunrise/sunset casting

Hi

I'm new at Node Red, and now (in Home Assistant Node Red), I want to cast when the sun will rise and set. I found this api https://api.sunrise-sunset.org.

When I put the output in a debug node I get the correct output, but when I try to cast it to a google home device via the cast node, I get the error: No contentType given!, using "audio/basic" which is maybe wrong! (https://api.sunrise-sunset.org/json?&formatted=0&lat=51.05&lng=5.22) .

I searched around a bit, but didn't find a solution.

Information: It doesn't need to be this api, I just want to have the sunrise and sunset time (and maybe if possible, without the date so only the time) and cast it.

Hope you can help me out.

Regards
Christophe

Hi

You dont really need to go to an external API - you can simply use node-red-contrib-cron-plus...

image

[{"id":"d79fd70f.20bee8","type":"cronplus","z":"553814a2.1248ec","name":"sunrise and sunset minus 10mins","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"sunup","topic":"sunup","payloadType":"json","payload":"{\"message\":\"It will be Sunrise in 10 minutes\"}","expressionType":"solar","expression":"0 * * * * * *","location":"54.998989574504265 -1.417386531829834","offset":"-10","solarType":"selected","solarEvents":"sunrise"},{"name":"sundown","topic":"sundown","payloadType":"json","payload":"{\"message\":\"It will be Sunset in 10 minutes\"}","expressionType":"solar","expression":"0 * * * * * *","location":"54.998989574504265 -1.417386531829834","offset":"-10","solarType":"selected","solarEvents":"sunset"}],"x":540,"y":860,"wires":[["be81c8d4.22abd8"]]},{"id":"2f2e57c4.fffbd8","type":"inject","z":"553814a2.1248ec","name":"It will be Sunrise in 10 minutes","topic":"It will be Sunrise in 10 minutes","payload":"{\"message\":\"It will be Sunrise in 10 minutes\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":960,"wires":[["be81c8d4.22abd8"]]},{"id":"93c6685e.5afb28","type":"inject","z":"553814a2.1248ec","name":"It will be Sunset in 10 minutes","topic":"It will be Sunset in 10 minutes","payload":"{\"message\":\"It will be Sunset in 10 minutes\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":1000,"wires":[["be81c8d4.22abd8"]]},{"id":"be81c8d4.22abd8","type":"cast-to-client","z":"553814a2.1248ec","name":"","url":"","contentType":"","message":"","language":"en","ip":"192.168.1.19","port":"8009","volume":"100","x":830,"y":860,"wires":[[]]},{"id":"378a201b.01a73","type":"comment","z":"553814a2.1248ec","name":"test operation","info":"","x":470,"y":920,"wires":[]}]
1 Like

Ok, I'll give it a try tomorrow (I'm now at work) and get back to you, thanks.

Several other nodes also contain sun/moon and other astronomical time measures so if CRON+ is too complex for you, try some of the other timer nodes. CRON+ is the most versatile though.

1 Like

You can also use node-red-contrib-sun-position, which has an Inject node similar to the standard one which allows to inject any message with any payload at a sun-time (sunset,sunrise,...).

Ok, maybe I'm not getting this right (or I didn't explain it well). What I'm gonna use it for is a morning routine. So, if the morning routine triggers, it must say: "Today, the weather is ... and the sun will rise at (time_sunrise) and set at (time_sunset). So definitely not when the sun comes up, cause in the summer I would hate to be awake at 5am or so when I could sleep longer (nobody does I think). Can I configure this cronplus node for that too? And if yes, how?

I concur :wink:

I cant help with the weather part but the sunrise / sunset times are fairly easy.

Obviously I dont know how you intend to trigger the flow (6am alarm? a PIR sensor? a button on your wall???) but in the below example I use a CRON node to fire at 6am Monday ~ Friday.

So considerations you need to make are - what if the sun is already up when the flow is triggered (i.e. mid-summer sunrise is at before 5am where I am) so if you look at the function i wrote, i get it to say "... the sun is already up and it sets at xx yy")

The cron node is immensely configurable (read the built in help) you can use times, CRON schedules, solar events etc etc. You can also automate it (i.e. set it from input mesages like the dashboard).

So here is the flow I knocked up to do the message you requested (I will let you replace the fake weather part)...

[{"id":"ba53ccac.28a1d","type":"cast-to-client","z":"21106466.70e60c","name":"","url":"","contentType":"","message":"","language":"en","ip":"192.168.1.19","port":"8009","volume":"100","x":1690,"y":220,"wires":[[]]},{"id":"8f1e45ea.c35958","type":"cronplus","z":"21106466.70e60c","name":"describer","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[],"x":1540,"y":160,"wires":[["e0041b7e.81f008","70ef167f.cbc798"]]},{"id":"488a6eb1.f1ef8","type":"debug","z":"21106466.70e60c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1650,"y":280,"wires":[]},{"id":"e0041b7e.81f008","type":"function","z":"21106466.70e60c","name":"generate message to cast","func":"var now = new Date();\nvar sunriseTime;\nvar sunsetTime;\n\n//find the sunset & sunrise items\nvar sunrise = msg.payload.result.eventTimes.find(e => e.event == \"sunrise\")\nvar sunset = msg.payload.result.eventTimes.find(e => e.event == \"sunset\")\n\n//first, ensure sunrise comes before sunset\nif(sunrise) sunriseTime = sunrise.time;\nif(sunset) sunsetTime = sunset.time;\nif(sunriseTime && sunsetTime && sunriseTime > sunsetTime ) {\n    //sunriseTime is before sunsetTime, delete it!\n    sunriseTime = null;\n}\n\nvar sunInfo = \"\";\n\nif(sunriseTime && sunriseTime > now) {\n    let hh = sunriseTime.getHours();\n    let mm = sunriseTime.getMinutes();\n    sunInfo = \"The sun will rise at \" + hh + \" \" + mm;\n} else {\n    sunInfo = \"The sun is already up\";\n}\n\nif(sunsetTime && sunsetTime > now) {\n    let hh = sunsetTime.getHours();\n    let mm = sunsetTime.getMinutes();\n    if(sunInfo) {\n        sunInfo += \" and\";\n    } else {\n        sunInfo += \"The sun\";\n    }\n    sunInfo += \" will set at \" + hh + \" \" + mm;\n}\n\n//build the message to cast\nvar weather = flow.get(\"weather\");\nvar message = weather ?  weather : \"\";\nmessage += message ? \" \" + sunInfo : sunInfo;\n\nmsg.payload = {\n    message: message\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":1370,"y":220,"wires":[["488a6eb1.f1ef8","ba53ccac.28a1d"]]},{"id":"ee767009.e8076","type":"change","z":"21106466.70e60c","name":"Describe solar events","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"command\":\"describe\",\"expressionType\":\"solar\",\"solarType\":\"selected\",\"solarEvents\":\"sunrise,sunset\",\"location\":\"54.998989574504265 -1.417386531829834\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1360,"y":160,"wires":[["8f1e45ea.c35958"]]},{"id":"111511e0.66ff8e","type":"change","z":"21106466.70e60c","name":"Get weather info (faked)","rules":[{"t":"set","p":"weather","pt":"flow","to":"Today, rain is expected.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1490,"y":100,"wires":[["ee767009.e8076","436d39b2.8ac038"]]},{"id":"9f77c2a8.c9ac5","type":"cronplus","z":"21106466.70e60c","name":"6am","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"morning_alert","topic":"morning_alert","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 6  *  * mon-fri","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":1310,"y":100,"wires":[["111511e0.66ff8e"]]},{"id":"436d39b2.8ac038","type":"debug","z":"21106466.70e60c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1730,"y":100,"wires":[]},{"id":"70ef167f.cbc798","type":"debug","z":"21106466.70e60c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1730,"y":160,"wires":[]}]
1 Like

Yes, I didn't do that well :grinning:

No need to, I got that one nailed

I was thinking about a webhook, cause then I could link it to Tasker on my Android phone.

Nice you thought of that, didn't even come up in my grey cells :upside_down_face: :wink:

Very much appreciated. Thanks.

Maybe one more thing: would it be possible to have the volume at a different level at a different time? For example, if I'm working, I'll get up at 7 am and then I want it to be 30, but in the weekend (after 9 am) I want it to be 60. Is that possible too?

I think it's possible (I'm away from computers for a few days so can't help) Might be worth a new thread tho.

1 Like