One Button Press, cycle through 3 Yeelight Colours

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.

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.

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!

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...

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.

1 Like

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

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

1 Like

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! :smiley:

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.

1 Like

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.

1 Like

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

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!

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

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.

[{"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.

@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_nextyou cycle through 4 states. Look at Input Select - Home Assistant