Make light alternate between colors with button clicks

I’ve got a smart RGB bulb in my son’s room that we use to let him know when he can come out after bed/nap. When it turns green, he can come out, etc.

I have a spare Samsung button laying around, so I thought I’d use it so he could press it to cycle through colors but I cannot figure it out for the life of me.

I was hoping to do it like this: Single press -> Light off -> Turn on light or Single Press -> Light on -> turn light red -> press again -> turn light green, etc.

Double press turns it off.

Long press color loops.

I have the initial turn on and turn red working, as well as turn off. I can start the color loop, but have no idea how to stop it. I the color loop flow somewhere in this community.

I’ve tried many different ways to get it to manually cycle through colors but I cannot get it to work. Can someone point me in the right direction to make this work?

Also, how do you stop a delay node in the color loop? I’ve searched and cannot find this info anywhere.

[{"id":"574f0b87.a5b294","type":"inject","z":"5f5b2ee4.811bd","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":400,"wires":[["8849a33e.1bcde"]]}]

I’ve been doing a lot of research on this, and I’ve been trying it with the node-red-contrib-dsm using the “traffic lights” example, and I just cant get it figured out. Currently, it just flashes through all the colors and stops at Green. Does anyone have any thoughts?

[{"id":"632239a.6b393c8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"3e26b6d9.0745fa","type":"dsm","z":"632239a.6b393c8","name":"traffic lights","sm_config":"{\n    \"stateOutput\": \"payload\",\n    \"currentState\": \"off\",\n    \"states\": {\n        \"off\": {\n            \"tick\": \"red\"\n        },\n        \"red\": {\n            \"tick\": \"red-yellow\"\n        },\n        \"red-yellow\": {\n            \"tick\": \"green\"\n        },\n        \"green\": {\n            \"tick\": \"yellow\"\n        },\n        \"yellow\": {\n            \"tick\": \"red\"\n        }\n    }\n}","x":280,"y":200,"wires":[["933aaac9.68d598","eeeeddd0.fb6d4","6ef5c626.d42a08"]]},{"id":"b9d973d8.bc3a4","type":"inject","z":"632239a.6b393c8","name":"Trigger","topic":"tick","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":100,"wires":[[]]},{"id":"95a15a62.9e65c8","type":"comment","z":"632239a.6b393c8","name":"Traffic lights _______________________________________","info":"","x":240,"y":20,"wires":[]},{"id":"933aaac9.68d598","type":"dsm","z":"632239a.6b393c8","name":"","sm_config":"{\n    \"methods\": {\n        \"onTransition\": \"output=true;if(msg.payload==='red'||msg.payload==='red-yellow') {msg.payload=1;sta.fill='red';} else {msg.payload=0;sta.fill='grey';}\",\n        \"status\": {\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"msg.payload\"\n            }\n        }\n    }\n}","x":470,"y":100,"wires":[["75beb9c9.85eb08","e7527bb2.fb0458"]]},{"id":"eeeeddd0.fb6d4","type":"dsm","z":"632239a.6b393c8","name":"","sm_config":"{\n    \"methods\": {\n        \"onTransition\": \"output=true;if(msg.payload==='yellow'||msg.payload==='red-yellow') {msg.payload=1;sta.fill='yellow';} else {msg.payload=0;sta.fill='grey';}\",\n        \"status\": {\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"msg.payload\"\n            }\n        }\n    }\n}","x":470,"y":220,"wires":[["ce47a283.6881a","2c859c.0bf55a64"]]},{"id":"6ef5c626.d42a08","type":"dsm","z":"632239a.6b393c8","name":"","sm_config":"{\n    \"methods\": {\n        \"onTransition\": \"output=true;if(msg.payload==='green') {msg.payload=1;sta.fill='green';} else {msg.payload=0;sta.fill='grey';}\",\n        \"status\": {\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"msg.payload\"\n            }\n        }\n    }\n}","x":470,"y":320,"wires":[["6d4ba3ad.f87eac","f89ae6fb.3fe008"]]},{"id":"a6dace16.84fcf","type":"switch","z":"632239a.6b393c8","name":"which button?","property":"payload.event.device_ieee","propertyType":"msg","rules":[{"t":"eq","v":"28:6d:97:00:01:08:59:e2","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":240,"y":400,"wires":[["f61f2674.32fb48","77ca9685.0ba948"]]},{"id":"f61f2674.32fb48","type":"switch","z":"632239a.6b393c8","name":"Wyatt's Bedroom Button","property":"payload.event.command","propertyType":"msg","rules":[{"t":"eq","v":"button_single","vt":"str"},{"t":"eq","v":"button_double","vt":"str"},{"t":"eq","v":"button_hold","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":450,"y":400,"wires":[["51951b02.b11884","2c8e1d40.ba0312"],["e31cfe3d.25302"],[]]},{"id":"ea5b0d75.533f8","type":"server-events","z":"632239a.6b393c8","name":"zha_event","server":"26956a45.92d866","event_type":"zha_event","x":80,"y":400,"wires":[["a6dace16.84fcf","c2ec66ec.d866f8"]]},{"id":"77ca9685.0ba948","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":350,"y":480,"wires":[]},{"id":"c2ec66ec.d866f8","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":150,"y":480,"wires":[]},{"id":"51951b02.b11884","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":520,"wires":[]},{"id":"2c8e1d40.ba0312","type":"change","z":"632239a.6b393c8","name":"tick","rules":[{"t":"set","p":"topic","pt":"msg","to":"tick","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":90,"y":300,"wires":[["518e31ca.83e31","3e26b6d9.0745fa"]]},{"id":"518e31ca.83e31","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":270,"y":300,"wires":[]},{"id":"75beb9c9.85eb08","type":"api-call-service","z":"632239a.6b393c8","name":"Red","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.hue_go_1","data":"{\"brightness_pct\":100,\"transition\":1.5,\"rgb_color\":[255,0,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":100,"wires":[["890d4f6e.f978e"]]},{"id":"ce47a283.6881a","type":"api-call-service","z":"632239a.6b393c8","name":"Pink","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.hue_go_1","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":220,"wires":[["73f60f5d.68238"]]},{"id":"6d4ba3ad.f87eac","type":"api-call-service","z":"632239a.6b393c8","name":"Green","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"light.hue_go_1","data":"{\"brightness_pct\":100,\"transition\":1.5,\"rgb_color\":[0,128,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":320,"wires":[["4dba1c1c.3a6794"]]},{"id":"2c859c.0bf55a64","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":490,"y":180,"wires":[]},{"id":"f89ae6fb.3fe008","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":490,"y":280,"wires":[]},{"id":"e7527bb2.fb0458","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":490,"y":60,"wires":[]},{"id":"73f60f5d.68238","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":220,"wires":[]},{"id":"4dba1c1c.3a6794","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":320,"wires":[]},{"id":"890d4f6e.f978e","type":"debug","z":"632239a.6b393c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":100,"wires":[]},{"id":"e31cfe3d.25302","type":"api-current-state","z":"632239a.6b393c8","name":"Wyatt's lamp on?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.hue_go_1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":770,"y":420,"wires":[["cda6bf69.0cef7"],[]]},{"id":"cda6bf69.0cef7","type":"api-call-service","z":"632239a.6b393c8","name":"Turn off","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_off","entityId":"light.hue_go_1","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":940,"y":420,"wires":[[]]},{"id":"26956a45.92d866","type":"server","z":"","name":"Hass.io","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Not really tested but might get you going in a good direction

[{"id":"c482d30c.c0925","type":"switch","z":"ce42ee66.56a91","name":"which button?","property":"payload.event.device_ieee","propertyType":"msg","rules":[{"t":"eq","v":"28:6d:97:00:01:08:59:e2","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":256,"y":720,"wires":[["cbe50838.1fe098"]]},{"id":"cbe50838.1fe098","type":"switch","z":"ce42ee66.56a91","name":"Wyatt's Bedroom Button","property":"payload.event.command","propertyType":"msg","rules":[{"t":"eq","v":"button_double","vt":"str"},{"t":"eq","v":"button_single","vt":"str"},{"t":"eq","v":"button_hold","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":466,"y":720,"wires":[["98be1f11.49106","78ecfc29.9401f4"],["a8cc4235.2dd4d","78ecfc29.9401f4"],["a8cc4235.2dd4d","1586fed1.f2c7e1"]]},{"id":"be507b62.b0ff38","type":"server-events","z":"ce42ee66.56a91","name":"zha_event","event_type":"zha_event","x":96,"y":720,"wires":[["c482d30c.c0925"]]},{"id":"98be1f11.49106","type":"api-current-state","z":"ce42ee66.56a91","name":"Wyatt's lamp on?","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.hue_go_1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":714,"y":688,"wires":[["fdee5089.753b5"],[]]},{"id":"fdee5089.753b5","type":"api-call-service","z":"ce42ee66.56a91","name":"Turn off","version":1,"service_domain":"light","service":"turn_off","entityId":"light.hue_go_1","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":892,"y":688,"wires":[[]]},{"id":"e2faa519.fb90a8","type":"api-call-service","z":"ce42ee66.56a91","name":"Cycle","version":1,"service_domain":"light","service":"turn_on","entityId":"light.hue_go_1","data":"{\t   \"brightness_pct\": 100,\t   \"transition\": 1.5,\t   \"rgb_color\": payload\t}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1106,"y":736,"wires":[[]]},{"id":"a8cc4235.2dd4d","type":"function","z":"ce42ee66.56a91","name":"","func":"// Get the saved index value or set it zero\nlet index = context.get(\"index\") || 0;\n\n// Array of colors to cycle through\nconst colors = [\n    [255,0,0], // Red\n    [255,105,180], // Pink\n    [0,128,0] // Green\n];\n\n// if the index value is highier than array length reset back to zero\nif(index >= colors.length) {\n    index = 0;\n}\n\n// send the color value as msg.payload\nmsg.payload = colors[index++];\n\n// save new index value\ncontext.set(\"index\", index);\n\nreturn msg;","outputs":1,"noerr":0,"x":978,"y":736,"wires":[["e2faa519.fb90a8"]]},{"id":"78ecfc29.9401f4","type":"change","z":"ce42ee66.56a91","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":784,"wires":[["1586fed1.f2c7e1"]]},{"id":"1586fed1.f2c7e1","type":"trigger","z":"ce42ee66.56a91","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"-3","extend":false,"units":"s","reset":"","bytopic":"all","name":"loop","x":834,"y":784,"wires":[["a8cc4235.2dd4d"]]}]
1 Like

Thank you!!

This, “rgb_color”: payload", was what I was trying to figure out, but didn’t know it, lol. Change the payload to the RBG color and template it. Thank you so much, this taught me a lot!

This is what I ended up with. I’m sure there’s a better way to do the time range nodes, which I’m going to look into, but I wanted to see if I could make it do what I wanted.

[{"id":"aaf375.d0a1ec88","type":"time-range-switch","z":"6b11d2aa.d35e7c","name":"9p to 5a","lat":"27.918805","lon":"-81.95932","startTime":"21:00","endTime":"05:00","startOffset":0,"endOffset":0,"x":900,"y":420,"wires":[[],["a7bafd15.fb573"]]}]

Basically, between 9p and 5a, he can only turn the light on to 10% red, and turn the light off. Between 5a and 9p, he can play with the colors.

Thanks again!!