Control home assistant light with zigbee2mqtt TRADFRI 2-button-switch

Use your IKEA TRADFRI 2-button-remote to control a Home Assistant light

Use node-red to control a home assistant light instance with the IKEA TRADFRI 2-button-remote. The flow sets 2 different timeframes (daytime and nighttime) to modify the default light setting. Daytime has a startup brightness at 100% and a normal light tone. Nighttime has startup brightness of 20% and a very warm light tone.

Set up the MQTT connection at the switch. Set up the light source at the call-service node. Modify the time frame and the light settings.

Want to buy me a coffee?

You can do that via Paypal

[{"id":"cfc108254b76f5fb","type":"mqtt in","z":"691da6d1926424ea","name":"Zigbee_Switch","topic":"zigbee2mqtt/Zigbee_Switch","qos":"2","datatype":"auto-detect","broker":"0efe62605df6aa09","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":340,"wires":[["861b39abb310a2e4"]]},{"id":"861b39abb310a2e4","type":"switch","z":"691da6d1926424ea","name":"Payload.Action","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"brightness_move_up","vt":"str"},{"t":"eq","v":"brightness_move_down","vt":"str"},{"t":"eq","v":"brightness_stop","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":460,"y":340,"wires":[["91fbb9f9996d04de"],["d3598c039d834b75"],["38545a5ccac297fd","da86cdae25ddca2c"],["38545a5ccac297fd","f449f9c10dc4cdc5"],["207e4730a5d5b870"]]},{"id":"c956d40b42f1c8df","type":"api-call-service","z":"691da6d1926424ea","name":"","server":"a86a4d9c.b5813","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.buro_tisch"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1670,"y":300,"wires":[[]]},{"id":"116502281ec89c64","type":"function","z":"691da6d1926424ea","name":"JSON_builder day","func":"var newMsg = {\n    payload: {\n        \"data\": {\n            \"color_temp_kelvin\": \"2732\",\n            \"brightness\": msg.payload\n        }\n    }\n};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":360,"wires":[["c956d40b42f1c8df"]]},{"id":"da86cdae25ddca2c","type":"function","z":"691da6d1926424ea","name":"Brightness up","func":"var step = 32;\nvar brightness =  flow.get(\"lamp_brightness\");\n\nif(brightness > 0){\n    brightness = brightness + step;\n    flow.set(\"lamp_brightness\", brightness);\n\t}\n\t\nif(brightness >= 255){\n    brightness = 254;\n    flow.set('press', false);\n    flow.set(\"lamp_brightness\", brightness);\n    }\n\nmsg.payload = brightness;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":500,"wires":[["1ca7be0b28502e9d","7b62c720850e715a","bc7d5055014b9fbf"]]},{"id":"f449f9c10dc4cdc5","type":"function","z":"691da6d1926424ea","name":"Brightness down","func":"var step = 32;\nvar brightness = flow.get(\"lamp_brightness\");\n\nif(brightness <= 255){\n    brightness = brightness - step; \n    flow.set(\"lamp_brightness\", brightness);\n\t}\n\t\nif(brightness <= 0){\n    brightness = 1;\n    flow.set('press', false);\n    flow.set(\"lamp_brightness\", brightness);\n    }\n\nmsg.payload = brightness;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":560,"wires":[["95c5d55f548914df","7b62c720850e715a","bc7d5055014b9fbf"]]},{"id":"3da94222ca7c0158","type":"switch","z":"691da6d1926424ea","name":"Control loop","property":"press","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1330,"y":560,"wires":[["f449f9c10dc4cdc5"]]},{"id":"ec97e2b824d4236f","type":"switch","z":"691da6d1926424ea","name":"Control loop","property":"press","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1330,"y":500,"wires":[["da86cdae25ddca2c"]]},{"id":"1ca7be0b28502e9d","type":"delay","z":"691da6d1926424ea","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1130,"y":500,"wires":[["ec97e2b824d4236f"]]},{"id":"95c5d55f548914df","type":"delay","z":"691da6d1926424ea","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1130,"y":560,"wires":[["3da94222ca7c0158"]]},{"id":"d34a3a2a55fb4fc8","type":"comment","z":"691da6d1926424ea","name":"loop every 500 ms","info":"","x":1150,"y":460,"wires":[]},{"id":"f20274718fe51b51","type":"comment","z":"691da6d1926424ea","name":"increase/decrease brightness","info":"","x":780,"y":460,"wires":[]},{"id":"38545a5ccac297fd","type":"change","z":"691da6d1926424ea","name":"start","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":340,"wires":[["a609aba2e449c191"]]},{"id":"207e4730a5d5b870","type":"change","z":"691da6d1926424ea","name":"stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":380,"wires":[["a609aba2e449c191"]]},{"id":"a609aba2e449c191","type":"change","z":"691da6d1926424ea","name":"Loop_Control","rules":[{"t":"set","p":"press","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":360,"wires":[[]]},{"id":"977878590aa9827e","type":"comment","z":"691da6d1926424ea","name":"start/stop loop","info":"","x":740,"y":300,"wires":[]},{"id":"91fbb9f9996d04de","type":"time-range-switch","z":"691da6d1926424ea","name":"","lat":"","lon":"","startTime":"20:00","endTime":"sunrise","startOffset":0,"endOffset":0,"x":740,"y":120,"wires":[["768f3b1547335778"],["c920384e624ee83d"]]},{"id":"768f3b1547335778","type":"change","z":"691da6d1926424ea","name":"Set brightness to 20%","rules":[{"t":"set","p":"payload","pt":"msg","to":"51","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":100,"wires":[["695300bcf923e010","7b62c720850e715a"]]},{"id":"c920384e624ee83d","type":"change","z":"691da6d1926424ea","name":"Set brightness to 100%","rules":[{"t":"set","p":"payload","pt":"msg","to":"255","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":140,"wires":[["695300bcf923e010","7b62c720850e715a"]]},{"id":"d3598c039d834b75","type":"change","z":"691da6d1926424ea","name":"Set brightness to 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":240,"wires":[["7b62c720850e715a"]]},{"id":"695300bcf923e010","type":"change","z":"691da6d1926424ea","name":"Save starting brightness","rules":[{"t":"set","p":"lamp_brightness","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1310,"y":120,"wires":[["fa3b300b93ed4c17"]]},{"id":"cb79985e7484815e","type":"comment","z":"691da6d1926424ea","name":"Set brightness based on time","info":"","x":1020,"y":60,"wires":[]},{"id":"7a12ad8e7df443d4","type":"comment","z":"691da6d1926424ea","name":"Turn off light","info":"","x":730,"y":200,"wires":[]},{"id":"47826767a8662de7","type":"comment","z":"691da6d1926424ea","name":"Turn on light","info":"","x":730,"y":80,"wires":[]},{"id":"2527293774ade5e8","type":"function","z":"691da6d1926424ea","name":"JSON_builder night","func":"var newMsg = {\n    payload: {\n        \"data\": {\n            \"xy_color\": [0.561, 0.4042],\n            \"brightness\": msg.payload\n        }\n    }\n};\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":240,"wires":[["c956d40b42f1c8df"]]},{"id":"7b62c720850e715a","type":"time-range-switch","z":"691da6d1926424ea","name":"","lat":"","lon":"","startTime":"20:00","endTime":"sunrise","startOffset":0,"endOffset":0,"x":1200,"y":300,"wires":[["2527293774ade5e8"],["116502281ec89c64"]]},{"id":"3aaff59fb25e5878","type":"comment","z":"691da6d1926424ea","name":"TRADFRI 2 button remote","info":"","x":210,"y":280,"wires":[]},{"id":"76173bf8bb97396a","type":"comment","z":"691da6d1926424ea","name":"Set light(s) you want to control","info":"","x":1720,"y":260,"wires":[]},{"id":"fa3b300b93ed4c17","type":"debug","z":"691da6d1926424ea","name":"msg.payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1550,"y":120,"wires":[]},{"id":"bc7d5055014b9fbf","type":"debug","z":"691da6d1926424ea","name":"msg.payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1130,"y":620,"wires":[]},{"id":"0efe62605df6aa09","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"a86a4d9c.b5813","type":"server","name":"Home Assistant","addon":true}]

Flow Info

Created 2 years, 5 months ago
Rating: 4 1

Owner

Actions

Rate:

Node Types

Core
  • change (x7)
  • comment (x8)
  • debug (x2)
  • delay (x2)
  • function (x4)
  • mqtt in (x1)
  • mqtt-broker (x1)
  • switch (x3)
Other

Tags

  • IKEA
  • TRADFRI
  • HomeAssistant
  • Zigbee
  • 2-button-remote
  • zigbee2mqtt
  • mqtt
  • dimmer
  • remote
  • light
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option