[DEPRECATED] Kasa Plug, Switch, and Bulb integration

Ahh! I found the problem, when the device is not in the local subnet, resp.mac will be null, and this is your first check in the response when discovering the device and then you use it for the device DNI.

I think maybe checking for IP is null instead and then getting the MAC from cmdResp.mac for the DNI would solve this, plus it is in human readable format instead of hex (easier to match a device from the devices page)... do you know if you can use ":" in a DNI? maybe replace them with "-"...

I'll give this a try and send you my changes if I can make it all work...

BTW, thanks for those tips!

I updated the code as follows:

a. Stop polling at device 254, avoiding a duplicate response from the global address.

b. Do not use the udp returned mac for a filter if null.

c. Parse the get_sysinfo return mac or mic_mac field (different based on device type) to generate the dni in format of MAC less colons.

I did no change the polling range. Could not figure out a way to do that w/o user interface. Would require a significant design change. You will have to hard-code that area.

Dave

1 Like

This is Awesome Dave! got home from work ready try my coding skills but you were faster! :grinning:

Discovery worked with the new code and I was able to add the devices and they work perfectly!

I don't have any problem with hardcoding my subnet. Thanks so much for this integration!

@djgutheinz

Errors in logs:

Tried each of my bulbs, no problem (bulbs are where line 274 is in refreshResponse). Exactly what bulb was it? Is it repeatable?

FYI - the code is JSON parsing the return from decrypt. It indicates a truncated string was received (therefore only 12 characters long). Looks like a spurious comms glitch - but....

Turn on 'traceLog' (line 30-31 of the driver) and capture one cycle. If it continues, I will have to create a test driver with even more data.

Dave

These are the 230 color bulbs. Will turn on trace logging and get back to you. And this happens on both 230 and 130 bulbs.

After line 270, insert the below code. It will tell me what the "raw" data return is.

log.error parseLanMessage(response)

Also, what version are you using? (line 33 of driver)

I have run 100 cycles w/o an error. Still confused.

Version: 4.0.01

Added the log.error info...information below

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:53:29.191 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch: Power: on / Brightness: 100% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:53:29.189 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch Color Mode is CT. Color is Moonlight.

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:53:29.099 pm [error](http://10.0.2.38/device/edit/271)[index:00, mac:50C7BFA0EC9F, ip:0a000297, port:270f, type:LAN_TYPE_UDPCLIENT, payload:D0F281F88BFF9AF7D5EF94B6D1B4C09FEC95E68FE187E8CAF08BA9DAADF284E193B18BA998B68EA096B6F481E884E0C0F1C9F9C1F1C8E8BADFB39DAD94A593A69FBD91B3DBACF385E092B08AA899B787A589ABC6A9CDA8C4E6DCFEB2F0C1F2C2EABFECC5E7CBE98DE89BF88AE393E78EE18FAD97B5E68BEA98ECCC9BF2DF99F0D09CD99DBDFF8AE684A4D3BACEA686C5AAC6A9DBFBB8D0B1DFB8D1BFD8FAD6F495F990F182A09AB8F49DEB82EC8BABF996F994B499B9FA95E083EBC9E5C7AAC3A0FF8BF282E7C5FFDD94DB8FA1F2BFFEACF8BAEFA3E1C3EFCDA9CCBAE596E283F792B08AA8C6A9DBB6D7BB99B597FA93F0AFC2A3C0E2D8FACFFFBC8BC98FCEFEBBF8C187A589ABCFAADCB5D6B3FA9EBC86A49CAC9DAF9CD8EFAE9EDAECDFE6A4E5D190D5EDDC9EDCE9D0E6D1E3A696D795AD9CA49DA49DD99FDDFFD3F19EFB96DFBB99A381B184C680B7F5C684C1F0C6F1C487B2F3C5FDCBFCBE89C8FFBA8ECDF4B284C2F5D7FBD9B1C68FEBC9F3D1E0D1E0A596A39AAA92A69FA8E9D9ECD9E8DA9FAD98A1E3A196A098A899DCEDDDFFD3F198EBB4D2B3D0A4CBB9C0E2D8BEDFB3C0A589ABCFA6D5B6D986F095E7C5FFDDECC2F2D0FCDEBDC9BBD788F88AE591FE9DF29EEDCFF58EACC2A3CEAB89B391DDB4DAB1D8BD9FB391E782F083EA85EBC9F3D1E0CEFEDCA18DAFC3AACDA5D18EFD89E89CF9DBE19AB8D7B9E689EF89AB91A08CAEC3ACC8AD8FB597F996E489E884A68AA8C0B5D0F2C8F8D4F685E490E597F682EB84EAC8F2C2EECCAFC0ACC3B1EE9AFF92E2C0FACEFECEFED2F092E089EE86F29CF98AF9DBE1D0E0D0AD81A3CAB9E682EB86EB8AE884E1C3F9C8E4C6AFDC83E08FE38CFEDCE6D7FBD9B0C39CEA8BF990F193FF9AC5A6C9A5CAB8E793F69BEBC9F3C2EECCBCCEABCDA8DAA8CDA9F685F190E481A399C2B99BF29CF89DE5C7FDCDE1C3ABDEBB99A393BF9DEE8FFB8EFC9DE980EF81A399A985A7C4ABC7A8DA85F194F989AB91A394A494B89AF88AE384EC98F693E093B18BBE8EF3DFA486EF81E580F8DAE0D1FDDFB7C2A785BF8FA381F293E792E081F59CF39DBF85B287AB89EA85E986F4ABDFBAD7A785BF8FA381E391F89FF783ED88FB88AA90A191A1DCF08BA9C0AECAAFD7F5CFFDD1F39BEE8BA993A290A08CAEDDBCC8BDCFAEDAB3DCB290AA9DA884A6C5AAC6A9DB84F095F888AA90A08CAECCBED7B0D8ACC2A7D4A785BF8EBE8EF3DFA486EF81E580F8DAE0D3FFDDB5C0A587BD8FBB8BA785F697E396E485F198F799BB81B683AF8DEE81ED82F0AFDBBED3A381BB8BA785E795FC9BF387E98CFF8CAE94A595A5D885A98BF98AF990B288A591A884A6C7A4D0B9CFAAF598F793F6D4EECCA2CDA3C6E4C8EA82E786F685EC96F3D1EBD9E0D1E6D6E6CAE88DFF8DD2B1DEBADFFDC7F78AF78A]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:52:29.081 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch: Power: on / Brightness: 100% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:52:29.081 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch Color Mode is CT. Color is Moonlight.

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:29.069 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch: Power: on / Brightness: 100% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:29.068 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch Color Mode is CT. Color is Moonlight.

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:20.634 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch: Power: on / Brightness: 100% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:20.633 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch Color Mode is CT. Color is Moonlight.

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:20.516 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch: Power: on / Brightness: 100% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:271](http://10.0.2.38/logs/past#dev271)2019-01-03 08:51:20.515 pm [info](http://10.0.2.38/device/edit/271)Living Room - Couch Color Mode is CT. Color is Moonlight.

No errors. I will look at later. Have potential work-arounds. Tell me if it happens again and try to get data when it happens.

What I believe is happening: The return from the bulb is being split by the bulb into two messages. I have seen this in the Node.js for energy monitor responses - but only on extremely long energy stat messages and when the bulb was very busy (i.e., just received an set color command).

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.

Logs:

[dev:273](http://10.0.2.38/logs#dev273)2019-01-04 10:07:58.363 am [info](http://10.0.2.38/device/edit/273)Fireplace - Right: Power: off

[dev:273](http://10.0.2.38/logs#dev273)2019-01-04 10:07:58.318 am [error](http://10.0.2.38/device/edit/273)[index:00, mac:50C7BFDB0816, ip:0a000284, port:270f, type:LAN_TYPE_UDPCLIENT, payload:D0F281F88BFF9AF7D5EF94B6D1B4C09FEC95E68FE187E8CAF08BA9DAADF284E193B18BA998B68EA096B6F481E884E0C0F1C9F9C1F1C8E8BADFB39DAD94A593A69FBD91B3DBACF385E092B08AA899B787A589ABC6A9CDA8C4E6DCFEB2F0C2F1C1E9AC9EA881A38FADC9ACDFBCCEA7D7A3CAA5CBE9D3F1A2CFAEDCA888DFB69BDDB494D89DD9F9BBCEA2C0E097FE8AE2C281EE82ED9FBFFC94F59BFC95FB9CBE92B0D1BDD4B5C6E4DEFCBAD3A1C4B4D8B9DABF9FB292C0A9CEA6D2F0DCFE93FA99C6B2CBBBDEFCC6E4ADE2B698CB86C795C183D69AD8FAD6F490F583DCAFDBBACEAB89B391FF90E28FEE82A08CAEC3AAC996FB9AF9DBE1C3F6C685B2F0B6F2B080B889BF9DB193F792E48DEE8BC2A684BE9CA494A597D591D491D5E6D5E5D6EEDD99A899A192A7E3A6E4DCEBAEEDA9EAD2EADBE3A092A6E5D795B79BB9D6B3DE97F3D1EBC9FAC387C1F3B580C1F9BCFDB9FCB88ACBF8C1F1B4F1C584B581C5F4C6F7C7F0B290BC9EF681C8AC8EB496A796A7E2D1E4DDEDD5E1D8EFAE9EAB9EAF9DD8EADFE6A4E6D1E7DFEFDE9BAA9AB894B6DFACF395F497E38CFE87A59FF998F487E2CEEC88E192F19EC1B7D2A082B89AAB85B597BB99FA8EFC90CFBFCDA2D6B9DAB5D9AA88B2C9EB85E489ECCEF4D69AF39DF69FFAD8F4D6A0C5B7C4ADC2AC8EB496A789B99BE6CAE884ED8AE296C9BACEAFDBBE9CA6DDFF90FEA1CEA8CEECD6E6CAE88CEA9EC1AEC09FEC98F98DE8CAF08BA9C4ABCFAA88B290FE91E38EEF83A18DAFC7B2D7F5CFFFD3F182E397E290F185EC83EDCFF5C5E9CBA8C7ABC4B6E99DF895E5C7FDC9F9C9F9D5F795E78EE981F59BFE8DFEDCE6D7E598E5C9EB82F1AECAA3CEA3C2A0CCA98BB180AC8EE794CBA8C7ABC4B694AE9FB391F88BD4A2C3B1D8B9DBB7D28DEE81ED82F0AFDBBED3A381BB8AA684F486E385E092E085E1BECDB9D8ACC9EBD18AF1D3BAD4B0D5AD8FB585A98BE396F3D1EBDBF7D5A6C7B3C6B4D5A1C8A7C9EBD1E1CDEF8CE38FE092CDB9DCB1C1E3D9EBDCECDCF0D2B0C2ABCCA4D0BEDBA8DBF9C3F6C6BB97ECCEA7C9ADC8B092A899B597FF8AEFCDF7C7EBC9BADBAFDAA8C9BDD4BBD5F7CDFACFE3C1A2CDA1CEBCE397F29FEFCDF7C7EBC9ABD9B0D7BFCBA5C0B3C0E2D8E9D9E994B8C3E188E682E79FBD87B599BBD3A6C3E1DBEAD8E8C4E695F480F587E692FB94FAD8E2D5E0CCEE8DE28EE193CCB8DDB0C0E2D8E8C4E684F69FF890E48AEF9CEFCDF7C6F6C6BB97ECCEA7C9ADC8B092A89BB795FD88EDCFF5C7F3C3EFCDBEDFABDEACCDB9D0BFD1F3C9FECBE7C5A6C9A5CAB8E793F69BEBC9F3C3EFCDAFDDB4D3BBCFA1C4B7C4E6DCEDDDED90CDE1C3B1C2B1D8FAC0EDD9E1CDEF8EED99F086E3BCD1BEDABF9DA785EB84EA8FAD81A3CBAECFBFCCA5DFBA98A290A999AC9EA68AA8CDBFCD92F19EFA9FBD87B7CAB7CA]

[dev:273](http://10.0.2.38/logs#dev273)2019-01-04 10:07:56.125 am [info](http://10.0.2.38/device/edit/273)Fireplace - Right: Power: on / Brightness: 12% / Circadian State: normal / Color Temp: 4000K / Color: [hue:0, saturation:0]

[dev:273](http://10.0.2.38/logs#dev273)2019-01-04 10:07:56.121 am [info](http://10.0.2.38/device/edit/273)Fireplace - Right Color Mode is CT. Color is Moonlight.
1 Like

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.

Dave

1 Like

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)

@djgutheinz

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.

Instructions:

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.

Dave

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).

1 Like

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}}

dev:2712019-01-05 11:02:29.116 am debugrefreshResponse: index:00, mac:50C7BFA0EC9F, ip:0a000297, port:270f, type:LAN_TYPE_UDPCLIENT, payload:D0F281F88BFF9AF7D5EF94B6D1B4C09FEC95E68FE187E8CAF08BA9DAADF284E193B18BA998B68EA096B6F481E884E0C0F1C9F9C1F1C8E8BADFB39DAD94A593A69FBD91B3DBACF385E092B08AA899B787A589ABC6A9CDA8C4E6DCFEB2F0C1F2C2EABFECC5E7CBE98DE89BF88AE393E78EE18FAD97B5E68BEA98ECCC9BF2DF99F0D09CD99DBDFF8AE684A4D3BACEA686C5AAC6A9DBFBB8D0B1DFB8D1BFD8FAD6F495F990F182A09AB8F49DEB82EC8BABF996F994B499B9FA95E083EBC9E5C7AAC3A0FF8BF282E7C5FFDD94DB8FA1F2BFFEACF8BAEFA3E1C3EFCDA9CCBAE596E283F792B08AA8C6A9DBB6D7BB99B597FA93F0AFC2A3C0E2D8FACFFFBC8BC98FCEFEBBF8C187A589ABCFAADCB5D6B3FA9EBC86A49CAC9DAF9CD8EFAE9EDAECDFE6A4E5D190D5EDDC9EDCE9D0E6D1E3A696D795AD9CA49DA49DD99FDDFFD3F19EFB96DFBB99A381B184C680B7F5C684C1F0C6F1C487B2F3C5FDCBFCBE89C8FFBA8ECDF4B284C2F5D7FBD9B1C68FEBC9F3D1E0D1E0A596A39AAA92A69FA8E9D9ECD9E8DA9FAD98A1E3A196A098A899DCEDDDFFD3F198EBB4D2B3D0A4CBB9C0E2D8BEDFB3C0A589ABCFA6D5B6D986F095E7C5FFDDECC2F2D0FCDEBDC9BBD788F88AE591FE9DF29EEDCFF58EACC2A3CEAB89B391DDB4DAB1D8BD9FB391E782F083EA85EBC9F3D1E0CEFEDCA18DAFC3AACDA5D18EFD89E89CF9DBE19AB8D7B9E689EF89AB91A18DAFCBADD986E987D8ABDFBECAAF8DB7CCEE83EC88EDCFF5D7B9D6A4C9A8C4E6CAE880F590B288B894B6C5A4D0A5D7B6C2ABC4AA88B282AE8CEF80EC83F1AEDABFD2A280BA8EBE8EBE92B0D2A0C9AEC6B2DCB9CAB99BA190A090ED90BC9EF784DBBFD6BBD6B7D5B9DCFEC4F5D9FB92E1BEDDB2DEB1C3E1DBEAC6E48DFEA1D7B6C4ADCCAEC2A7F89BF498F785DAAECBA6D6F4CEFFD3F181F396F095E795F094CBB8CCADD9BC9EA4FF84A6CFA1C5A0D8FAC0F0DCFE96E386A49EAE82A0D3B2C6B3C1A0D4BDD2BC9EA494B89AF996FA95E7B8CCA9C4B496AC9EA999A985A7C5B7DEB9D1A5CBAEDDAE8CB683B3CEE299BBD2BCD8BDC5E7DDECC0E28AFF9AB882B29EBCCFAEDAAFDDBCC8A1CEA082B88FBA96B4D7B8D4BBC996E287EA9AB882B29EBCDEACC5A2CABED0B5C6B597AD9CAC9CE1CDB694FD93F792EAC8F2C0ECCEA6D3B694AE9FAD9DB193E081F580F293E78EE18FAD97A095B99BF897FB94E6B9CDA8C5B597AD9DB193F183EA8DE591FF9AE99AB882B383B3CEE299BBD2BCD8BDC5E7DDEEC2E088FD98BA80B286B69AB8CBAADEABD9B8CCA5CAA486BC8BBE92B0D3BCD0BFCD92E683EE9EBC86B69AB8DAA8C1A6CEBAD4B1C2B193A998A898E5B894B6C4B7C4AD8FB598AC9DB193F291E58CFA9FC0ADC2A6C3E1DBF997F896F3D1FDDFB7D2B3C3B0D9A3C6E4DEECD5E5DDEFDBF7D5B0C2B0EF8CE387E2C0FACAB7CA

1 Like

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.

1 Like

Finally able to replicate. See Driver 4.0.03 update.

1 Like

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)