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"]]}]