tuya-home-assistant icon indicating copy to clipboard operation
tuya-home-assistant copied to clipboard

Tuya integration does not give status anymore after overnight

Open bartplessers opened this issue 2 years ago • 4 comments

Description I use home assistant with tuya integration.

Everything works well, but I noticed that in the morning, I can turn ON my light, but I can’t turn it OFF anymore. This is because my automation TOGGLE's the light:

# -----------------------------------------------
- id: '0x00158d000428fb20_action_single_right'
  alias: (S09) switch action single_right
  initial_state: true
  trigger:
  - entity_id: sensor.my_n01_p03_switch_s09_wandschakelaar_action
    platform: state
    to: single_right
  action:
  - entity_id: light.group_n01_p03_wastafel
    service: light.toggle
# -----------------------------------------------

and apparently the new STATUS of the light is not recognized. So what happens:

  • light is off
  • toggle command turns it on
  • but status (in hass) seems to remain off

If I push my switch again,

  • light is on (but hass thinks its off)
  • hass sends command to turn it on (but it is already on)

If I just reload my Tuya automations, things works perfect for “a while”. Don’t know for witch period, but it I notice that in the morning it has above behavior

Does anybody has the same experience? Resolution?

kind regards, Bart

Expected behavior

The status of Tuya devices is correctly displayed in HASS

Diagnostics for integrations and devices

see config_entry-tuya-179341e4676b8c0e6284065b58a431f9.json.txt

Home Assistant Version Home Assistant 2022.9.1

**Device info (please complete the following information, which can be found in [log] "name": "Badkamer - Wastafel ", "model": "ST2776G Wifi+\u84dd\u7259BLE", "category": "dj", "product_id": "0xmtemznyuvx7iim", "product_name": "Up/Down Light RGB+CCT", "online": true, "sub": false,

(for more details, see attached file)

bartplessers avatar Sep 09 '22 15:09 bartplessers

Problem is described in https://github.com/home-assistant/core/issues/62408

workaround: Create a yaml file, i.e.

\\SERVER\usr\share\hassio\homeassistant\packages\reload_integrations.yaml

with following content

# =================================================
# Automation voor het oplossen van problemen met TUYA devices
# =================================================

# ----------------------------------------------------------------------
# Automation: deze draait ieder uur
# ----------------------------------------------------------------------
# Tuya heeft een probleem met zijn security tokens die komen te vervallen
# zie ook https://github.com/home-assistant/core/issues/62408
# Hieronder een automatisering die de Tuya integratie herlaadt ieder uur
automation:
  - id: reload_integration_tuya
    alias: "Reload integration (Tuya)"
    description: 'Deze automatisering herlaadt de status van TUYA lampen'
    trigger:
      - platform: time_pattern
        hours: "*" # token is 2h geldig = 120 minuten. Dus herlaad iedere uur --> geen problemen
    condition: []
    action:
      - service: homeassistant.reload_config_entry
        target:
          entity_id:
            - light.badkamer_wastafel # als je één licht reload, wordt eigenlijk de volledige integratie herladen
    mode: single


# ----------------------------------------------------------------------
# Input: Als je manueel een integratie wil herladen. Hier een keuzelijst van mogelijke integraties
# ----------------------------------------------------------------------
input_select:
  reload_integration:
    name: 'Select Integration'
    options:
      - Tuya
      - OwnTracks
      - Denon Heos
      - Denon AVR-X2700H
      - AirVisual
      - Gdacs
      - Ikea
      - Life360
      - Luftdaten Roosendaal T
      - Luftdaten Rotterdam P
      - Luftdaten Rotterdam T
      - Luftdaten Tilburg P
      - Luftdaten Tilburg T
      - OpenWeather
      - Owntracks
      - Philips Hue 1
      - Philips Hue 2
      - Plugwise
      - Plex
      - Spotify
      - Synology Dsm
      - Weatherbit
      - Xbox
    initial: Tuya



# ----------------------------------------------------------------------
# Script: voor het herladen van een integratie op basis van zijn ID
# ----------------------------------------------------------------------
# Idere integratie heeft een uniek ID. Dit kan je vinden onder
# "R:\usr\share\hassio\homeassistant\.storage\core.config_entries"
script:
  reload_integration:
    alias: Reload integration
    mode: restart
    sequence:
      service: homeassistant.reload_config_entry #rest_command.reload_integration
      data:
        entry_id: >
          {% set mapper =
            {'Tuya':'179341e4676b8c0e6284065b58a431f9',
             'OwnTracks': 'f4691330488bb41bd7d47c35ced2a1db',
             'Denon Heos': '6e29dfb384fa8ae516e68f0f510b1954',
             'Denon AVR-X2700H': '19faab1d7568e81281235dc8ac87f20f',
             'AirVisual':'37redacted105b',
             'Gdacs':'05redacted2d2b18a',
             'Ikea':'377028redacted11b1951',
             'Ipp':'999b75bdredacted1716',
             'Life360':'2a76ecredacted59e7c',
             'Luftdaten Rotterdam P':'c6redacted5f1281',
             'Luftdaten Rotterdam T':'016redacted625012e',
             'Luftdaten Tilburg P':'9565redacted09c',
             'Luftdaten Tilburg T':'8redactedbf8',
             'OpenWeather':'1dd0redacted93f',
             'Owntracks':'6b204redacted2a',
             'Philips Hue 1':'7afredactedbc5',
             'Philips Hue 2':'a4redacted731e',
             'Plugwise':'474redacted12',
             'Plex':'dd13eredactede05',
             'Spotify':'12edredacted08',
             'Synology Dsm':'fd8redacted81367',
             'Weatherbit':'79ae7redacted2b5c',
             'Xbox':'e8cd7bredactede6'} %}
          {% set state = states('input_select.reload_integration') %}
          {% set id = mapper[state] if state in mapper %}
          {{id}}




# ----------------------------------------------------------------------
# Group: bovenstaande entities worden samen getoon in Lovelace autogen
# ----------------------------------------------------------------------

group:
# -------------------------------------------
  reload_integration:
    name: "Reload Integrations"
    entities:
    - input_select.reload_integration
    - script.reload_integration

bartplessers avatar Sep 13 '22 14:09 bartplessers

Hi, sorry but I have to reopen this issue

This is what happend tonight


2022-09-15 08:26:32.088 DEBUG (SyncWorker_3) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/devices/bf9f25b49af00347bf85fj/commands,                params = None,                body = {'commands': [{'code': <DPCode.SWITCH_LED: 'switch_led'>, 'value': True}]},                t = 1663223192088
2022-09-15 08:26:32.269 DEBUG (SyncWorker_3) [tuya_iot] Response: {
  "code": 1010,
  "msg": "token invalid",
  "success": false,
  "t": 1663223191868,
  "tid": "56d69b8434bf11edb8b0eead5955aa4d"
}
2022-09-15 08:26:32.269 DEBUG (SyncWorker_3) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/iot-01/associated-users/actions/authorized-login,                params = None,                body = {'username': '[email protected]', 'password': '***', 'country_code': '32', 'schema': 'tuyaSmart'},                t = 1663223192269
2022-09-15 08:26:32.391 DEBUG (SyncWorker_3) [tuya_iot] Response: {
  "result": {
    "access_token": "***",
    "expire_time": 7200,
    "platform_url": "https://openapi.tuyaeu.com",
    "refresh_token": "***",
    "uid": "***"
  },
  "success": true,
  "t": 1663223191978,
  "tid": "56de593434bf11edb8b0eead5955aa4d"
}
2022-09-15 08:26:54.857 DEBUG (SyncWorker_0) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/devices/bf9f25b49af00347bf85fj/commands,                params = None,                body = {'commands': [{'code': <DPCode.SWITCH_LED: 'switch_led'>, 'value': True}]},                t = 1663223214857
2022-09-15 08:26:54.971 DEBUG (SyncWorker_0) [tuya_iot] Response: {
  "result": true,
  "success": true,
  "t": 1663223214570,
  "tid": "645457bb34bf11ed88e4c653447a1a01"
}
2022-09-15 08:26:55.026 DEBUG (Thread-21 (_thread_main)) [tuya_iot] _on_log: Received PUBLISH (d0, q0, r0, m0), 'cloud/token/in/345c195e41e47fe6b961e19cf69acb2a', ...  (348 bytes)
2022-09-15 08:26:55.027 DEBUG (Thread-21 (_thread_main)) [tuya_iot] payload-> b'{"data":"9mG42NwHuDH5GBNlVW9DAQcCXrdSRXS3id3XekWUEL1bkQGUkreqgDsq4UHaKx3DdNdRHPs+8MWc6WqNGXw4RtLOiasP5mYhFV7ovet7lU5NBTz/hlxpyxtjxTHy0pj5T96+MwGW+Ulo5l+szgVVTIuLJ+vix2XGcixodfNx/R9KpRLJtgncYV9mRSOwXdMiMoa4I8iu5sFLJrWOQBKqTkeWlOu7ty5KtKXwv38NYgXMF6Xa/mmzpIKO9XA/MfRR","protocol":4,"pv":"2.0","sign":"4b0cfa8f77cbd76c2d6787853da4893a","t":1663223213}'
2022-09-15 08:26:55.030 DEBUG (Thread-21 (_thread_main)) [tuya_iot] on_message: {'data': {'dataId': 'd75e03cc-fb2b-408d-a05a-378a6d325214', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663223213', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '4b0cfa8f77cbd76c2d6787853da4893a', 't': 1663223213}
2022-09-15 08:26:55.031 DEBUG (Thread-21 (_thread_main)) [tuya_iot] mq receive-> {'data': {'dataId': 'd75e03cc-fb2b-408d-a05a-378a6d325214', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663223213', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '4b0cfa8f77cbd76c2d6787853da4893a', 't': 1663223213}
2022-09-15 08:26:55.032 DEBUG (Thread-21 (_thread_main)) [tuya_iot] mq _on_device_report-> [{'code': 'switch_led', 't': '1663223213', 'value': True, '20': True}]
2022-09-15 08:27:04.043 DEBUG (Thread-21 (_thread_main)) [tuya_iot] _on_log: Sending PINGREQ
2022-09-15 08:27:04.071 DEBUG (Thread-21 (_thread_main)) [tuya_iot] _on_log: Received PINGRESP

As you can see

2022-09-15 08:26:32.088 --> I turned my light on. A POST is send to tuya 2022-09-15 08:26:32.269 --> token is invalid 2022-09-15 08:26:32.269 --> a call to get a new token 2022-09-15 08:26:54.857 --> a new POST to turn the light on, this time with a valid token 2022-09-15 08:26:55.031 --> a mqtt message with the currect state of my light: 'switch_led' = True, meaning it's ON

However: this state is NOT displayed on Home Assistant!

If I do the same with a valid token, the CORRECT state is displayed in Home Assistant:

2022-09-15 10:32:16.291 DEBUG (SyncWorker_7) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/devices/bf9f25b49af00347bf85fj/commands,                params = None,                body = {'commands': [{'code': <DPCode.SWITCH_LED: 'switch_led'>, 'value': True}]},                t = 1663230736291
2022-09-15 10:32:16.546 DEBUG (SyncWorker_7) [tuya_iot] Response: {
  "result": true,
  "success": true,
  "t": 1663230736029,
  "tid": "e77aed9634d011ed81ad1613c76f6213"
}
2022-09-15 10:32:16.683 DEBUG (Thread-22 (_thread_main)) [tuya_iot] _on_log: Received PUBLISH (d0, q0, r0, m0), 'cloud/token/in/345c195e41e47fe6b961e19cf69acb2a', ...  (348 bytes)
2022-09-15 10:32:16.684 DEBUG (Thread-22 (_thread_main)) [tuya_iot] payload-> b'{"data":"/0AKjj/UEKOWc5KS3seStrUtIisE6Y+0cI2mB5nsGJcpw9XxeMTM1cOmPsHApf2rdNdRHPs+8MWc6WqNGXw4RtLOiasP5mYhFV7ovet7lU5NBTz/hlxpyxtjxTHy0pj5T96+MwGW+Ulo5l+szgVVTIuLJ+vix2XGcixodfNx/R9KpRLJtgncYV9mRSOwXdMiA+glKr5svxlqvWJamlMU6UeWlOu7ty5KtKXwv38NYgXMF6Xa/mmzpIKO9XA/MfRR","protocol":4,"pv":"2.0","sign":"9c0be8c18102612b89d5bed619908c4f","t":1663230735}'
2022-09-15 10:32:16.685 DEBUG (Thread-22 (_thread_main)) [tuya_iot] on_message: {'data': {'dataId': 'ab4edcd4-8afd-43a5-b246-868bbaaaaef3', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663230735', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '9c0be8c18102612b89d5bed619908c4f', 't': 1663230735}
2022-09-15 10:32:16.685 DEBUG (Thread-22 (_thread_main)) [tuya_iot] mq receive-> {'data': {'dataId': 'ab4edcd4-8afd-43a5-b246-868bbaaaaef3', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663230735', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '9c0be8c18102612b89d5bed619908c4f', 't': 1663230735}
2022-09-15 10:32:16.686 DEBUG (Thread-22 (_thread_main)) [tuya_iot] mq _on_device_report-> [{'code': 'switch_led', 't': '1663230735', 'value': True, '20': True}]

If I compare both mqtt responses: WRONG 2022-09-15 08:26:55.031 DEBUG (Thread-21 (_thread_main)) [tuya_iot] mq receive-> {'data': {'dataId': 'd75e03cc-fb2b-408d-a05a-378a6d325214', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663223213', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '4b0cfa8f77cbd76c2d6787853da4893a', 't': 1663223213}

CORRECT 2022-09-15 10:32:16.685 DEBUG (Thread-22 (_thread_main)) [tuya_iot] mq receive-> {'data': {'dataId': 'ab4edcd4-8afd-43a5-b246-868bbaaaaef3', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663230735', 'value': True, '20': True}]}, 'protocol': 4, 'pv': '2.0', 'sign': '9c0be8c18102612b89d5bed619908c4f', 't': 1663230735}

I can't see any differences

So I think this is an issue with the tuya integration and/or home assistant; If your token is invalid, there will be 2 POST calls to change the state of the light, but the response of the second will NOT be displayed in the GUI of Home Assistant.

any help is appreciated. If there is need for testing, be my guest.

kind regards, Bart Plessers

bartplessers avatar Sep 15 '22 08:09 bartplessers

@frenck : maybe can you take a look at this? Sorry to bother you, but I saw that https://github.com/home-assistant/core/pull/62858 suggested this problem was solved, but I still experience issues as described above. Also, the solution offered in https://github.com/home-assistant/core/issues/62408 did not solve my problem.

thanx for your time, grtz B

bartplessers avatar Sep 15 '22 08:09 bartplessers

Here is another very interesting log:

2022-09-16 08:49:51.825 --> new access_token after restart integration 2022-09-16 09:12:37.227 --> switch light on, apparently is token invalid 2022-09-16 09:12:37.337 --> new access_token aquired

But POST action to switch light on is not repeated, so light will not be turned ON


2022-09-16 08:49:50.806 ERROR (Thread-27) [tuya_iot] error while get mqtt config
2022-09-16 08:49:50.947 DEBUG (MainThread) [tuya_iot] stop
2022-09-16 08:49:50.948 DEBUG (MainThread) [tuya_iot] _on_log: Sending DISCONNECT
2022-09-16 08:49:50.956 DEBUG (Thread-28 (_thread_main)) [tuya_iot] disconnect
2022-09-16 08:49:50.959 DEBUG (SyncWorker_7) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/iot-01/associated-users/actions/authorized-login,                params = None,                body = {'username': '[email protected]', 'password': '***', 'country_code': '32', 'schema': 'tuyaSmart'},                t = 1663310990959
2022-09-16 08:49:51.825 DEBUG (SyncWorker_7) [tuya_iot] Response: {
  "result": {
    "access_token": "***",
    "expire_time": 7200,
    "platform_url": "https://openapi.tuyaeu.com",
    "refresh_token": "***",
    "uid": "***"
  },
  "success": true,
  "t": 1663310991802,
  "tid": "c343a5e6358b11ed81ae1613c76f6213"
}
2022-09-16 08:49:51.828 DEBUG (MainThread) [tuya_iot] start



(...)



2022-09-16 09:11:56.494 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Sending PINGREQ
2022-09-16 09:11:56.523 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Received PINGRESP
2022-09-16 09:12:37.023 DEBUG (SyncWorker_15) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/devices/bf9f25b49af00347bf85fj/commands,                params = None,                body = {'commands': [{'code': <DPCode.SWITCH_LED: 'switch_led'>, 'value': True}]},                t = 1663312357023
2022-09-16 09:12:37.227 DEBUG (SyncWorker_15) [tuya_iot] Response: {
  "code": 1010,
  "msg": "token invalid",
  "success": false,
  "t": 1663312357213,
  "tid": "f186818f358e11edb7bc16d5dde8e980"
}
2022-09-16 09:12:37.227 DEBUG (SyncWorker_15) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/iot-01/associated-users/actions/authorized-login,                params = None,                body = {'username': '[email protected]', 'password': '***', 'country_code': '32', 'schema': 'tuyaSmart'},                t = 1663312357227
2022-09-16 09:12:37.337 DEBUG (SyncWorker_15) [tuya_iot] Response: {
  "result": {
    "access_token": "***",
    "expire_time": 7200,
    "platform_url": "https://openapi.tuyaeu.com",
    "refresh_token": "***",
    "uid": "***"
  },
  "success": true,
  "t": 1663312357323,
  "tid": "f18f9b3d358e11eda6d81a46e4e2cc74"
}
2022-09-16 09:12:56.592 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Sending PINGREQ
2022-09-16 09:12:56.617 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Received PINGRESP



(...)





2022-09-16 09:13:52.840 DEBUG (SyncWorker_4) [tuya_iot] Request: method = POST,                 url = https://openapi.tuyaeu.com/v1.0/devices/bf9f25b49af00347bf85fj/commands,                params = None,                body = {'commands': [{'code': <DPCode.SWITCH_LED: 'switch_led'>, 'value': False}]},                t = 1663312432840
2022-09-16 09:13:53.069 DEBUG (SyncWorker_4) [tuya_iot] Response: {
  "result": true,
  "success": true,
  "t": 1663312433056,
  "tid": "1eb5b3f6358f11ed81ae1613c76f6213"
}
2022-09-16 09:13:53.118 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Received PUBLISH (d0, q0, r0, m0), 'cloud/token/in/345c195e41e47fe6b961e19cf69acb2a', ...  (348 bytes)
2022-09-16 09:13:53.119 DEBUG (Thread-30 (_thread_main)) [tuya_iot] payload-> b'{"data":"cRYNaoBe6JuIwdhF8w17C49qabzXpMXd1g13/Qz16xb5QEcVGE64TLbS91bneMEAdNdRHPs+8MWc6WqNGXw4RtLOiasP5mYhFV7ovet7lU5NBTz/hlxpyxtjxTHy0pj5T96+MwGW+Ulo5l+szgVVTIuLJ+vix2XGcixodfNx/R9KpRLJtgncYV9mRSOwXdMinRbr/XhbyCneR1QV4cmdx9nwe6pWbysQkKSNJI2ddpg3KmhBLwm1X47YMgppoHl8","protocol":4,"pv":"2.0","sign":"6e291859ab3c13a4cc200f6962cf3128","t":1663312432}'
2022-09-16 09:13:53.120 DEBUG (Thread-30 (_thread_main)) [tuya_iot] on_message: {'data': {'dataId': 'd56894bd-e5f6-4bd4-a4ed-fd3d96da9ae9', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663312432', 'value': False, '20': False}]}, 'protocol': 4, 'pv': '2.0', 'sign': '6e291859ab3c13a4cc200f6962cf3128', 't': 1663312432}
2022-09-16 09:13:53.120 DEBUG (Thread-30 (_thread_main)) [tuya_iot] mq receive-> {'data': {'dataId': 'd56894bd-e5f6-4bd4-a4ed-fd3d96da9ae9', 'devId': 'bf9f25b49af00347bf85fj', 'productKey': '0xmtemznyuvx7iim', 'status': [{'code': 'switch_led', 't': '1663312432', 'value': False, '20': False}]}, 'protocol': 4, 'pv': '2.0', 'sign': '6e291859ab3c13a4cc200f6962cf3128', 't': 1663312432}
2022-09-16 09:13:53.121 DEBUG (Thread-30 (_thread_main)) [tuya_iot] mq _on_device_report-> [{'code': 'switch_led', 't': '1663312432', 'value': False, '20': False}]
2022-09-16 09:13:57.127 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Sending PINGREQ
2022-09-16 09:13:57.150 DEBUG (Thread-30 (_thread_main)) [tuya_iot] _on_log: Received PINGRESP


bartplessers avatar Sep 16 '22 08:09 bartplessers