# One Button Press, cycle through 3 Yeelight Colours

**URL:** <https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607>\
**Category:** General\
**Created:** [27 May 2019 17:25 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607 "2019-05-27T17:25:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoap](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@brunoap](https://discourse.nodered.org/u/brunoap)\
**Post date:** [27 May 2019 17:25 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/1 "2019-05-27T17:25:37Z")

</div>

Hi. I have one RF Button that publishes the same payload on MQTT.  
How to make every button press, cycle an Yeelight through: White, Green, Purple, OFF and repeat...  
Input MQTT, example: home/OpenMQTTGateway/RFtoMQTT with payload 123456.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [27 May 2019 17:38 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/2 "2019-05-27T17:38:50Z")

</div>

What have you tried? Have you something already to share? It really is "multipart question" so it will be long story to explain all possible solutions to develop this kind of logic.

---

<div class="post-metadata">

**Author:** ![brunoap](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@brunoap](https://discourse.nodered.org/u/brunoap)\
**Post date:** [27 May 2019 18:01 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/3 "2019-05-27T18:01:21Z")

</div>

Unfortunately i haven't tried anything. I am using on home assistant, where i've found some solutions using input\_select and automation.  
Thinking it could be easier on Node-Red...  
Thanks!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 May 2019 19:53 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/4 "2019-05-27T19:53:27Z")

</div>

What part do you need help with?

As @hotNipi said this is a multi part question.

Part 1...  
As your button already publishes an MQTT message, you can easily subscribe to that. That will be the start of your flow - an `MQTT in` node that listens to the appropriate topic.

Part 2...

> [@brunoap](#):
>
> How to make every button press, cycle an Yeelight through: White, Green, Purple, OFF and repeat...

This part is bread and butter stuff for node red.  
Break the problem down in your head then use a series of `delay` nodes to create your logic.

Part 3...  
Controlling yeelight. I'm sorry I have no knowledge of this - I'm sure you can figure this part out.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 May 2019 19:57 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/5 "2019-05-27T19:57:03Z")

</div>

Part 3 additional...  
Seems there is a contrib node for controlling the yeelight.

> **[node-red-contrib-yeelight](https://flows.nodered.org/node/node-red-contrib-yeelight)**
>
> Yeelight control node for node-red.

So to answer your question, node red is an excellent fit for this project.

---

<div class="post-metadata">

**Author:** ![brunoap](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@brunoap](https://discourse.nodered.org/u/brunoap)\
**Post date:** [27 May 2019 20:00 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/6 "2019-05-27T20:00:31Z")

</div>

Hi. The input and output parts, are already kind easy for me. What i dont know, is the processing part. Which receives the same input, but need to cycle through 4 states.  
Thanks! 😃

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [27 May 2019 20:08 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/7 "2019-05-27T20:08:21Z")

</div>

High level solution logic explanation:  
Hold current state in node red as index in array of all possible states.  
On input add 1 to index but check If new index is equal with array length set index to 0.  
Store index as current index  
Send new state got from array from that index.

Edit:  
Made correction in order of steps. Asking array elements by index should always stay in boundaries.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [28 May 2019 00:08 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/8 "2019-05-28T00:08:53Z")

</div>

> [@brunoap](#):
>
> cycle an Yeelight through: White, Green, Purple, OFF and repeat...

This sounds like a perfect application for a finite state machine, so you might try `node-red-contrib-dsm`. The node has a bit of a learning curve, but it could be worth the effort. I suggest you skip most of the README and go straight to the examples. The `Traffic lights` example, despite having "no practical use," seems to be almost what you need.

---

<div class="post-metadata">

**Author:** ![brunoap](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@brunoap](https://discourse.nodered.org/u/brunoap)\
**Post date:** [28 May 2019 02:51 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/9 "2019-05-28T02:51:39Z")

</div>

Hi! I am very bad on coding. 😊 This Traffic Lights example, using dsm seems what i was looking for. Few coding and quick results. I will try and post results. Thanks! 😃

---

<div class="post-metadata">

**Author:** ![nodenewbie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodenewbie/32/59397_2.png) [@nodenewbie](https://discourse.nodered.org/u/nodenewbie)\
**Post date:** [24 April 2022 05:18 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/10 "2022-04-24T05:18:13Z")

</div>

> [@drmibell](#):
>
> contrib-dsm

Hi! I used the great suggestions here to make a switch using a tradri remote. I have set one button to cycle forward through my light settings, but am not sure how I can make another button cycle backwards? See the code attached to see how far I got!

```auto
[{"id":"56b4dc5804ea7fc4","type":"tab","label":"Living room","disabled":false,"info":"","env":[]},{"id":"9846adf4c977d6da","type":"deconz-input","z":"56b4dc5804ea7fc4","name":"Tradfri button event watcher","topic":"","config_version":2,"server":"de1c643661343cb4","statustext":"","statustext_type":"default","search_type":"device","device_list":["sensors/uniqueid/04:cd:15:ff:fe:2f:82:9a-01-1000"],"device_name":"","query":"{}","outputs":1,"output_rules":[{"type":"state","format":"single","output":"always","onstart":true,"payload":["buttonevent"]}],"x":120,"y":220,"wires":[["163c070383027236"]]},{"id":"163c070383027236","type":"switch","z":"56b4dc5804ea7fc4","name":"Tradri button checker","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1002","vt":"num"},{"t":"eq","v":"2002","vt":"num"},{"t":"eq","v":"3002","vt":"num"},{"t":"eq","v":"4002","vt":"num"},{"t":"eq","v":"5002","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":440,"y":220,"wires":[[],[],[],["1153347edecb67a5"],["498282321d6d197d"]]},{"id":"58466e228a6ae3ca","type":"switch","z":"56b4dc5804ea7fc4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"},{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":1010,"y":220,"wires":[["9d8cb82783d9aa2f"],["aac9f79144a348d2"],["6c36427e7a82723c"],["1625558914085e38"],["0514d625f05dac9f"]]},{"id":"1153347edecb67a5","type":"change","z":"56b4dc5804ea7fc4","name":"Go forward","rules":[{"t":"set","p":"topic","pt":"msg","to":"tick","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":180,"wires":[["dbf762f0a8ac943a"]]},{"id":"498282321d6d197d","type":"change","z":"56b4dc5804ea7fc4","name":"Go back","rules":[{"t":"set","p":"topic","pt":"msg","to":"untick","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":240,"wires":[["dbf762f0a8ac943a"]]},{"id":"dbf762f0a8ac943a","type":"dsm","z":"56b4dc5804ea7fc4","name":"Switch","sm_config":"{\n \"stateOutput\": \"payload\",\n \"currentState\": \"1\",\n \"states\": {\n \"1\": {\n \"tick\": \"2\",\n \"untick\": \"5\"\n\n },\n \"2\": {\n \"tick\": \"3\",\n \"untick\": \"1\"\n\n },\n \"3\": {\n \"tick\": \"4\",\n \"untick\": \"2\"\n\n },\n \"4\": {\n \"tick\": \"5\",\n \"untick\": \"3\"\n\n },\n \"5\": {\n \"tick\": \"1\",\n \"untick\": \"4\"\n\n }\n }\n}","x":860,"y":200,"wires":[["58466e228a6ae3ca"]]},{"id":"9d8cb82783d9aa2f","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"","server":"bb41d7c4.cad268","version":5,"debugenabled":true,"domain":"light","service":"turn_off","areaId":[],"deviceId":["4f0ae39c15581d7f831f00a766d1c89a"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1190,"y":180,"wires":[[]]},{"id":"aac9f79144a348d2","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"","server":"bb41d7c4.cad268","version":5,"debugenabled":true,"domain":"light","service":"turn_off","areaId":[],"deviceId":["4f0ae39c15581d7f831f00a766d1c89a"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1190,"y":220,"wires":[[]]},{"id":"6c36427e7a82723c","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"","server":"bb41d7c4.cad268","version":5,"debugenabled":true,"domain":"light","service":"turn_off","areaId":[],"deviceId":["4f0ae39c15581d7f831f00a766d1c89a"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1190,"y":260,"wires":[[]]},{"id":"1625558914085e38","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"","server":"bb41d7c4.cad268","version":5,"debugenabled":true,"domain":"light","service":"turn_off","areaId":[],"deviceId":["4f0ae39c15581d7f831f00a766d1c89a"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1190,"y":300,"wires":[[]]},{"id":"0514d625f05dac9f","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"","server":"bb41d7c4.cad268","version":5,"debugenabled":true,"domain":"light","service":"turn_off","areaId":[],"deviceId":["4f0ae39c15581d7f831f00a766d1c89a"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1190,"y":340,"wires":[[]]},{"id":"de1c643661343cb4","type":"deconz-server","name":"Phoscon-GW","config_version":1,"ip":"core-deconz.local.hass.io","port":"40850","ws_port":"8081","secure":false,"polling":"15"},{"id":"bb41d7c4.cad268","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}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 April 2022 06:35 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/11 "2022-04-24T06:35:57Z")

</div>

Using contrib-dsm is complete overkill & just complicates matters.

A simple function node is all that is required to cycle forwards / backwards through a set of colours.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be812a78c41ffcdfb90c152151d2d184353de792.png)

```auto
[{"id":"163c070383027236","type":"switch","z":"56b4dc5804ea7fc4","name":"Tradri button checker","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1002","vt":"num"},{"t":"eq","v":"2002","vt":"num"},{"t":"eq","v":"3002","vt":"num"},{"t":"eq","v":"4002","vt":"num"},{"t":"eq","v":"5002","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":440,"y":220,"wires":[[],[],[],["89fb1e5ac9c2d506"],["89fb1e5ac9c2d506"]]},{"id":"58aee18d26653bc7","type":"inject","z":"56b4dc5804ea7fc4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4002","payloadType":"num","x":210,"y":200,"wires":[["163c070383027236"]]},{"id":"1e4b989bc312e05d","type":"inject","z":"56b4dc5804ea7fc4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5002","payloadType":"num","x":210,"y":240,"wires":[["163c070383027236"]]},{"id":"89fb1e5ac9c2d506","type":"function","z":"56b4dc5804ea7fc4","name":"colour cycle","func":"/ ****colour list - update as required***** /\nconst colours = [\n { \"rgbw_color\": [255, 255, 127, 255], \"brightness\": \"255\" }, //\"warm-white\", \n { \"rgbw_color\": [0, 0, 255, 255], \"brightness\": \"255\" }, //\"cool-white\", \n { \"rgbw_color\": [255, 0, 128, 0], \"brightness\": \"255\" }, //\"pink\",\n { \"rgbw_color\": [64, 255, 0, 0], \"brightness\": \"255\" }, //\"green\",\n { \"rgbw_color\": [0, 255, 128, 0], \"brightness\": \"255\" }, //\"aqua\",\n { \"rgbw_color\": [61, 0, 255, 0], \"brightness\": \"255\" }, //\"purple\",\n];\n\n/ ****dont change***** /\nconst inc = msg.payload === 4002;\nlet colour = context.get(\"colour\"); //restore last colour\n\nif(inc) {\n colour = colour == null ? 0 : colour + 1;//inc colour\n if(colour >= colours.length) {\n colour = 0;\n }\n} else {\n //decrement\n colour = colour == null ? 0 : colour - 1;//dec colour\n if (colour < 0) {\n colour = colours.length-1;\n }\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":240,"wires":[["63e178b832de9a4b","1290c123da086427"]]},{"id":"63e178b832de9a4b","type":"api-call-service","z":"56b4dc5804ea7fc4","name":"light.kitchen_cabinet_leds","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"payload","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":930,"y":240,"wires":[[]]},{"id":"1290c123da086427","type":"debug","z":"56b4dc5804ea7fc4","name":"colour","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":870,"y":300,"wires":[]},{"id":"2b29bbf7.71aad4","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

**NOTE**  
I have made many assumptions (I dont use HA, I dont have YeeLight, I have no idea if you are even cycling colours like this 3 YEAR OLD thread was originally about) so it may help you, it may not.

TBH, I would suggest you start a new thread, with appropriate title, include information about the kit you have, what you are trying to achieve & any progress you have made.

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [24 April 2022 10:26 UTC](https://discourse.nodered.org/t/one-button-press-cycle-through-3-yeelight-colours/11607/12 "2022-04-24T10:26:17Z")

</div>

@Steve-Mcl solution is a fully functional solution using NodeRed  
If you want to make more Home Assistant based, you could create an `input_select` in HA with the four states that you want (White, Green, Purple, OFF) and then through successive call of `input_select.select_next`you cycle through 4 states. Look at [Input Select - Home Assistant](https://www.home-assistant.io/integrations/input_select/)
