Core: TP-Link light & switch communication errors in Hassio 0.91.x

Created on 12 Apr 2019  ·  52Comments  ·  Source: home-assistant/core

Home Assistant release with the issue:

Since 0.91

Last working Home Assistant release (if known):
0.90 but im not sure. It's right after the integration.

Operating environment (Hass.io/Docker/Windows/etc.):

Hass.io

Component/platform:

TP-Link Smart Home

Description of problem:

Frequent errors communicating with tp-link switch hs110 and tp-link light lb100

For the switch and light in 2 seperate errors:

Could not read state for 192.168.xx.xx: Communication error

For the switch templates, bellow is one but its the same for every template entry:

Could not render template Daily Energy: UndefinedError: 'mappingproxy object' has no attribute 'today_energy_kwh'

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

tplink:
  light:
    - host: 192.168.xx.xx
  switch:
    - host: 192.168.xx.xx

Traceback (if applicable):

File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 379, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/tplink/light.py", line 176, in update
    self.smartbulb.brightness)
  File "/usr/local/lib/python3.7/site-packages/pyHS100/smartbulb.py", line 218, in brightness
    return int(light_state['dft_on_state']['brightness'])
KeyError: 'dft_on_state'

Additional information:

tplink

Most helpful comment

@TheGardenMonkey would you be able to put a PR in for this with your fix?

All 52 comments

Hey there @rytilahti, mind taking a look at this issue as its been labeled with a integration (tplink) you are listed as a codeowner for? Thanks!

The "Communication Error" is caused when the backend library cannot communicate with the device. The template error comes up when there is no data, I suppose. I'm not sure we can do much about these...

The last error is something we can fix, but for that I need more information. You could try to enable debug logging for pyHS100 in the homeassistant's configuration and/or use the command line tool to see what information is being returned (there may be a state confusion, the "location" for brightness in the payload depends on whether the library thinks the bulb is on or off.

Before the move to integration, the TP-Link switches worked without any issue. Now these communication errors are logged frequently or switch/light entities randomly become Unavailable with nothing being logged. There's no way to make the integration more stable? Am willing to gather information to help; have a mix of HS100, HS200, HS210, HS220 switches.

@imsorrywhat are you sure you are not getting an error? If you look at this https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/tplink/switch.py#L132 it should print out an error for the first time it gets disconnected after being available. Without knowing more why it is failing I or anyone else cannot really help to find out what's the problem you have, but it sounds to be different as what is described in this issue.

@rytilahti Appreciate the response! I am seeing errors but it's the same Communication Errors listed in this issue. Below is a sample from today's logs. Is there somewhere else I can look to find something more useful? I'm running Hassio on rPi.

Took this screencap just before posting this reply. See how often it goes unavailable?
Screen Shot 2019-05-02 at 10 40 16 PM

2019-05-03 03:02:56 WARNING (SyncWorker_0) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:05:00 WARNING (SyncWorker_1) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:06:33 WARNING (SyncWorker_16) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.242: Communication error
2019-05-03 03:07:35 WARNING (SyncWorker_11) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:11:46 WARNING (SyncWorker_5) [homeassistant.components.tplink.light] Could not read state for 192.168.2.27: Communication error
2019-05-03 03:18:25 WARNING (SyncWorker_11) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:24:06 WARNING (SyncWorker_5) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:26:40 WARNING (SyncWorker_0) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.242: Communication error
2019-05-03 03:26:40 WARNING (SyncWorker_14) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:28:44 WARNING (SyncWorker_2) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:31:50 WARNING (SyncWorker_6) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:32:56 WARNING (SyncWorker_0) [homeassistant.components.tplink.light] Could not read state for 192.168.2.27: Communication error
2019-05-03 03:34:25 WARNING (SyncWorker_12) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.243: Communication error
2019-05-03 03:35:00 WARNING (SyncWorker_5) [homeassistant.components.tplink.light] Could not read state for 192.168.2.27: Communication error
2019-05-03 03:36:29 WARNING (SyncWorker_10) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.125: Communication error

i have that as well

I have the template issue:

ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Menjador Today's Consumption: UndefinedError: 'mappingproxy object' has no attribute 'today_energy_kwh'

The others templates work well.

@gruesomehit that is another bug, which could be caused by communication problems though.

@imsorrywhat this error happens when the device is not responding to the requests (for a reason or another), which indicates that it's likely a problem in network connectivity.

Having the same communication error. Running two instances, one docker, version 0.88.2 non hassio without any errors on the TPLink component. One hassio on NUC running 0.93.2 which gives communication errors and an unknown state as result. Tried both manual config as well as using UI.

Running 2 TPLink HS110 switches. One working fine without issues or losing connectivity, one giving issues. Both are in the same room, with the one failing even closer to the Wifi Access Point (3 meters with concrete ceiling to pass through) then the non-failing one (same room, approx 4 meters).

Same problem for me with some switches getting communication error and HA marking them as unavailable, never trying to reconnect. All this worked fine prior to the discovery implementation. When I open the Kasa app the switch shows up as unavailable first, but then is available after some seconds, while HA keeps the state. I'd set up an automation for refreshing switches, but there are no usable services registered with the add-on :(

If there is an entity available, you can call homeassistant.update_entity on it, I think. But as long as the entity exists, it should be automatically updated every 10 or 20 seconds or so. Do they respond to pyhs100 calls from console?

i have the same problem:
Jun 01 16:43:23 HOMEAUTOMATION hass[20709]: 2019-06-01 16:43:23 INFO (MainThread) [homeassistant.components.automation] Initialized trigger sun sets Jun 01 16:43:24 HOMEAUTOMATION hass[20709]: 2019-06-01 16:43:24 INFO (MainThread) [homeassistant.components.automation] Initialized trigger sun rises Jun 01 16:44:19 HOMEAUTOMATION hass[20709]: 2019-06-01 16:44:19 WARNING (Thread-9) [homeassistant.components.tplink.light] Could not read state for 10.112.102.104: Communication error
the situation is very strange because I can activate and deactivate the lamp from the main page, but HA can not comminicate with the light [KL110] if it is activated by automation. how can i fix this?

hassio
Unfortunatelly connectivity issues remain and appear frequently

And I still get them in 0.95

I am getting the same errors since updating to the TPLink integration recently. Running 96.4

My log shows this many many times:

2019-07-27 17:30:45 WARNING (SyncWorker_0) [homeassistant.components.tplink.switch] Could not read state for 192.168.1.203: Communication error

It is HS100 plug. Configured as a switch in the component.

Hi, we can proceed with adding one entry every month the next years.
On the other hand somebody could tell us, if there will be any changes or if anybody is thinking about a solution.
My approach would be: as long as it is not possible, to change the behavior of TP-Link, pls make an option to get rid of errors if the user wanted.
These issues are completely worthless so far and simply filling up the log file. Then pls log only fatal errors.

The recent release of the backend library improves (potentially) the situation with help of caching, so porting the integration to use that could potentially solve the problem altogether.

For the time being, you can always change the default logging level by adjusting your logger's config if you want. If you want to push towards changing the default settings regarding to recorded logging levels (or change error reporting to use some sort of backoff timer), you will have to bring it up to a debate on the infrastructure repository (and I bet not all developers are interested in that, considering how hard it is sometimes to get useful input to fix bugs..).

Looking at @soppilif 's log there are only two coming from tplink, and the rest are caused by template sensor. Maybe that is something that could be fixed?

Yes, you're right. I should finally fix that and display "Unknown" in case of missing attributes.
The log level is anyway set to critical.
What I'm watching is the info section in developer tools. That seems to cannot be influenced by the logger, right?

Any update on this? Because I am still getting communication errors on 0.100.1 as of today...

i dont think it has to do with the local hardware but rather with the hassio code itself.
The smart devices used to work fine before the tplink integration.
After that the problem appeared.

I'm not sure if this should be part of this issue or not, What I observed is the TP Link integration auto discovered an HS100, and then appears to have recorded the IP address for it as part of the configuration some place.

At some later point, enough was rebooted that the IP address of the HS100 changed causing communications to fail.

WARNING (SyncWorker_0) [homeassistant.components.tplink.switch] Could not read state for 192.168.1.236: Communication error

At this point HA (0.100.2) wasn't able to communicate with the device. I was however able to resolve this by disabled the entity and then later re-enabling it.

Is that the intended behavior or should that be considered an issue? (With auto-discovery, it's not clear that a static IP address configuration for the device might be required in order for the integration to be stable.)

I'm having the same issue (example log line: Could not read state for 192.168.1.249: Communication error).
I'm in the process of transitioning from using a pi and moving everything to docker. It's curious that this only seems to be happening on the new docker instance. Sometimes they are available, and other times they aren't.
Right now I have 5 devices (3 tplink switches and 2 yeelights). On my docker instance, one light and one switch is currently unavailable (both of which have been available recently (previous 24 hours)) but on my pi instance they are all currently available.
Not sure if this may help somebody with the diagnosis. It is frustrating as I'm loathed to stop my pi instance until it's sorted

I posted some code that I think will work based on a similar issue for the HS300 in https://github.com/home-assistant/home-assistant/issues/28590#issuecomment-554688802

Could someone help me test it? Basically I just put an option to retry it after a second. I have it still printing the warning, but at least it prevents the devices from going unavailable / reverting their states incorrectly.

I would like to submit this but would like to have some others help me validate it. I am concerned that maybe I missed something because it is pretty simple :)

@TheGardenMonkey your changes work great over here! I used to have the same issue with the tplink switches and lights going unavailable randomly. Your changes fixed things over here. I replaced the stock switch.py and light.py with your modified ones and have been working flawlessly.

@rct the entities should be mapped based on the unique id (which is the mac address), so change of the IP address should not cause problems. Could you please open a separate issue for that, thanks!

When will this be released , I am running hassio on Windows VM and getting Could not read state for : Communication error. Or is possible to apply above patch to my setup ?

I don't think my pull request will be approved. The concern is that the issue isn't specific to the tp-link integration (someone else just posted on the community that they have a similar issue with Hue bulbs). Additionally there are another set of changes coming to the underlying kasa / tplink python libraries and I guess this will get re-evaluated after that.

I am still open to assisting with code changes if needed...and of course if anyone else who is able to validate my changes I would definitely appreciate it...well i think everyone would :).

I hope your changes get approved. Even if there are other long term plans for addressing this, I see no reason not to apply this fix now. Very glad I can just keep re-copying your updated TPlink files over to my install as necessary - they work perfectly over here. The TPLink integration has never works as good as it does right now, as your changes keep consistent connectivity with no "unavailable". I have lots of TPLink devices over here including, switches, lights, and strip, All work perfectly.

I would love to have your help to get these changes in my setup @TheGardenMonkey

I would love to have your help to get these changes in my setup @TheGardenMonkey

For hassio I think you can create a custom component, I am not 100% sure though and will have to look into that.

thanks I will appreciate that

A fair warning that you have to be mindful to check if other changes are merged into the tplink component and decide to go with those or not. I plan to pay attention since I like to stay on the latest version of HA. I personally just see my changes as temporary anyway

So in hassio, I assume you can still create a custom_components in your main configuration directory (at the same level where your configuration.yaml is. Under there you create a directory called tplink and simply copy all of the tplink files including the ones that I changed. This should also work for those using the pip install method so you don't have to keep remembering to copy the files (also makes it easier to just trash my changes when something better comes along).

~/.homeassistant/custom_components/tplink:

-rw-r--r-- 1 homeassistant homeassistant 5735 Jan 11 09:19 common.py
-rw-r--r-- 1 homeassistant homeassistant 364 Jan 11 09:19 config_flow.py
-rw-r--r-- 1 homeassistant homeassistant 44 Jan 11 09:19 const.py
-rw-r--r-- 1 homeassistant homeassistant 3764 Jan 11 09:19 __init__.py
-rw-r--r-- 1 homeassistant homeassistant 8651 Jan 11 09:19 light.py
-rw-r--r-- 1 homeassistant homeassistant 248 Jan 11 09:19 manifest.json
-rw-r--r-- 1 homeassistant homeassistant 381 Jan 11 09:19 strings.json
-rw-r--r-- 1 homeassistant homeassistant 6422 Jan 11 09:19 switch.py

Copy the files from here (*.py and *.json):
https://github.com/TheGardenMonkey/home-assistant/tree/dev/homeassistant/components/tplink

When you start HA your logs will see:
2020-01-11 09:23:26 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for tplink which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.

When there is an issue accessing one of the switches / lights, you should see something like this:
2020-01-11 09:40:25 WARNING (SyncWorker_5) [custom_components.tplink.switch] Retrying in 2 for 192.168.86.166|Laptop due to: Communication error

In my environment I have never had more than 1 in a row of those and I was tempted not to print them at all (or set them to INFO instead of WARNING) since there really isn't anything that can be done about them. I do still sometimes have some devices not initially discovered but from what I can tell it happens when my switches are having some issue (or are not connected to access point further away).

If for some reason it doesn't work just try to give as much detail as possible and I will try to help.

Thanks I followed your instructions successfully but looks like it is not a 100% success

2020-01-11 14:17:28 WARNING (SyncWorker_4) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Kitchen Night light due to: Communication error

2020-01-11 14:17:33 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.family_room_light is taking over 10 seconds

2020-01-11 14:17:33 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_night_light is taking over 10 seconds

2020-01-11 14:19:01 WARNING (SyncWorker_2) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX  |Kitchen Night light due to: Communication error

2020-01-11 14:19:06 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_night_light is taking over 10 seconds

2020-01-11 14:25:44 WARNING (SyncWorker_0) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX  |Kitchen Night light due to: Communication error

2020-01-11 14:25:49 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_night_light is taking over 10 seconds

2020-01-11 14:25:51 WARNING (SyncWorker_2) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX  |Kitchen Night light due to: Communication error

2020-01-11 14:25:58 WARNING (SyncWorker_1) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Kitchen Night light due to: Communication error

2020-01-11 14:26:05 WARNING (SyncWorker_2) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX  |Kitchen Night light due to: Communication error

2020-01-11 14:26:10 WARNING (MainThread) [homeassistant.components.switch] Updating tplink switch took longer than the scheduled update interval 0:00:30

2020-01-11 14:31:56 WARNING (SyncWorker_3) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Kitchen Night light due to: Communication error

2020-01-11 14:32:01 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.kitchen_night_light is taking over 10 seconds

2020-01-11 14:37:06 WARNING (SyncWorker_1) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Family Room Light due to: Communication error

2020-01-11 14:37:11 WARNING (MainThread) [homeassistant.helpers.entity] Update of switch.family_room_light is taking over 10 seconds

2020-01-11 14:37:13 WARNING (SyncWorker_1) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Family Room Light due to: Communication error

2020-01-11 14:37:20 WARNING (SyncWorker_3) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Family Room Light due to: Communication error

2020-01-11 14:37:27 WARNING (SyncWorker_4) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Family Room Light due to: Communication error

2020-01-11 14:37:32 WARNING (MainThread) [homeassistant.components.switch] Updating tplink switch took longer than the scheduled update interval 0:00:30

2020-01-11 14:39:41 WARNING (SyncWorker_3) [custom_components.tplink.switch] Retrying in 2 for XXXXXXX |Kitchen Night light due to: Communication error

From: Angelo Gagliano notifications@github.com
Reply-To: home-assistant/home-assistant reply@reply.github.com
Date: Saturday, January 11, 2020 at 9:55 AM
To: home-assistant/home-assistant home-assistant@noreply.github.com
Cc: jaydhary14 jay.dhary@gmail.com, Comment comment@noreply.github.com
Subject: Re: [home-assistant/home-assistant] TP-Link light & switch communication errors in Hassio 0.91.x (#23045)

A fair warning that you have to be mindful to check if other changes are merged into the tplink component and decide to go with those or not. I plan to pay attention since I like to stay on the latest version of HA. I personally just see my changes as temporary anyway

So in hassio, I assume you can still create a custom_components in your main configuration directory (at the same level where your configuration.yaml is. Under there you create a directory called tplink and simply copy all of the tplink files including the ones that I changed. This should also work for those using the pip install method so you don't have to keep remembering to copy the files (also makes it easier to just trash my changes when something better comes along).

~/.homeassistant/custom_components/tplink:

-rw-r--r-- 1 homeassistant homeassistant 5735 Jan 11 09:19 common.py
-rw-r--r-- 1 homeassistant homeassistant 364 Jan 11 09:19 config_flow.py
-rw-r--r-- 1 homeassistant homeassistant 44 Jan 11 09:19 const.py
-rw-r--r-- 1 homeassistant homeassistant 3764 Jan 11 09:19 init.py
-rw-r--r-- 1 homeassistant homeassistant 8651 Jan 11 09:19 light.py
-rw-r--r-- 1 homeassistant homeassistant 248 Jan 11 09:19 manifest.json
-rw-r--r-- 1 homeassistant homeassistant 381 Jan 11 09:19 strings.json
-rw-r--r-- 1 homeassistant homeassistant 6422 Jan 11 09:19 switch.py

Copy the files from here (*.py and *.json):
https://github.com/TheGardenMonkey/home-assistant/tree/dev/homeassistant/components/tplink

When you start HA your logs will see:
2020-01-11 09:23:26 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for tplink which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.

When there is an issue accessing one of the switches / lights, you should see something like this:
2020-01-11 09:40:25 WARNING (SyncWorker_5) [custom_components.tplink.switch] Retrying in 2 for 192.168.86.166|Laptop due to: Communication error

In my environment I have never had more than 1 in a row of those and I was tempted not to print them at all (or set them to INFO instead of WARNING) since there really isn't anything that can be done about them. I do still sometimes have some devices not initially discovered but from what I can tell it happens when my switches are having some issue (or are not connected to access point further away).

If for some reason it doesn't work just try to give as much detail as possible and I will try to help.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

Just in case, did you restart an extra time? Are you getting that immediatly after the restart or after it is use for sometime? You may want to increase the SLEEP_TIME at the top of switch.py to 5 from 2 and change the MAX_ATTEMPTS to 40.

Do the devices eventually go offline or just not show up in HA at all?

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Hi, the issue is still valid. I have two switches which return unavailable everyday several times.
I changed the repeater and choosed a stronger one, there had been an update of the devices provided by tp-link and I'm on 107.7 now.
The cases decreased but they are still valid.

I use lots of tp-link devices (lights, switches, plugs, etc). I can confirm 100% that @TheGardenMonkey modifications fix the problem. Since there has never been an official pull request that I'm aware of, I just keep copying his versions over each latest release of HA to keep things working properly. You need the light.py and switch.py from his repo and the problem with unavailable status will go away. It is the best we can do right now short of a pull request that gets approved.

ok, I will look for them and modify my .py files

@TheGardenMonkey would you be able to put a PR in for this with your fix?

https://github.com/home-assistant/core/pull/35460

Submitted an updated version of @TheGardenMonkey fix a few days ago. This will fix the "unknown" status issues with the lights and switches for TPLink. Been using it for 6 months and it works perfect.

I have a lot of disconnections with tplink switches and bulbs. I solve the problem when I use without discovery

fyi, this error still exists in 110.1
I have 7 switches and I get the following many times a day.
Could not read state for 192.168.1.110: Communication error
thanks

@DarwinData - We have several pull requests in that will fix this issue. I have been running the modified tplink component for the past 5 months and it works perfect. Our pull requests have not been approved due to them being work-arounds and there is other work being done to the back end library. I still don't understand why the fix/work-around can't be added for the time being. In the meantime, feel free to copy switch.py and light.py from my repository. Copy them over the release version and you will be fine. The other option is to run it as a custom component. Here is the link: https://github.com/MarkHofmann11/core/tree/dev/homeassistant/components/tplink

Hi, I solved mine by replacing my repeater and getting a better connectivity for the switches. No errors from then on

OK, so I tried the updated code as a custom component and get the following error:
Unable to prepare setup for platform tplink.switch: Platform not found (cannot import name 'SwitchEntity' from 'homeassistant.components.switch' (/usr/src/homeassistant/homeassistant/components/switch/__init__.py)).
Connection lost. Reconnecting…
None of the switches work so I had to revert back.
thanks

@DarwinData - What version of HA? I think you need to be running the very latest version since I pulled those changes from the dev repository. Most likely at least 110.

I was on 109.6 when I got the errors but I have now updated to 110.2 and the log errors are gone.
thanks!

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

This issue is still current to me

This seemed to be fixed for a while, but i've also started having issues again as well.

Yes, I thought it was just me. Indeed it again stopped working

I've recently replaced my outdated tplink router (flashed openwrt) with ubiquiti amplifi and so far i havent experienced any issue with my tplink switch and light bulb.

It may be related with an overloaded router? Mine was having troubles supporting 20+ devices connected at the same time. Maybe that affected the connectivity of some?

Was this page helpful?
0 / 5 - 0 ratings