MQTT - Single button press - multiple different actions (in order/sequence)

Hi all,
I'm using Node-RED with HomeAssistant and have a few different automation's up and running (fairly new to it), however I am getting stuck with one requirement in particular and multiple Google searches over the past few days aren't helping me.

I want a single button press to change some LED colours as follows:

  • Press the button once, lights change to blue
  • Press the button again, lights change to red
  • Press the button again, lights change to green
  • Press the button again, lights change to pink
  • Press the button again, goes back to the start of the colour order and repeats

I essentially just want each time the button is pressed to move one step along in the "sequence".

I can already successfully read the MQTT topic of the button in question (fyi it's a Shelly Button1) in Node-RED.
And I can already successfully use the call-service node to switch an LED to a colour.
But I am stuck with how I loop the sequence of colour states in the middle etc.

The closest suggestion I've found so far is this high-level overview on another forum post but am unsure how to implement it.

Any help greatly appreciated :+1:

You need to remember last state. for this there are several solutions. The easiest (for me) is a function node...

image

[{"id":"75f941daf629c2ba","type":"inject","z":"219b0be6.926804","name":"fake button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":1570,"y":320,"wires":[["bcc8bbbf106ec10f"]]},{"id":"bcc8bbbf106ec10f","type":"function","z":"219b0be6.926804","name":"colour cycle","func":"/**** colour list - update as required *****/\nconst colours = [ \n    \"red\", \n    \"blue\", \n    \"green\",\n];\n\n/**** dont change *****/\nlet colour = context.get(\"colour\"); //restore last colour\ncolour = colour == null ? 0 : colour + 1;//inc colour\nif(colour >= colours.length) {\n    colour = 0;\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1730,"y":320,"wires":[["fc3d23dad0eccca5"]]},{"id":"fc3d23dad0eccca5","type":"debug","z":"219b0be6.926804","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1750,"y":360,"wires":[]}]

↑ Flow to import (CTRL+I)

PS, as a new user, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Wow, thanks for the super quick reply Steve :+1:
I've imported your flow and it looks pretty clear so I'll have a play around and see what I can manage.

Tonight's TV viewing will definitely be the video playlist as I hadn't come across these ones yet. :grinning:

2 Likes

Steve, thanks again for you help, all working perfectly now.

For others who are interested, below is a copy of my sequence. The Shelly button1 allows for single, double, and triple press, so I have used the double and triple actions to turn off some lights. And the single presses cycle through the chosen colours on the led strip.

Not sure if anyone has any other suggestion of a more efficient or alternative way of doing it. But this works very well in my case.

[{"id":"ea25eb5e.b24738","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"1bf7a70f.f2eaa9","type":"function","z":"ea25eb5e.b24738","name":"colour cycle","func":"/**** colour list - update as required *****/\nconst colours = [ \n    \"warm-white\", \n    \"cool-white\", \n    \"pink\",\n    \"green\",\n    \"aqua\",\n    \"purple\",\n];\n\n/**** dont change *****/\nlet colour = context.get(\"colour\"); //restore last colour\ncolour = colour == null ? 0 : colour + 1;//inc colour\nif(colour >= colours.length) {\n    colour = 0;\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":160,"wires":[["e6737066.52b7b"]]},{"id":"ce98287e.c5a158","type":"api-call-service","z":"ea25eb5e.b24738","name":"w-white","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\"rgbw_color\":[255,255,127,255],\"brightness\":\"255\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":20,"wires":[[]]},{"id":"e6737066.52b7b","type":"switch","z":"ea25eb5e.b24738","name":"switch colour","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"warm-white","vt":"str"},{"t":"eq","v":"cool-white","vt":"str"},{"t":"eq","v":"pink","vt":"str"},{"t":"eq","v":"green","vt":"str"},{"t":"eq","v":"aqua","vt":"str"},{"t":"eq","v":"purple","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":830,"y":180,"wires":[["ce98287e.c5a158"],["3fb4be2f.803032"],["ac8fba7.13e9048"],["2edba638.21165a"],["e6203f84.80bfe"],["aac44ec4.d4a21"]]},{"id":"3fb4be2f.803032","type":"api-call-service","z":"ea25eb5e.b24738","name":"c-white","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\t   \"rgbw_color\":[0,0,255,255],\t   \"brightness\":\"255\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":80,"wires":[[]]},{"id":"ac8fba7.13e9048","type":"api-call-service","z":"ea25eb5e.b24738","name":"pink","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\t   \"rgbw_color\":[255,0,128,0],\t   \"brightness\":\"255\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1030,"y":140,"wires":[[]]},{"id":"2edba638.21165a","type":"api-call-service","z":"ea25eb5e.b24738","name":"green","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\t   \"rgbw_color\":[64,255,0,0],\t   \"brightness\":\"255\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1030,"y":200,"wires":[[]]},{"id":"e6203f84.80bfe","type":"api-call-service","z":"ea25eb5e.b24738","name":"aqua","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\t   \"rgbw_color\":[0,255,128,0],\t   \"brightness\":\"255\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1030,"y":260,"wires":[[]]},{"id":"aac44ec4.d4a21","type":"api-call-service","z":"ea25eb5e.b24738","name":"purple","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.kitchen_cabinet_leds","data":"{\"rgbw_color\":[61,0,255,0],\"brightness\":\"255\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1030,"y":320,"wires":[[]]},{"id":"ae821bc5.1e1798","type":"mqtt in","z":"ea25eb5e.b24738","name":"SHELLY BUTTON-1","topic":"shellies/BUTTON-1/input_event/0","qos":"2","datatype":"json","broker":"8a8a9b24.3d7728","nl":false,"rap":true,"rh":0,"x":130,"y":200,"wires":[["ccbee992.24be48"]]},{"id":"ccbee992.24be48","type":"switch","z":"ea25eb5e.b24738","name":"NO. OF PRESS","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"S","vt":"str"},{"t":"eq","v":"SS","vt":"str"},{"t":"eq","v":"SSS","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":360,"y":200,"wires":[["1bf7a70f.f2eaa9"],["6ba57213.c7bd5c"],["cc62ecd7.25e69"]]},{"id":"6ba57213.c7bd5c","type":"api-call-service","z":"ea25eb5e.b24738","name":"leds off","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.kitchen_cabinet_leds","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":580,"y":220,"wires":[[]]},{"id":"cc62ecd7.25e69","type":"api-call-service","z":"ea25eb5e.b24738","name":"downlights off","server":"2b29bbf7.71aad4","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.kitchen_lights","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":600,"y":280,"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},{"id":"8a8a9b24.3d7728","type":"mqtt-broker","name":"HA MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.