home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.66k stars 28.82k forks source link

Tuya Switch Not Keeping Its State When Toggled #36744

Closed homecb closed 3 years ago

homecb commented 4 years ago

The problem

Tuya switch component works, but the toggle does not keep its state. The issue is a resurrection of https://github.com/home-assistant/core/issues/25992 I couldn't comment on that thread (locked), thus created a new one.

Environment

Traceback/Error logs

Additional information

memphis3 commented 4 years ago

I don't know programming, but is the HA team able to improve this in future updates?

pt., 26 cze 2020, 10:15 użytkownik Naoki Sawada notifications@github.com napisał:

As @ollo69 https://github.com/ollo69 said, the cause of this problem is the Tuya API request limit. The HA side cannot solve the cause.

I tried the verification and correction by the following PR.

As a result of taking measures for the time being, the status change made in HA is reflected immediately and the problem disappears even on the UI.

However, if you change the status with the Smart Life app, etc., there will be a time lag of several seconds to 2 minutes before it is reflected in HA.

In order to do a complete sync, you have to do the sync locally, such as with localtuya, but there is no way to get the device local key automatically and integrating it is very expensive.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/home-assistant/core/issues/36744#issuecomment-650048564, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOWN4V36YOAVLAKOWF5BH6DRYRKJLANCNFSM4N4XQGEA .

aguilaair commented 4 years ago

I don't know programming, but is the HA team able to improve this in future updates? pt., 26 cze 2020, 10:15 użytkownik Naoki Sawada notifications@github.com napisał: As @ollo69 https://github.com/ollo69 said, the cause of this problem is the Tuya API request limit. The HA side cannot solve the cause. I tried the verification and correction by the following PR. - ollo69/ha_tuya_custom#1 <ollo69/ha_tuya_custom#1> As a result of taking measures for the time being, the status change made in HA is reflected immediately and the problem disappears even on the UI. However, if you change the status with the Smart Life app, etc., there will be a time lag of several seconds to 2 minutes before it is reflected in HA. In order to do a complete sync, you have to do the sync locally, such as with localtuya, but there is no way to get the device local key automatically and integrating it is very expensive. — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#36744 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOWN4V36YOAVLAKOWF5BH6DRYRKJLANCNFSM4N4XQGEA .

Nope, this problem comes from Tuya. They seem to be closing down on people no using their apps (earlier this year they blocked tuya-convert for example)

smic717394 commented 4 years ago

I dont think the problem is the request limits, using the Appdaemon v4 they work fine.

smic717394 commented 4 years ago

Hi,

I have the device_id, local key and ip. I change the configuration.yalm and copy the files to config\custom_components\localtuya, I reset the server, I get no error just the new switch doesn't show up. I´m missing the part editing the switch.py and "and edit it for ID/DPS that is correct for your device." what exactly should I change?

kevinvugts commented 4 years ago

Same issue here. Just joined the hassio program but my tuya devices state in the UI does not work properly. After deactivating a switch for example, the lovelace UI reverts back to state "active". However the devices are turning on and of just fine.

acs-lux commented 4 years ago

For covers (shutters) I use a cover template to get rid of the unknown state. Works fine if controlled by HA but because of the unknown state and lengthy delay in updates from Tuya it rarely reflects smartlife changes. I'm thinking of making all the light switches and power sockets into templates now to fix the HA control updates since these devices dont have an unknown state it should work.

enricota66 commented 4 years ago

Nope, this problem comes from Tuya. They seem to be closing down on people no using their apps (earlier this year they blocked tuya-convert for example)

First all thanks developer for your hard work. If a Tuya problem I don't understand. Why the Alexa integration work fine? ...

aguilaair commented 4 years ago

If a Tuya problem I don't understand. Why the Alexa integration work fine? ...

As they have developed the integration themselves they have access to a special API (probably) which isn't rate limietd.

nao-pon commented 4 years ago

If a Tuya problem I don't understand. Why the Alexa integration work fine? ...

The HA integration uses its own API endpoint for HA developed by Tuya.

-https://pypi.org/project/tuyapy/

However, this API library doesn't seem to have been updated in a long time.

Beginning earlier this month, requests to this API endpoint will frequently return errors. In addition, the documentation for this API endpoint is not publicly available, so we are limited in what we can do.

cr8tor456 commented 4 years ago

Is there a way we can contact Tuya to let them know? Perhaps they are unaware of the problem.

If they are aware and wont help, perhaps we should go review our purchases on Amazon to let others know about this new complication from this manufacturer.

nao-pon commented 4 years ago

Is there a way we can contact Tuya to let them know? Perhaps they are unaware of the problem.

I am writing to tuyasmart@tuya.com today, I've posted this issue with this Issue link. There is no reply so far. I will let you know as soon as they reply.

DPAmes commented 4 years ago

I don't think Tuya have completely abandoned this api or are unaware of problems. The other issue that happened last week was that color setting for color bulbs was suddenly broken, producing wrong colors when set by the HA Tuya component. That was fixed this week, apparently at Tuya's end. I don't think that could have been part of the rate-limit problem, although it may have been caused inadvertently by changes made at the same time.

enricota66 commented 4 years ago

I am writing to tuyasmart@tuya.com today, I've posted this issue with this Issue link. There is no reply so far. I will let you know as soon as they reply.

I'm starting to understand how many problems there are home automation built on integrations. I think it will be a difficult road to go. We will absolutely need a standard protocol, or it will be the end

smic717394 commented 4 years ago

Hi guys, can anybody help me. I extracted the local key, the device id and ip, I downloaded the localtuya and copy it to config\custom_components\localtuya in configuration.yalm I created the switch: as bellow, I deleted the tuya integration and rebooted the server. I get no error, but this switch doesn't appear anywhere. What I'm doing wrong?

switch:

smic717394 commented 4 years ago

Hi guys, can anybody help me. I extracted the local key, the device id and ip, I downloaded the localtuya and copy it to config\custom_components\localtuya in configuration.yalm I created the switch: as bellow, I deleted the tuya integration and rebooted the server. I get no error, but this switch doesn't appear anywhere. What I'm doing wrong?

switch:

  • platform: localtuya host: 192.168.0.3 local_key: fe596182f1b77xxx device_id: 03132278a4cf1xxxxx name: Lamp1 update_interval: 3

Ok, I got it fixed with another version of the localtuya, an older version, the only problem the update_interval is not supported in this so it takes about 10 seconds to update https://github.com/fastcolors/localtuya-homeassistant

nao-pon commented 4 years ago

I sent a PR to PaulAnnekov/tuyaha.

Is the PR destination good there?

acs-lux commented 4 years ago

I'm thinking of making all the light switches and power sockets into templates now to fix the HA control updates since these devices dont have an unknown state it should work.

Using templates for Switches (for lights and sockets) doesn't have the same result that cover templates do. Not sure why...

gomble commented 3 years ago

I sent a PR to PaulAnnekov/tuyaha.

Is the PR destination good there?

How is the status ? When will be this fixed ?

Duke-Box commented 3 years ago

Hi,

Thanks for all the work guys but what is the workaround? I have read all the posts, including the fixes that have now been deleted.

@nao-pon @ollo69 - what do you recommend? How do I install your latest updates? Where are the instructions? @ollo69 - your readme file tells me nothing. What, as code owner, is the latest status?

Do I have to go down the route of localtuya? does this synch with the smartlife app? So many unanswered questions here. Your expertise and opinion would be greatly appreciated.

estebanz01 commented 3 years ago

tuyaha maintainer already gave an answer. There's some work to be done in order to fix this problem and he's aware of that as well as the colaborators of the tuyaha project. In the mean time, you can:

Patience is needed, since the problem is how tuyaha makes some queries to the tuya api, which is a relative flaky API in terms of TCP connection handling and rate-limiting.

Duke-Box commented 3 years ago

@estebanz01

Thanks for your prompt response. However it doesn't answer the question of how to install the latest commits. And flashing devices means that only some of them will work as the rest can't be flashed.

Localtuya seems to be the answer but I still require the answer to my question, does it synch with smartlife app without delay. It seems to be quite an effort for something that might not work as well as the old tuya integration, so I am reticent to try it without a positive response to this question.

Especially as you are advocating patience here, but in the meantime my HA installation is useless.

ollo69 commented 3 years ago

All, @estebanz01 explain very well current situation. We cannot do miracle, response time provided by Tuya APi is not under our control and Tuya seems not be interested on fix this. At this moment I suggest to try to replace standard component with tuya_custom. This is exactly the same component implemented inside Home Assistant but with some change in the library used to comunicate with Tuya Cloud. This will not resolve all issue, but all possible feed-back will be usefull to find the best working solution. When a acceptable solution will be found, we will PR the change to TuyaHA library and in HA component. Installing the custom componet is really simple adding custom repository in HACS or manual copyng the component in your custom_components directory.

estebanz01 commented 3 years ago

@ollo69 a basic approach to start fixing the issue at the origin https://github.com/PaulAnnekov/tuyaha/pull/38 . I'm looking at your tuya custom repo and it looks promising.

nombreregular commented 3 years ago

Trying @ollo69 workaround for the second day and everything is working perfect. I only have lights and plugs though, but it's working as intended.

nao-pon commented 3 years ago

@Duke-Box Remove the current Tuya component and install https://github.com/ollo69/ha_tuya_custom as a custom component instead. There are two ways to install it. One is to use HACS, and the other is to place the tuya_custom directory in the archive you downloaded in the custom_components directory. I recommend HACS.

Restart HA and install and use the Tuya Custom integration.

Controlling through HA all works. Changes in HA will be immediately reflected in the SmartLife app. However, there will be a time lag for the changes in the SmartLife app to be reflected in HA. This is indefinite between 15 seconds and 2 minutes. We have no control over this as it is due to the Tuya API.

PS, my work has been merged into ollo69/ha_tuya_custom.

GBCRAS1 commented 3 years ago

Does this custom component still needs "local_key" for each device to be added to configuration.yaml ?

enricota66 commented 3 years ago

My solution is tuya-convert and tasmota firmware

gomble commented 3 years ago

My solution is tuya-convert and Taso firmware

I have bought 3 different switches which was listed at tuyaconvert compatiblity website, no one is flashable because of new firmware. Therefor that is no solution for me...

Scope666 commented 3 years ago

My solution is tuya-convert and tasmota firmware

Not for the faint of heart. I had to break out the Dremel and soldering iron for 3 out of 15 devices. If you have a slightly older Teckin SP20 DO NOT try tuya-convert on it, 2 out of 2 bricked for me. I had to serial flash them to recover. I have another 2 SP20's that I've left with the stock firmware to avoid the Dremel mess. Using the custom-tuya component for those. I also have a Martin & Jerry 3 way dimmer that can't take Tasmota (not ESP chip) so that's still on Tuya as well.

image

homecb commented 3 years ago

@Duke-Box Remove the current Tuya component and install https://github.com/ollo69/ha_tuya_custom as a custom component instead. There are two ways to install it. One is to use HACS, and the other is to place the tuya_custom directory in the archive you downloaded in the custom_components directory. I recommend HACS.

Restart HA and install and use the Tuya Custom integration.

Controlling through HA all works. Changes in HA will be immediately reflected in the SmartLife app. However, there will be a time lag for the changes in the SmartLife app to be reflected in HA. This is indefinite between 15 seconds and 2 minutes. We have no control over this as it is due to the Tuya API.

PS, my work has been merged into ollo69/ha_tuya_custom.

Can someone help me out here please. When adding the custom repository in HACS (repo URL, setting type to Integration) the save button remains disabled. Am I missing something?

Update: Upgraded HACS, working now.

Thrasher2020 commented 3 years ago

The custom repository version is working nicely with my 2 remaining power sockets. It would be great if this version could read the power usage - I can't flash these remaining ones to Tasmota and it would be good to leave them with Tuya but get the readings!

Scope666 commented 3 years ago

I had to fight with this too... it took a few attempts but eventually HACS accepted it.

Can someone help me out here please. When adding the custom repository in HACS (repo URL, setting type to Integration) the save button remains disabled. Am I missing something?

enricota66 commented 3 years ago

My solution is tuya-convert and tasmota firmware

Not for the faint of heart. I had to break out the Dremel and soldering iron for 3 out of 15 devices. If you have a slightly older Teckin SP20 DO NOT try tuya-convert on it, 2 out of 2 bricked for me. I had to serial flash them to recover. I have another 2 SP20's that I've left with the stock firmware to avoid the Dremel mess. Using the custom-tuya component for those. I also have a Martin & Jerry 3 way dimmer that can't take Tasmota (not ESP chip) so that's still on Tuya as well.

Hi I have try all mode possible not only Tuya Convert... First soldering too....This il my first Slitinto 202 1 I can't suggest this mode because is not easy and is not the best result, anyway work fine and it can resume your bricked device... 2 Tuya convert guide on this video work fine for me....... on other two device one the same and one diffrent type.

https://www.youtube.com/watch?v=dt5-iZc4_qU&t=514s

Duke-Box commented 3 years ago

Guys, thanks for all your input. Especially @allo69 and @nao-pon for your updates and instructions. After linking your custom component into HACS I see the instruction are there too - excellent

Ok - so removed old Tuya integration, restarted HA, installed the custom component through HACS, restarted HA, configured the custom tuya by Configurator/Integrations, restarted HA. all tuya devices found! Trouble is no one told me the entity names change! No problem just change them back through Configurator/Integrations/Tuya custom component/devices and update. All my automations/scripts and lovelace cards back and appear to be working with acceptable responses

Great work!

Update: after playing with this, the response between the HA UI and the device is virtually instantaneous, as is the status in the smartlife app. Trigger something In HA and the state changes in th UI and smartlife immediately. The delay between Smartlife and the HA UI is slow - between 15 seconds best and 1min 40 secs worst. However that's fine as I would only use one or the other to control devices and the delay in HA showing a change is not a show stopper for me.

I am very pleased with this :-)

cannondale0815 commented 3 years ago

Update: after playing with this, the response between the HA UI and the device is virtually instantaneous, as is the status in the smartlife app. Trigger something In HA and the state changes in th UI and smartlife immediately. The delay between Smartlife and the HA UI is slow - between 15 seconds best and 1min 40 secs worst. However that's fine as I would only use one or the other to control devices and the delay in HA showing a change is not a show stopper for me.

The real issue is if you have automations coded against a state change. I.e. if I press the physical kitchen smart light switch, also turn on the corner light. For those kind of automations, you don't want a two minute delay.

Now, if all your switches are Tuya, then one can actually use automations in the SmartLife/Tuya app instead of HA. But if you have a combination of ecosystems and want HA to manage such automations, you are screwed when there's a two-minute delay.

Duke-Box commented 3 years ago

@ cannondale0815

Yes very true. I hadn't thought of that as I don't use smartlife automations. All my mixed ecosystem automations are within HA so that isn't a problem for me. Anything I have outside HA is usually an Alexa routine - so I should check those.

Good observation.

cvroque commented 3 years ago

Update: after playing with this, the response between the HA UI and the device is virtually instantaneous, as is the status in the smartlife app. Trigger something In HA and the state changes in th UI and smartlife immediately. The delay between Smartlife and the HA UI is slow - between 15 seconds best and 1min 40 secs worst. However that's fine as I would only use one or the other to control devices and the delay in HA showing a change is not a show stopper for me.

The real issue is if you have automations coded against a state change. I.e. if I press the physical kitchen smart light switch, also turn on the corner light. For those kind of automations, you don't want a two minute delay.

Now, if all your switches are Tuya, then one can actually use automations in the SmartLife/Tuya app instead of HA. But if you have a combination of ecosystems and want HA to manage such automations, you are screwed when there's a two-minute delay.

Yeah, I have the same issue over here. I have a tuya switch on my sink light, when I turn it on manually I also want it to turn on my yeelight and set it to max brightness. I done this through HA and it worked very well until these recent issues.

With the current delay and incorrect status report it became too unreliable and I ended up exchanging it for a sonoff basic and I'll add the tuya on my garage where it doesn't need to trigger anything.

Duke-Box commented 3 years ago

@connondale0815 @carlosvroque

I now see your point and I have the same with my HA automations triggered by an Alexa routine.

Time to rethink :-(

JorgeMaTeixeira-zz commented 3 years ago

I think this problem will let me change everything I have from tuya for mqtt devices from other brands.. But I need switchs with wifi and rf, to use wall rf switches too..

JorgeMaTeixeira-zz commented 3 years ago

@ollo69 @nao-pon i´m using custom component installed using hacs, and is working much better now.. The only issue i found is with cover switches, HA is always reporting that covers are oppening, when they aren´t oppening.

image

Maybe this happen because of the undefined state of the cover switch. When the covers are stopped, the switch should stay in undefined state, that way i can open or close. Right now they keep forcing the open state, that way if i want to open, i need to use the stop command first and only then i can use the open command. Other thing is that closing state is not showing up, i can close, but the state is not showed in HA.

estebanz01 commented 3 years ago

@JorgeMaTeixeira since you have covers, can you try cloning this PR into your local https://github.com/PaulAnnekov/tuyaha/pull/38 and run the script I posted in a comment there? I really want to see if my proposal works for this device. I only have switches. Also, @ollo69, I'm wondering if you have a cover/fan/light on tuya for testing. 😁

Scope666 commented 3 years ago

@estebanz01 If it helps, it seems to be working with my Martin & Jerry 3 way dimmer switch:

Got devices
{   'header': {'code': 'SUCCESS', 'payloadVersion': 1},
    'payload': {   'devices': [   {   'data': {   'brightness': '91',
                                                  'online': True,
                                                  'state': 'false'},
                                      'dev_type': 'light',
                                      'ha_type': 'light',
                                      'icon': 'https://images.tuyaus.com/smart/program_category_icon/tgq.png',
                                      'id': 'eb26f9da7bd1477823lcx6',
                                      'name': 'Stair Lights'},
                                  {   'data': {'online': True, 'state': True},
                                      'dev_type': 'switch',
                                      'ha_type': 'switch',
                                      'icon': 'https://images.tuyaus.com/smart/icon/1511859381_0.png',
                                      'id': '07200178dc4f223f5216',
                                      'name': 'Anne TV Switch'},
                                  {   'data': {'online': True, 'state': False},
                                      'dev_type': 'switch',
                                      'ha_type': 'switch',
                                      'icon': 'https://images.tuyaus.com/smart/icon/1526260644vu7k0j60o0tazwbryfb4bzkt9_0.jpg',
                                      'id': '18480742cc50e3cf1271',
                                      'name': 'Trent Light'},
                                  {   'data': {'online': True, 'state': False},
                                      'dev_type': 'switch',
                                      'ha_type': 'switch',
                                      'icon': 'https://images.tuyaus.com/smart/icon/1526260644vu7k0j60o0tazwbryfb4bzkt9_0.jpg',
                                      'id': '18480742cc50e3cefdca',
                                      'name': 'Theo Light'}],
                   'scenes': []}}
JorgeMaTeixeira-zz commented 3 years ago

@estebanz01 i will try, but i need toread first, I'm a newbie, everything is hard to me. Ahahah.

estebanz01 commented 3 years ago

@Scope666 thanks! if you copy/paste this script https://github.com/PaulAnnekov/tuyaha/pull/38#issuecomment-652143897 to a new py file within the tuyaha folder you cloned, and just run it, it will automatically grab the light device and it will reflect the status in real time. Let me know if you can test it 😀 .

@JorgeMaTeixeira no worries! we are here to learn. You can do this in a terminal (mac OS or Linux):

git clone https://github.com/estebanz01/tuyaha
cd tuyaha
git checkout use-session-object

For windows, you can follow this tutorial: https://www.howtogeek.com/451360/how-to-clone-a-github-repository/

Then within the tuyaha folder, you just need to create a new py file and put this script in there: https://github.com/PaulAnnekov/tuyaha/pull/38#issuecomment-652143897. You can run it by doing python3 myscriptfile.py.

Again, thank you all for you help!

matteos1 commented 3 years ago

Also in hassio tuya switch not have the control on/off

Scope666 commented 3 years ago

@Scope666 thanks! if you copy/paste this script PaulAnnekov/tuyaha#38 (comment) to a new py file within the tuyaha folder you cloned, and just run it, it will automatically grab the light device and it will reflect the status in real time. Let me know if you can test it 😀 .

Sorry for the delay, here you go. The light turned on and off as the script was running, matching the output:

Craigs-MBP:tuyaha craig$ python3 test.py 
[   <tuyaha.devices.light.TuyaLight: "Stair Lights" (eb26f9da7bd1477823lcx6)>,
    <tuyaha.devices.switch.TuyaSwitch: "Anne TV Switch" (07200178dc4f223f5216)>,
    <tuyaha.devices.switch.TuyaSwitch: "Trent Light" (18480742cc50e3cf1271)>,
    <tuyaha.devices.switch.TuyaSwitch: "Theo Light" (18480742cc50e3cefdca)>]
[   {   'data': {'brightness': '91', 'online': True, 'state': 'true'},
        'dev_type': 'light',
        'ha_type': 'light',
        'icon': 'https://images.tuyaus.com/smart/program_category_icon/tgq.png',
        'id': 'eb26f9da7bd1477823lcx6',
        'name': 'Stair Lights'},
    {   'data': {'online': True, 'state': False},
        'dev_type': 'switch',
        'ha_type': 'switch',
        'icon': 'https://images.tuyaus.com/smart/icon/1511859381_0.png',
        'id': '07200178dc4f223f5216',
        'name': 'Anne TV Switch'},
    {   'data': {'online': True, 'state': True},
        'dev_type': 'switch',
        'ha_type': 'switch',
        'icon': 'https://images.tuyaus.com/smart/icon/1526260644vu7k0j60o0tazwbryfb4bzkt9_0.jpg',
        'id': '18480742cc50e3cf1271',
        'name': 'Trent Light'},
    {   'data': {'online': True, 'state': True},
        'dev_type': 'switch',
        'ha_type': 'switch',
        'icon': 'https://images.tuyaus.com/smart/icon/1526260644vu7k0j60o0tazwbryfb4bzkt9_0.jpg',
        'id': '18480742cc50e3cefdca',
        'name': 'Theo Light'}]
turning on
True
turning off
True
turning on
True
adriandelcl commented 3 years ago

I have the same issue here, I hope this is resolved soon!

iwzoo commented 3 years ago

Hi all, this seems Tuya's problem, I've submitted a ticket to Tuya official. If you have the same problem, please contact the online consult on page https://developer.tuya.com/en/demo

Screen Shot 2020-07-03 at 4 21 01 PM
Erickclee commented 3 years ago

Guys, thanks for all your input. Especially @allo69 and @nao-pon for your updates and instructions. After linking your custom component into HACS I see the instruction are there too - excellent

Ok - so removed old Tuya integration, restarted HA, installed the custom component through HACS, restarted HA, configured the custom tuya by Configurator/Integrations, restarted HA. all tuya devices found! Trouble is no one told me the entity names change! No problem just change them back through Configurator/Integrations/Tuya custom component/devices and update.

Firstly, Thanks for all the effort and help, it really works. After using the Custom, the switch has stop toggling back and show correct state when switched. However, My Tuya temperature sensor is no longer working or showing the entity in HA after I use the tuya_custom Can anyone help to fix this portion in Tuya_custom....Thanks in advance...you guys are great! 1

ollo69 commented 3 years ago

I suspect that climate device do not provide info throught discovery, so I need to revert last change. Are you able to enable debug and attach here your log file so that I can check better received information? You should add the following line in configuration.yaml:

logger:
  default: info
  logs:
    custom_components.tuya_custom: debug