TP-LINK LightBulb

I just received my first bulb, and couldn't find a node for it. So i used the information i found in the excellent node.js library https://www.npmjs.com/package/tplink-lightbulb to write a simple function for it.

This version also returns the status message from the bulb after setting it.

[{"id":"d8a872e.180499","type":"subflow","name":"TP-LINK LightBulb","info":"","in":[{"x":72.00000286102295,"y":157.9999942779541,"wires":[{"id":"8ba15332.95324"}]}],"out":[{"x":645.9999885559082,"y":314.9999942779541,"wires":[{"id":"b04624ef.e4a878","port":0}]}]},{"id":"b886f8.3a3d7908","type":"udp out","z":"d8a872e.180499","name":"","addr":"","iface":"","port":"9999","ipv":"udp4","outport":"61234","base64":false,"multicast":"false","x":933.9828262329102,"y":160.01040077209473,"wires":[]},{"id":"fd9121e3.d5f1f","type":"function","z":"d8a872e.180499","name":"TP-Link LightBulb","func":"// msg.brightness : 0 = uit,  >0 & <=100 = aan\n// msg.on = 1 of 0\n// msg.transitionperiod = xxxx ms  (LB100 enkel voor ON ?)\n// msg.ip = ip address of bulb\n\n// defaults\nvar brightness = msg.brightness ? msg.brightness : 100;\nvar on = msg.on;\nvar transitionperiod = msg.transitionperiod ? msg.transitionperiod : 1;\n\nif( on === 0 ){\n    msg.brightness = 0;\n}\nif( brightness > 100 )\n{\n    brightness = 100;\n}\n\nvar json = {\n      'smartlife.iot.smartbulb.lightingservice': {\n        'transition_light_state': {\n          'ignore_default': 1,\n          'on_off' : on ,\n          'transition_period': transitionperiod,\n          'brightness': brightness\n        }\n      }\n};\n  \nvar buffer = new Buffer(JSON.stringify(json));\n  \n// encrypt    \nvar key = 0xAB;\nfor (let i = 0; i < buffer.length; i++) {\n    const c = buffer[i];\n    buffer[i] = c ^ key;\n    key = buffer[i];\n}\n\nmsg.payload = buffer;\n\nreturn msg;","outputs":1,"noerr":0,"x":752.9827041625977,"y":159.01040935516357,"wires":[["b886f8.3a3d7908"]]},{"id":"262ebfc9.9f30b","type":"udp in","z":"d8a872e.180499","name":"","iface":"","port":"61234","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":236.9827423095703,"y":307.0103645324707,"wires":[["b04624ef.e4a878"]]},{"id":"b04624ef.e4a878","type":"function","z":"d8a872e.180499","name":"Answer","func":"var buffer = msg.payload;    \nvar key = 0xAB;   \n    \n    for (let i = 0; i < buffer.length; i++) {\n      const c = buffer[i]\n      buffer[i] = c ^ key\n      key = c\n    }\n    \nvar json = buffer.toString();    \nvar answer = JSON.parse(json);\n\nmsg.payload = answer[\"smartlife.iot.smartbulb.lightingservice\"][\"transition_light_state\"];\n    \nmsg.on = msg.payload[\"on_off\"];\nmsg.err_code = msg.payload[\"err_code\"];\n\nmsg.brightness = msg.payload[\"brightness\"];\nmsg.saturation = msg.payload[\"saturation\"];\nmsg.color_temp = msg.payload[\"color_temp\"];\nmsg.hue = msg.payload[\"hue\"];\n\n\nreturn msg;\n\n\n\n//{\"smartlife.iot.smartbulb.lightingservice\":{\"transition_light_state\":{\"on_off\":1,\"mode\":\"normal\",\"hue\":0,\"saturation\":0,\"color_temp\":2700,\"brightness\":50,\"err_code\":0}}}\n","outputs":1,"noerr":0,"x":405.98272705078125,"y":307.01038551330566,"wires":[[]]},{"id":"b1812847.000c18","type":"function","z":"d8a872e.180499","name":"Simple triggered queue","func":"// if queue doesn't exist, create it\ncontext.queue = context.queue || [];\ncontext.busy = context.busy || false;\n//node.warn(context.busy);\n// if the msg is a trigger one release next message\nif (msg.hasOwnProperty(\"trigger\")) {\n    if (context.queue.length > 0) {\n        var m = context.queue.shift();\n        return {payload:m};\n    }\n    else {\n        context.busy = false;\n    }\n}\nelse {\n    if (context.busy) {\n        // if busy add to queue\n        context.queue.push(msg.payload);\n    }\n    else {\n        // otherwise we are empty so just pass through and set busy flag\n        context.busy = true;\n        return msg;\n    }\n}\n\nreturn null;","outputs":1,"noerr":0,"x":345.99998474121094,"y":242.13190460205078,"wires":[["8ba15332.95324"]]},{"id":"a78c5534.db8158","type":"function","z":"d8a872e.180499","name":"set trigger  voor Queue","func":"// handle the return from the exec in here \n// if all is good then set msg.trigger property to exist\nmsg.trigger = 1;\nreturn msg;","outputs":1,"noerr":0,"x":451.9999237060547,"y":160.13190460205078,"wires":[["b1812847.000c18","fd9121e3.d5f1f"]]},{"id":"8ba15332.95324","type":"delay","z":"d8a872e.180499","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":258.99998474121094,"y":159.13190460205078,"wires":[["a78c5534.db8158"]]},{"id":"12d51835.d81308","type":"comment","z":"d8a872e.180499","name":"Communication with lightbulb is via on port 9999 and incomming port 61234","info":"","x":492.05213928222656,"y":107.5068941116333,"wires":[]},{"id":"4aaaaf17.45e4f","type":"inject","z":"16d1a4b6.8ed06b","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":144.00004196166992,"y":424.0000820159912,"wires":[["d09fa387.bf8e8"]]},{"id":"342a79b6.f7ba76","type":"inject","z":"16d1a4b6.8ed06b","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":146.00003814697266,"y":489.0000648498535,"wires":[["d09fa387.bf8e8"]]},{"id":"d09fa387.bf8e8","type":"function","z":"16d1a4b6.8ed06b","name":"Set   Lamp On/Off","func":"\nif( msg.payload === 1 )\n{\n    msg.on = 1;\n\n}\nelse\n{\n    msg.on = 0;\n   \n}\n\nmsg.brightness = 50;\nmsg.transitionperiod = 0;\nmsg.ip = '10.0.250.38';\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":325.00012588500977,"y":454.0000638961792,"wires":[["b6ddbc06.a04ce"]]},{"id":"8122c5bd.4fb018","type":"debug","z":"16d1a4b6.8ed06b","name":"","active":true,"console":"false","complete":"true","x":708,"y":458.00008392333984,"wires":[]},{"id":"b6ddbc06.a04ce","type":"subflow:d8a872e.180499","z":"16d1a4b6.8ed06b","name":"","x":531.0833053588867,"y":457.97223472595215,"wires":[["8122c5bd.4fb018"]]}]
lucwuyts

Flow Info

created 6 days ago

Node Types

Core
  • comment (x1)
  • debug (x1)
  • delay (x1)
  • function (x5)
  • inject (x2)
  • udp in (x1)
  • udp out (x1)
Other
  • subflow (x1)
  • subflow:d8a872e.180499 (x1)

Tags

  • tplink
  • tp-link
  • lb100
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option