Tuya integration hangsup

New version: Tuya Test v2.5

The following automation is a workaround, which has proven for months to be a working solution for the complete Tuya lock-up problem. This will mainly test and only executes a reload when there is really a lock-up, because every reload creates a “Tuya outage” of 20 seconds where you might loose data.
It requires some tinkering: define helpers and define the device.entity in the automation and modify or delete the reporting (I used Telegram), if needed. There is now also logging written to the syslog, so you can always verify the functioning.

Rough steps to take:

  1. Make three helpers: an input_boolean “Tuya Test TempState” and two counters: “Tuya success” and “Tuya failed”. Setup/modify the counters with step size 1 (although checking is done every 5 minutes).
  2. Install the automation, by pasting the below into a new automation (put your automation view in YAML mode). Disable automation, after saving, until you are finished editing.
  3. Replace the device_id “xxxxx” with the ID of your own selected Tuya cloud device (you can find the ID by right clicking on the “diagnostic download”-link, in the device screen, and copy the last part of that link), you want to use as the “master-sample” (preferably with an electronic switch, like child-lock or LED-indicator) for this automation.
  4. Replace the entity_id “switch.yyyyy_child_lock” with the entity-id (you can find the ID if you click the configuration-“wheel” in the entity screen) of the previous selected “master-sample” Tuya cloud device.

I made a little dashboard with the two counter entities (unit is minutes):
screenshot.233

The automation (copy into a new automation and disable immediately, after saving, until you are ready setting up the helpers and defining the “master test sample” Tuya device-entity ):

alias: Tuya Test v2.5
description: >-
  This automation is a work-around to reload the Tuya integration when the Tuya
  process locks up. This is tested by setting and probing an entity every 5
  minutes. When a test fails, there will be one additional retry after one
  minute, before the decision to reload. This approach, sees to it, that a
  reload is only performed when really necessary, because a reload creates a
  Tuya "outage" of more than 20 seconds. You can scan the system-log for entries
  with: [Automation][TuyaTest2.5]
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ ((now().strftime('%M')|int(0) % 5) == 0) }}"
      - condition: template
        value_template: "{{ states('counter.tuya_failed')|int(0) == 1 }}"
action:
  - service: telegram_bot.send_message
    data:
      message: >-
        Debug: Check started{{'\r\n'}}Success-counter =
        {{states('counter.tuya_success')}} min  Failed-counter =
        {{states('counter.tuya_failed')}} min
      disable_notification: true
    enabled: false
  - service: system_log.write
    data_template:
      message: >-
        [Automation][TuyaTest2.5] Check started: Success-counter =
        {{states('counter.tuya_success')}} min  Failed-counter =
        {{states('counter.tuya_failed')}} min
      level: debug
    enabled: false
  - if:
      - condition: state
        entity_id: switch.yyyyy_child_lock
        state: unavailable
    then: null
    else:
      - if:
          - condition: device
            type: is_on
            device_id: xxxxx
            entity_id: switch.yyyyy_child_lock
            domain: switch
        then:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.tuya_test_tempstate
        else:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.tuya_test_tempstate
      - type: toggle
        device_id: xxxxx
        entity_id: switch.yyyyy_child_lock
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: switch.yyyyy_child_lock
                state: unavailable
              - condition: and
                conditions:
                  - condition: device
                    type: is_off
                    device_id: xxxxx
                    entity_id: switch.yyyyy_child_lock
                    domain: switch
                  - condition: state
                    entity_id: input_boolean.tuya_test_tempstate
                    state: "off"
              - condition: and
                conditions:
                  - condition: device
                    type: is_on
                    device_id: xxxxx
                    entity_id: switch.yyyyy_child_lock
                    domain: switch
                  - condition: state
                    entity_id: input_boolean.tuya_test_tempstate
                    state: "on"
        sequence:
          - if:
              - condition: template
                value_template: "{{ states('counter.tuya_failed')|int(0) >= 1 }}"
            then:
              - if:
                  - condition: template
                    value_template: "{{ states('counter.tuya_failed')|int(0) == 1 }}"
                then:
                  - service: counter.reset
                    data: {}
                    target:
                      entity_id: counter.tuya_success
                  - service: counter.decrement
                    entity_id: counter.tuya_failed
                  - service: telegram_bot.send_message
                    data:
                      message: >-
                        Error: No Tuya reaction anymore{{'\r\n'}}Success-counter
                        = {{states('counter.tuya_success')}} min  Failed-counter
                        = {{states('counter.tuya_failed')}} min
                      disable_notification: true
                    enabled: true
                  - service: system_log.write
                    data_template:
                      message: >-
                        [Automation][TuyaTest2.5] No Tuya reaction anymore:
                        Success-counter = {{states('counter.tuya_success')}}
                        min  Failed-counter = {{states('counter.tuya_failed')}}
                        min
                      level: error
              - repeat:
                  count: 5
                  sequence:
                    - service: counter.increment
                      entity_id: counter.tuya_failed
              - service: telegram_bot.send_message
                data:
                  message: "Warning: Reload Tuya integration in progress{{'\r\n'}}Success-counter = {{states('counter.tuya_success')}} min  Failed-counter = {{states('counter.tuya_failed')}} min"
                  disable_notification: true
                enabled: true
              - service: system_log.write
                data_template:
                  message: >-
                    [Automation][TuyaTest2.5] Reload Tuya integration in
                    progress: Success-counter =
                    {{states('counter.tuya_success')}} min  Failed-counter =
                    {{states('counter.tuya_failed')}} min
                  level: warning
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 5
                  milliseconds: 0
              - service: homeassistant.reload_config_entry
                data: {}
                target:
                  entity_id: switch.yyyyy_child_lock
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 20
                  milliseconds: 0
            else:
              - service: counter.increment
                entity_id: counter.tuya_failed
              - service: system_log.write
                data_template:
                  message: >-
                    [Automation][TuyaTest2.5] Retry, Tuya Test failed once:
                    Success-counter = {{states('counter.tuya_success')}} min 
                    Failed-counter = {{states('counter.tuya_failed')}} min
                  level: warning
              - service: telegram_bot.send_message
                data:
                  message: "Warning: Retry, Tuya Test failed once{{'\r\n'}}Success-counter = {{states('counter.tuya_success')}} min  Failed-counter = {{states('counter.tuya_failed')}} min"
                  disable_notification: true
                enabled: false
    default:
      - if:
          - condition: template
            value_template: "{{ states('counter.tuya_success')|int(0) == 0 }}"
        then:
          - service: counter.reset
            data: {}
            target:
              entity_id: counter.tuya_failed
          - service: telegram_bot.send_message
            data:
              message: >-
                "Warning: Tuya is active again{{'\r\n'}}Success-counter =
                {{states('counter.tuya_success')}} min  Failed-counter =
                {{states('counter.tuya_failed')}} min"
              disable_notification: true
            enabled: true
          - service: system_log.write
            data_template:
              message: >-
                "[Automation][TuyaTest2.5] Tuya active again: Success-counter =
                {{states('counter.tuya_success')}} min  Failed-counter =
                {{states('counter.tuya_failed')}} min"
              level: warning
      - if:
          - condition: template
            value_template: "{{ states('counter.tuya_failed')|int(0) == 1 }}"
        then:
          - service: counter.reset
            data: {}
            target:
              entity_id: counter.tuya_failed
        else:
          - repeat:
              count: 5
              sequence:
                - service: counter.increment
                  entity_id: counter.tuya_success
  - if:
      - condition: state
        entity_id: switch.yyyyy_child_lock
        state: unavailable
    then: null
    else:
      - if:
          - condition: state
            entity_id: input_boolean.tuya_test_tempstate
            state: "on"
        then:
          - type: turn_on
            device_id: xxxxx
            entity_id: switch.yyyyy_child_lock
            domain: switch
        else:
          - type: turn_off
            device_id: xxxxx
            entity_id: switch.yyyyy_child_lock
            domain: switch
mode: single
3 Likes