Examples using TP-Link IoT Smart Home devices

I am wondering if anyone using one of the Nodes for controlling the TP-Link Smart Home devices such as the HS100 or HS110 could provide some example flows or codes to help head me in the right direction.

I have managed to do some of the basic things, but as soon as it becomes more complicated I start to have problems with my code, such as the switch not turning off or on when I expected it to.

I have tried the more advanced node-red-contrib-tplink-iot, but have problems with the formatting the messages (such as is the switch on or off) to update the dashboard switch or even use in functions.

perhaps if you could post an example of the message that doesn’t work we can see where you are going wrong?

From the ReadMe you linked to it would look as simple as making msg.payload true or false
The easiest way to do this is with the change node where you can select the type to be boolean (where it usually displays a/z) and then select try or false.

I have gotten a HS105 to work using both an exec node to the python code and using node-red-contrib-tplink-iot
here is a flow using the node-red-contrib-tplink-iot:

[{"id":"7b3f85e1.b50214","type":"tab","label":"tplink using NR node","disabled":false,"info":""},{"id":"e35ac893.1af7d8","type":"smart-plug","z":"7b3f85e1.b50214","name":"HS105 - tplink117","device":"192.168.48.117","interval":10000,"eventInterval":1000,"x":420,"y":180,"wires":[["cc1a2883.cfbc2"]]},{"id":"50813b84.4ac6ac","type":"inject","z":"7b3f85e1.b50214","name":"","topic":"","payload":"getCloudInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":120,"wires":[["e35ac893.1af7d8"]]},{"id":"cc1a2883.cfbc2","type":"debug","z":"7b3f85e1.b50214","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":660,"y":180,"wires":[]},{"id":"540d4425.12c3a4","type":"inject","z":"7b3f85e1.b50214","name":"","topic":"","payload":"getInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":80,"wires":[["e35ac893.1af7d8","93c6b97a.b8d128"]]},{"id":"4db59cca.8aa1b4","type":"inject","z":"7b3f85e1.b50214","name":"ON","topic":"","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":220,"wires":[["e35ac893.1af7d8"]]},{"id":"908eaba1.53a9c","type":"inject","z":"7b3f85e1.b50214","name":"OFF","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":280,"wires":[["e35ac893.1af7d8"]]},{"id":"a6950264.193128","type":"catch","z":"7b3f85e1.b50214","name":"","scope":null,"x":340,"y":60,"wires":[["6288530b.7973c4"]]},{"id":"6288530b.7973c4","type":"debug","z":"7b3f85e1.b50214","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":560,"y":80,"wires":[]},{"id":"80354a5d.cba968","type":"inject","z":"7b3f85e1.b50214","name":"","topic":"","payload":"getQuickInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":160,"wires":[["e35ac893.1af7d8"]]},{"id":"93c6b97a.b8d128","type":"smart-plug","z":"7b3f85e1.b50214","name":"HS105 - tplink119","device":"192.168.48.119","interval":10000,"eventInterval":1000,"x":450,"y":340,"wires":[["a08efec1.e23978"]]},{"id":"a08efec1.e23978","type":"debug","z":"7b3f85e1.b50214","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":670,"y":340,"wires":[]},{"id":"333aeeb2.24ee02","type":"inject","z":"7b3f85e1.b50214","name":"OFF","topic":"","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":460,"wires":[["93c6b97a.b8d128"]]},{"id":"73ff277e.55cd4","type":"inject","z":"7b3f85e1.b50214","name":"ON","topic":"","payload":"true","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":420,"wires":[["93c6b97a.b8d128"]]},{"id":"a87952da.578b4","type":"inject","z":"7b3f85e1.b50214","name":"SWITCH","topic":"","payload":"switch","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":320,"wires":[["e35ac893.1af7d8"]]}]

If you look at the inject nodes you will see the formats.

Also I originally had a problem with on/off because the node wanted boolean true/false. I sent a PR to allow text on/off, text true/false or boolean true/false. that should be in the latest version.

Paul

Thank you Paul for this example.

Based on your code I changed one of the HS105 switches to the settings for my HS110. It came up showing it was connected.

I then created a Inject for the getPowerUpdateEvents with only an initial injection and this was able to send the messages through to the debug as I changed the power on/off with the TP Link Kasa App. This is one of the things I have been looking to do. Where I then have a problem is with handling the message information.

{"powerOn":false,"timestamp":"2018-08-22T07:51:22+10:00"}

All I want is the status of the "powerOn" and not the time stamp information. I gather I can use something like the "split" or "switch" functions, but I wasn't able to get this to work. Any suggestions?

Also do you use a "Dashboard" switch to control your switches?

As long as that is an object and not a string (the debug will tell you) then if you only want the "false" part of that then a change node set to

move  msg.payload.powerOn
  to  msg.payload

should do it.

I haven't yet, I only got the switches two weeks ago and was away last week and this week am being invaded by grandkids - my daughter & son-in-law are selling there house and since it is so easy to come here and keep the kids from messing up their house. So i don't know how much time I will have to play with this.

It just took two seconds to add two buttons with the corect values and it turns off and on fine.

You sound like you more than have your handsful.

Thank you for taking the time to provide what you have done so far.

Wow! Simply when you know what you are doing. I spent more than an hour trying to work this out and I clearly wasn't even close.

Thanks for this great tip.

Thank you again for all your help. I have made some progress.

I now have it so that it waits 0.5 seconds for the HS110 to come online, then injects a "false" to the HS100 to set the switch to off. The message payload from the "powerOn Filter" is fed back to the Control Panel switch, so that position of the switch match what the mode the HS110 is in. The TP-Link Kasa app can be used as well and that keeps in sync with the position shown on the control panel switch.

The only issue I am having is an additional message is being created from the powerOn Filter.

node: PowerOn Messagesmsg.payload : undefined
undefined

Code

[{"id":"2550a1c4.f1502e","type":"tab","label":"TP-Link Smart Switch with Control Panel Switch","disabled":false,"info":""},{"id":"649540a3.def8b","type":"smart-plug","z":"2550a1c4.f1502e","name":"HS110","device":"192.168.1.41","interval":10000,"eventInterval":1000,"x":650,"y":180,"wires":[["b9ef143e.abace8","5bcc6d3c.c5fe24"]]},{"id":"b9ef143e.abace8","type":"debug","z":"2550a1c4.f1502e","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":180,"wires":},{"id":"987d5ad4.ea5328","type":"inject","z":"2550a1c4.f1502e","name":"ON","topic":"","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":160,"wires":[["649540a3.def8b"]]},{"id":"8fc34e31.d6b17","type":"inject","z":"2550a1c4.f1502e","name":"OFF","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":220,"wires":[["649540a3.def8b"]]},{"id":"1b5dedc4.f3c9d2","type":"catch","z":"2550a1c4.f1502e","name":"","scope":null,"x":610,"y":60,"wires":[["9014dc0d.7637c"]]},{"id":"9014dc0d.7637c","type":"debug","z":"2550a1c4.f1502e","name":"Error Messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":860,"y":80,"wires":},{"id":"124b1396.b570bc","type":"inject","z":"2550a1c4.f1502e","name":"SWITCH","topic":"","payload":"switch","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":260,"wires":[["649540a3.def8b"]]},{"id":"6ca45d5d.eccf74","type":"inject","z":"2550a1c4.f1502e","name":"getPowerUpdateEvents","topic":"","payload":"getPowerUpdateEvents","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":390,"y":460,"wires":[["649540a3.def8b"]]},{"id":"5bcc6d3c.c5fe24","type":"change","z":"2550a1c4.f1502e","name":"powerOn Filter","rules":[{"t":"move","p":"payload.powerOn","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":300,"wires":[["725ce1bc.3124b","62aa304d.d2032"]]},{"id":"725ce1bc.3124b","type":"debug","z":"2550a1c4.f1502e","name":"PowerOn Messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1170,"y":300,"wires":},{"id":"62aa304d.d2032","type":"ui_switch","z":"2550a1c4.f1502e","name":"Control Panel","label":"switch","group":"ce4b59f5.7b5e88","order":0,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"powerOn","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":360,"y":360,"wires":[["649540a3.def8b"]]},{"id":"40204818.11ddc8","type":"inject","z":"2550a1c4.f1502e","name":"","topic":"","payload":"getCloudInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":390,"y":60,"wires":[["649540a3.def8b"]]},{"id":"bc74b481.ac48c8","type":"inject","z":"2550a1c4.f1502e","name":"","topic":"","payload":"getInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":20,"wires":[["649540a3.def8b"]]},{"id":"1ca17ca8.b22613","type":"inject","z":"2550a1c4.f1502e","name":"","topic":"","payload":"getQuickInfo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":390,"y":100,"wires":[["649540a3.def8b"]]},{"id":"916ba9c4.54f8c8","type":"inject","z":"2550a1c4.f1502e","name":"Initial Switch Position","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":340,"y":300,"wires":[["649540a3.def8b"]]},{"id":"ce4b59f5.7b5e88","type":"ui_group","z":"","name":"Smart Plug Test","tab":"85006852.b94c58","disp":true,"width":"6","collapse":false},{"id":"85006852.b94c58","type":"ui_tab","z":"","name":"Smart Plug Test","icon":"dashboard","order":3}]

I only see a hs110 in the flow - where do you use the HS100?

Sorry, there is only one HS110.

  1. Turn on the debug node connected to the output of the tplink node.
  2. run it
  3. in the debug tab, open the payload to display everything
  4. copy and paste into a reply what it shows

The HS105 does not return a field 'powerOn' but does have a 'relay_state' field - you are using 'powerOn' in the change node. This might just be a difference in devices, but I want to check it out.

Ahh I thik I know what is happening.

When you send the 'getPowerUpdateEvents' you are subscribing to power events (on/off). Thus when a power change happens, a msg is sent to the tplink node BUT the msg.payload only contains 'powerOn' and 'timestamp'. Your code is looking for something else in msg.payload that isn't there.

You need to add a switch node to the output of the tplink node to check what is being returned and then decide what to do in the varios cases.