Skip to content

Instantly share code, notes, and snippets.

@greghesp
Last active January 7, 2022 15:03
Show Gist options
  • Save greghesp/a833d32a94be00ac997cceff1b6cb119 to your computer and use it in GitHub Desktop.
Save greghesp/a833d32a94be00ac997cceff1b6cb119 to your computer and use it in GitHub Desktop.
RGB Colour loop for a Home Assistant Light

Use a Link node to connect to the below flow

  • Import the flow to Node Red
  • Pass msg.entity to the flow such as light.desk
  • Pass msg.status. on for color loop, stop to stop the loop, off to turn the light off. If no value provided, color loop will be on
  • Optional: Pass msg.delay. Set a delay in milliseconds. Defaults to 20s (20000) if no value provided

The output will trigger every time flow is actioned, this means if the color loop is active, it will trigger every 20 seconds, or by the delay set. It is advised only to use this for when the color loop is turned off or stopped.
A use case here would be if you want to stop the color loop, and then trigger a scene which will set the light to a fixed color

[{"id":"ec22fd2d9a498086","type":"tab","label":"Color Loop","disabled":false,"info":"","env":[]},{"id":"5c960d526cfff1b2","type":"link in","z":"ec22fd2d9a498086","name":"","links":["dd2114c718df4892","5085829335ba248c","ac68dbc399f99fd1","f23f2d6854efbe20","2764c38f9e14979d"],"x":115,"y":60,"wires":[["8e09f9d06493bcb6"]]},{"id":"651e1d7c5572c654","type":"link out","z":"ec22fd2d9a498086","name":"","mode":"return","links":[],"x":1195,"y":60,"wires":[]},{"id":"8e09f9d06493bcb6","type":"change","z":"ec22fd2d9a498086","name":"Store RGB Color","rules":[{"t":"set","p":"status","pt":"flow","to":"status","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":60,"wires":[["c15001e6c435dbed"]]},{"id":"c15001e6c435dbed","type":"function","z":"ec22fd2d9a498086","name":"Set Flow Var","func":"if(!msg.entity) {\n node.status({\n text: 'No Light defined',\n shape: 'ring',\n fill: 'red'\n });\n return; \n}\n\nflow.set(\"i\", 0);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":60,"wires":[["e6d566226c87b354"]]},{"id":"e6d566226c87b354","type":"function","z":"ec22fd2d9a498086","name":"Control Light","func":"setNodeStatus()\nswitch(flow.get(\"status\")){\n case(\"on\"):\n msg.payload = startColorFlow();\n break;\n case(\"stop\"):\n // msg.payload = stopColorFlow();\n break;\n case(\"off\"):\n msg.payload = turnOff();\n break;\n default:\n msg.payload = turnOff();\n}\n\nreturn msg;\n\nfunction startColorFlow(){\n var i = flow.get(\"i\");\n var red = Math.round(Math.sin(0.3*i + 0) * 127 + 128);\n var green = Math.round(Math.sin(0.3*i + 2) * 127 + 128);\n var blue = Math.round(Math.sin(0.3*i + 4) * 127 + 128);\n var rgb_array = null;\n \n if(flow.get(\"rgb_color\") && i==0) rgb_array = flow.get(\"rgb_color\")\n else {\n rgb_array = [red, green, blue]\n flow.set(\"rgb_color\", rgb_array)\n }\n \n i++;\n if (i>=32) i = 0;\n\n flow.set(\"i\", i);\n \n return {\n domain: 'light',\n service: 'turn_on',\n data: {\n entity_id: msg.entity, \n rgb_color: rgb_array,\n transition: 2\n }\n };\n}\n\nfunction stopColorFlow(){\n node.warn(\"rgbcolor\", flow.get(\"rgb_color\"))\n return {\n domain: 'light',\n service: 'turn_on',\n data: {\n rgb_color: flow.get(\"rgb_color\") || [253, 244, 220],\n entity_id: msg.entity\n }\n };\n}\n\nfunction turnOff(){\n return {\n domain: 'light',\n service: 'turn_off',\n data: {\n entity_id: msg.entity, \n transition: 1\n }\n };\n}\n\nfunction setNodeStatus(){\n const data = {};\n if(flow.get(\"status\") === \"on\") {\n data.text = \"Color Loop Enabled\",\n data.fill = \"green\"\n }\n if(flow.get(\"status\") === \"off\") {\n data.text = \"Color Loop Off\",\n data.fill = \"grey\"\n }\n if(flow.get(\"status\") === \"stop\") {\n data.text = \"Color Loop Stopped\",\n data.fill = \"red\"\n }\n node.status({\n text: data.text,\n shape: 'dot',\n fill: data.fill\n});\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":60,"wires":[["bdd4747a8b340a58","57cb4465d1220e9d"]]},{"id":"57cb4465d1220e9d","type":"delay","z":"ec22fd2d9a498086","name":"Delay","pauseType":"delayv","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":590,"y":160,"wires":[["5e81bbe30f142018"]]},{"id":"2ab77f2d250180d5","type":"api-call-service","z":"ec22fd2d9a498086","name":"Call Service Node","server":"68b1f3b6.69c6ac","version":3,"debugenabled":true,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":60,"wires":[["651e1d7c5572c654"]]},{"id":"bdd4747a8b340a58","type":"switch","z":"ec22fd2d9a498086","name":"","property":"status","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":60,"wires":[["2ab77f2d250180d5"],["2ab77f2d250180d5"]]},{"id":"5e81bbe30f142018","type":"switch","z":"ec22fd2d9a498086","name":"","property":"status","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":710,"y":160,"wires":[["e6d566226c87b354"]]},{"id":"68b1f3b6.69c6ac","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]
[{"id":"19ee12adcfd60da7","type":"change","z":"f68c2685.c5d538","name":"Set","rules":[{"t":"set","p":"status","pt":"msg","to":"stop","tot":"str"},{"t":"set","p":"entity","pt":"msg","to":"light.shelf","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":340,"wires":[["ac68dbc399f99fd1"]]},{"id":"ac68dbc399f99fd1","type":"link out","z":"f68c2685.c5d538","name":"Color Loop","mode":"link","links":["5c960d526cfff1b2"],"x":655,"y":340,"wires":[]},{"id":"4c142c9135376b20","type":"inject","z":"f68c2685.c5d538","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":410,"y":340,"wires":[["19ee12adcfd60da7"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment