[RELEASE] TP-Link Plug, Switch, and Bulb integration


I am using Scenes to call (1) lighting efffect for all the bulbs in the room. Let me try controlling independently to see if there are errors.

[UPDATE] same issues controlling independently too.


[dev:273]( 10:07:58.363 am [info]( - Right: Power: off


[dev:273]( 10:07:56.125 am [info]( - Right: Power: on / Brightness: 12% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:273]( 10:07:56.121 am [info]( - Right Color Mode is CT. Color is Moonlight.


The error in the message was my logging (to highlight within the great amount of logging). That was expected. It is the "raw" data that is received from the device. I was looking for another groovy.json.jsonexception with this data to confirm the truncation on receipt from the bulb.

I WILL rework the parsing of bulb responses - going back to an older method with more logic, but less messages. I was thinking about doing this anyway.



New errors in TP130 colored bulbs:

dev:2712019-01-05 08:58:29.110 am errorgroovy.json.JsonException: Expected a closing curly brace '}' or a comma ',' on line: 1, column: 12. But got an unterminated object. on line 312 (refreshResponse)



Just so you know even though there are errors in the logs everything works really well. Significantly reduced popcorn effect, almost instant on/off, etc. I am just helping out with errors I am seeing with the LB130 bulbs. The 230's have no errors.


I really need to know what is going on when you get the error. Essentially, I must have the decrypted raw return from the bulb when the error occurs.


a. Insert the code below after line 298 in the new device handler

 log.debug "commandResponse:  ${response}"

b. Insert the code below after line 309 in the new device handler.

 log.debug "refreshResponse:  ${response}"

b. Insert the code below after line 402 in the new device handler.

      log.debug cmdResponse

c. Assure that the DH's traceLog is false (that is default).

d. Open hubitat logging until you get the error. (keep traceLog false).

e. paste the error message (groovy.jsonJsonException) and the preceding message.

This should give me a view of the received data. If I can find this out, I can possibly fix it. Otherwise, I will mask out the error.

I noted that you are doing a lot of refresh commands. How often? Use case?

REFRESH. Note that for these bulbs, refresh is only useful in four cases:

a. Someone manually turns off the bulb (at plug, switch, or unscrew). Refresh will detect this and create an error saying the bulb is not connected at the next scheduled refresh.

b. Someone uses the Kasa App to change the bulb state. Refresh will update on the next cycle.

c. The bulb is turned on using google assistant or Amazon Alexa with the device using the Kasa Skill (amazon) or equivalent. Again updated on next refresh cycle. However, if you use the Hubitat Alexa Skill (or google if it exists), you should disable the Kasa Skill and use the Hubitat Skill. Then the state updates immediately since it is updated by HE.

d. (Very rare.) There is a glitch in the return message (spurious). Then next refresh cycle will update unless comms are in a very big problem.

Sorry for the verbose discourse. I see a lot of over-use of refresh. It stresses the bulbs (more processing) and could stress the HE.



Great feedback! I have it set to 1 minute refreshes. I don’t use any of the scenarios listed below and the bulbs are 99% controlled by HE; last 1% is using Kasa when a horrific issue happens with HE. Suggestions on refresh time with this in mind? Maybe I am over taxing everything.


Every minute should be OK unless you have a lot of Hubitat devices and should not stress the TP-Link devices.

I am modifying code today to "handle" your error as best as possible. The modification will include a 30 minute refresh option (which is what I prefer).


Here is the logs from the modified code:

dev:2712019-01-05 11:02:29.156 am errorgroovy.json.JsonException: Expected a closing curly brace '}' or a comma ',' on line: 1, column: 12. But got an unterminated object. on line 314 (refreshResponse)

dev:2712019-01-05 11:02:29.146 am debug{"system":{"get_sysinfo":{"sw_ver":"1.8.6 Build 180809 Rel.091659","hw_ver":"1.0","model":"LB130(US)","description":"Smart Wi-Fi LED Bulb with Color Changing","alias":"Living Room - Couch","mic_type":"IOT.SMARTBULB","dev_state":"normal","mic_mac":"50C7BFA0EC9F","deviceId":"80123D7A0D639BA4AE81BB59672E0AB818999DFB","oemId":"05BF7B3BE1675C5A6867B7A7E4C9F6F7","hwId":"111E35908497A05512E259BB76801E10","is_factory":false,"disco_ver":"1.0","ctrl_protocols":{"name":"Linkie","version":"1.0"},"light_state":{"on_off":0,"dft_on_state":{"mode":"normal","hue":0,"saturation":0,"color_temp":4000,"brightness":100}},"is_dimmable":1,"is_color":1,"is_variable_color_temp":1,"preferred_state":[{"index":0,"hue":0,"saturation":0,"color_temp":2700,"brightness":50},{"index":1,"hue":0,"saturation":75,"color_temp":0,"brightness":100},{"index":2,"hue":120,"saturation":75,"color_temp":0,"brightness":100},{"index":3,"hue":240,"saturation":75,"color_temp":0,"brightness":100}],"rssi":-41,"active_mode":"none","heapsize":290824,"err_code":0}}



Driver 4.0.03 update. All drivers for TP-Link devices have been updated to handle the error described by @aaron (summarized at bottom). The update includes:

a. Change Bulb Command Response processing to eliminate unnecessary 'refresh' command.
b. Added a 'Preference' for traceLog (default to false) to allow enhanced logging for problem resolution.
c. Added error processing to capture the error being corrected and suggest the fix.
d. Added 30 minutes as a 'refresh' option. (Note, unless you have some other use case, 30 minute refresh is recommended.)

Error: a groovy.json.JsonException was being received in refreshResponse.

Cause: the return message from the device was being fragmented due to (1) length, and (2) something in the TP-Link code doing this when the device is very busy. Caused by sending an on->off or off->on command to the device followed immediately by the refresh command. Occurred when the message exceeded a certain number of characters. This was keyed when the bulbs name (set by Kasa App) exceeds 18 characters.

Program Fix: Fixed in code for bulbs by removing automatic 'refresh' after a command is sent. Use the command return instead to update the state.

Known Residual Issue: If your bulb name is over 18 characters and you (or an automation) send the on->off of off->on command followed immediately by a refresh command, the error will occur.

Work-Around 1: Do not send the on->off of off->on command followed immediately by a refresh command. It is unnecessary.

Work-Around 2: change the name to less than 18 characters.


Finally able to replicate. See Driver 4.0.03 update.


I set all my devices to 30 minute refresh except one plug device that controls more automation sequences than it probably should. That is set to 5 minutes.

I now have this error with the TP-Link Switch though:

dev:3392019-01-05 01:05:53.070 pm errorjava.lang.NullPointerException: Cannot get property 'system' on null object on line 216 (refreshResponse)


Fixed. My error on copying the file!!!!! OUCH. Must be getting old (actually, I was in a hurry to fix same issue on SmartThings).


Updated device code for all devices. Color bulbs no longer have errors except for my long name.


Thanks for the assist in troubleshooting the problem. (BTW, your device is 1 character too long.) I would never have found without the assist.


I just installed UDP app/driver from GitHub for my HS110 plugs realising the driver doesn't show energy usage and this is the main reason I use the plugs for (OLED TV and room A/C). So if you still need a tester here I am.


Will have a test sometime this weekend. One known issue is the weekly and monthly stat coming back from bulb in two messages (intermittent issue, usually toward end of month). I will try to mitigate - but may still occur sometimes. If these are not important to you, then it will be much easier.



Updating the existing SmartThings code for this. Will probably be sometime next week (I will announce here). What I am creating:

Current power in Watts
Usage in kWh (also as states for readability in the device detail page.
a. Use by day this month as a JSON object (if feasible)
b. Use by month this year as a Json object (if feasible)
c. Use by month last year as a Json object (if feasible)

All will be as attributes. The Json objects can be accessed in a application to create reports, as desired.


That's great work of yours. Thanks in advance and I'm here if you need me. Just bear in mind I'm in Central Europe UTC+1 - 20.50 hrs here now.


HS110 Energy Monitor Test Driver. I have created a test driver for the HS110 Energy Monitor Plug that provides energy monitor data. File is in the devices folder: "test - TP-Link Engr Meter Plug (Hubitat).groovy".

Uses capability "power" and "energy" and adds attributes "power" (current power in watts) and "energy" (consumption today in kilo watt hours). Also adds states
a, "energyThisMonth" - daily consumption for this month.
b. "energyThisYear" - monthly consumption for this year.
c. "energyLastYear" - monthly consumption last year.
Also added three exposed functions to pull the data from the three states into an application: "sendThisMonth", "sendThisYear", and "sendLastYear". The HS110 can be selected on the input of the application using either capability power or energy.

I need comments back.

Update. You can update an already installed HS110 as follows:
a. On the HE device's page, update the "type" in the Device Information section and "Save Device"
b. Select "Display trace messages", then "Save Preferences".

You can also do a clean install manually.

Example of monthly state (energy_wh will be energy for HS110): energyThisMonth : [{month=1, year=2019, energy_wh=103, day=1}, {month=1, year=2019, energy_wh=133, day=2}, {month=1, year=2019, energy_wh=50, day=3}, {month=1, year=2019, energy_wh=39, day=4}, {month=1, year=2019, energy_wh=80, day=5}, {month=1, year=2019, energy_wh=84, day=6}, {month=1, year=2019, energy_wh=120, day=7}, {month=1, year=2019, energy_wh=50, day=8}, {month=1, year=2019, energy_wh=76, day=9}]

Example of yearly state (2019) ("energy_wh" will be "energy" for HS110): energyThisYear : [{month=1, year=2019, energy_wh=1088}]

Once functionality is verified, I will update the Application to install this driver for the HS110 plug.


My HS110 plugs are arriving today so I should get a change to help with testing.