Sunevent node to light lamp

Hi,

I've been tinkering with HA and Node-Red for a few days now and I want to make the garden lights and the porch light turn on and off based on a sunevent (node-red-contrib-sunevents (node) - Node-RED).

What I want is for the light to turn on at the sunevent dusk. As far as I know, a light needs the payload "on" to turn on. So based on this assumption, I have built the flow.

Below is an example of the flow as I have solved it myself, but I wonder if it can be done more easily.

The flow has 2 inject nodes so that the sunevent node is triggered every day (as advised in the documentation). Then a GPS location is pushed into the sunevent node. The sunevent node has a payload object with 4 properties: msg.payload.latitude, msg.payload.longitude, msg.payload.sunevent, and msg.payload.datetime. Since I am only interested in msg.payload.sunevent, I first remove the other 3 in a change node. Then the stripped payload goes to a JSON node to convert it into a string, then another change node to convert the string "sunevent":"dusk" to "on".

Is there perhaps an easier way to do this? Probably with function nodes, but I can't really code :9. By the way, the sunevent node keeps posting this at every sunevent and then passes it unchanged to the switch node as a text string instead of "on" because it is not converted if the string is anything other than "sunevent":"dusk". I haven't tested what effect that has on a light, for example.

Tips are welcome!

[{"id":"32cb8da3.eeffc2","type":"tab","label":"Sun Events simple example","disabled":false,"info":""},{"id":"2d8758dc.922e08","type":"inject","z":"32cb8da3.eeffc2","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payloadType":"str","x":110,"y":120,"wires":[["58c171f7.59b7a"]]},{"id":"ae9be1e3.2e7ab","type":"sun events","z":"32cb8da3.eeffc2","testmode":true,"verbose":true,"topic":"sunset","name":"","x":590,"y":180,"wires":[["7c3cf0a444bb5631"],[]]},{"id":"58c171f7.59b7a","type":"change","z":"32cb8da3.eeffc2","name":"set GPS coordinates (change node) ","rules":[{"t":"set","p":"payload.latitude","pt":"msg","to":"52.702521","tot":"num"},{"t":"set","p":"payload.longitude","pt":"msg","to":"3.188700","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["ae9be1e3.2e7ab"]]},{"id":"c0aa61b0.223ae","type":"inject","z":"32cb8da3.eeffc2","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 12 * * *","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":110,"y":240,"wires":[["58c171f7.59b7a"]]},{"id":"448101bf3080f56a","type":"debug","z":"32cb8da3.eeffc2","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":280,"wires":[]},{"id":"7c3cf0a444bb5631","type":"change","z":"32cb8da3.eeffc2","name":"","rules":[{"t":"delete","p":"payload.latitude","pt":"msg"},{"t":"delete","p":"payload.longitude","pt":"msg"},{"t":"delete","p":"payload.datetime","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":380,"wires":[["cf8e5d92f04a1243"]]},{"id":"cf8e5d92f04a1243","type":"json","z":"32cb8da3.eeffc2","name":"","property":"payload","action":"","pretty":false,"x":610,"y":480,"wires":[["a5ea0f971b1aad50"]]},{"id":"a5ea0f971b1aad50","type":"change","z":"32cb8da3.eeffc2","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"{\"sunevent\":\"dusk\"}","fromt":"str","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":480,"wires":[["448101bf3080f56a"]]}]

Install node-red-contrib-cron-plus. Enter your map coordinates. Choose your sun events (dusk, dawn, whatever) and it will output when it happens. No need to inject. No need to check. Simple.

thanks! Will def look into it. And what about my chain of thought like I described? Did it make sense? Just trying to learn :smiley:

I use the Big-Timer node to control my home lighting system and Cron-Plus to control my home surveillance system. Big-Timer is probably a bit of an "over-kill" for this application but was the first node I found many, many years ago when I created the flow and dashboard.




This part of the flow provides feedback to indicate when a light has reacted to its command.

As I wanted my home lighting system to work "unattended" in "auto" and "manual" mode, I ended-up building a Finite State Machine (FSM) to manage and control the various states. The lights come on at Sunset and go off at 22:00hrs. At 22:00hrs until sunrise the home surveillance system takes over monitoring my infrared detectors and sending messages to a Telegram account on my mobile phone if body-heat is detected. I use a number of S20 and S26 Mains Switches flashed with Tasmota to turn the external lights On/Off.

I can't publish the flow just now as it is huge and contains a number of embedded security features applicable to my house and property.
If I get some spare time at Xmas I'll try to simplify it and publish it on... Share Your Projects.

1 Like

I don't have too many lights to control so after some years of using a more complex scalable setup I went back to basics and have a small flow for each lamp:

CRON+ is used for the timed brightness setting but use the sun position nodes to output standard data to MQTT:

So I can have settings that change lighting when the sun is at a specific azimuth/elevation if I want (though I don't actually bother).

Instead, I use the daylight and sleep booleans as you can see.

Some of the lamps do use solar events though:

I really should rationalise things a bit more. But it all works so there isn't much incentive. :slight_smile: It has simply grown and evolved over time.

1 Like

Another angle/approach - I have true adaptive lighting based on a lux sensor that measures the lux outside (could be inside too ofcourse) and I map the incoming value to a range with a range node, between sunset-60 (ie starting 60 mins before sunset) and have a long transition of 2 minutes.

This will slowly turn on the lights based on the outside brightness, the darker it gets, the brighter the lights.

eg. lux 400 - 0 will map to 0 - 60% brightness. Every time a new measurement comes back from the lux sensor the lights will change their brightness.

1 Like