ZHA - TS004F Tuya 4-Button Dimmer Switch blueprint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Hello,

This is a blueprint for Tuya 4-button switch TS004F edition, for those using ZHA, in oposition to the blueprint proposed by @edememinov for Zigbee2MQTT here (Z2M - TS004F Tuya 4-Gang Switch blueprint).

EDITED on May 21st 2022:
All credits to @khvej8 who published this blueprint in the comments of the above blueprint for Z2M (https://community.home-assistant.io/t/z2m-ts004f-tuya-4-gang-switch-blueprint/314233/27) and to @Dunaar who shared with us a brilliant hack to solve an issue with this device after upgrading to HA 2022.4.3.

ATTENTION: Keep in mind that for this blueprint to work, you need to do a little manipulation on your switch as proposed by @Dunaar: press top-right button together with bottom-right button until bottom 4 (bottom left) blinks.

If you are trying to fix this automation after HA 2022.4.3 broke it, before applying the hack above, you will need to remove the existing TS0004 device and pair it again (press left-bottom button for 10s until 4 buttons flash).

This new version probably makes the following paragraph obsolete as now we have 12 events with ZHA! :slight_smile:

Please note that currently in ZHA, it detect less events than the Z2M version (see the whole discussion here: https://github.com/zigpy/zha-device-handlers/pull/969).

Keep in mind that I’m by no means a TS004F or blueprint specialist. I’m creating this as an attempt to to import it straight as a blueprint.

blueprint:
  name: ZHA - Tuya 4-Button Switch
  description: Automate your Tuya 4-Button Switch using ZHA events.
  domain: automation
  input:
    tuya_4button_switch:
      name: Tuya 4-Button Switch
      description: Tuya 4-Button Switch to use
      selector:
        device:
          integration: zha
          manufacturer: _TZ3000_xabckq1v
          model: TS004F
    button_1_click:
      name: Button 1 click
      description: Action to run on button upper-left single press
      default: []
      selector:
        action: {}
    button_1_double_click:
      name: Button 1 double-click
      description: Action to run on button upper-left double press
      default: []
      selector:
        action: {}
    button_1_long_press:
      name: Button 1 long press
      description: Action to run on button upper-left long press
      default: []
      selector:
        action: {}
    button_2_click:
      name: Button 2 click
      description: Action to run on button upper-right single press
      default: []
      selector:
        action: {}
    button_2_double_click:
      name: Button 2 double-click
      description: Action to run on button upper-right double press
      default: []
      selector:
        action: {}
    button_2_long_press:
      name: Button 2 long press
      description: Action to run on button upper-right long press
      default: []
      selector:
        action: {}
    button_3_click:
      name: Button 3 click
      description: Action to run on button lower-left single press
      default: []
      selector:
        action: {}
    button_3_double_click:
      name: Button 3 double-click
      description: Action to run on button lower-right double press
      default: []
      selector:
        action: {}
    button_3_long_press:
      name: Button 3 long press
      description: Action to run on button lower-left long press
      default: []
      selector:
        action: {}
    button_4_click:
      name: Button 4 click
      description: Action to run on button lower-right single press
      default: []
      selector:
        action: {}
    button_4_double_click:
      name: Button 4 double-click
      description: Action to run on button lower-right double press
      default: []
      selector:
        action: {}
    button_4_long_press:
      name: Long Press Dim-down
      description: Action to run on button lower-right long press
      default: []
      selector:
        action: {}
mode: single
max_exceeded: silent
trigger:
- platform: event
  event_type: zha_event
  event_data:
    device_id: !input 'tuya_4button_switch'
action:
- variables:
    command: '{{ trigger.event.data.command }}'
    endpoint_id: '{{ trigger.event.data.endpoint_id }}'
- choose:
  - conditions: '{{ false }}'
    sequence:
        - service: notify.persistent_notification
          data:
            message: '{{ endpoint_id }}'
            title: '{{ command }}'
  - conditions: '{{ command == ''remote_button_short_press'' }}'
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_1_click'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_2_click'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_3_click'
      - conditions: '{{ endpoint_id == 4 }}'
        sequence: !input 'button_4_click'
  - conditions: '{{ command == ''remote_button_double_press'' }}'
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_1_double_click'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_2_double_click'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_3_double_click'
      - conditions: '{{ endpoint_id == 4 }}'
        sequence: !input 'button_4_double_click'
  - conditions: '{{ command == ''remote_button_long_press'' }}'
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_1_long_press'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_2_long_press'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_3_long_press'
      - conditions: '{{ endpoint_id == 4 }}'
        sequence: !input 'button_4_long_press'
3 Likes

I get the message “unsupported url”.

I confess that I had the same issue as you when clicking on the badge, I tried to recreate it but it led to the same result.

In the end, I just copied the URL from the browser and pasted in Home Assistant and it imported correctly.

@luisfmrosa With the new things merged, is there a way to update your blueprint to allow all actions?

I want to use my TS004F for scene switching

Hi Nils, New tings merged, not sure what you mean? Have they implemented a way to change the switch to a 12 scene switch instead of the dimmer version? If yes, can you help where/how?
If yes, I will make a new version as soon as I get it working

1 Like

@luisfmrosa I think you have some search query string on the end of your url that might be hurting things. Even copying and pasting it did not work for me, but once I removed ? and all beyond it was fine.

I applied blueprint in my HA configuration but after an afternoon of test, it’s not working. If someone can help me to understand what’s happening ? thanks

Matt, we need some more detailes to help out:-)
Have you checked on developer tools that the events happen in the listen of “zha_event”?

I’ve tried to listen of zha_event but i’m not sure if logs should appear on the web page or in specific log, but when i start to listen some events and use any button of the switch, nothing happening.
However when i check traces in automation field, i’ve got this error message : “Error: UndefinedError: ‘dict object’ has no attribute ‘event’”.

{
  "node_descriptor": "NodeDescriptor(logical_type=<LogicalType.EndDevice: 2>, complex_descriptor_available=0, user_descriptor_available=0, reserved=0, aps_flags=0, frequency_band=<FrequencyBand.Freq2400MHz: 8>, mac_capability_flags=<MACCapabilityFlags.AllocateAddress: 128>, manufacturer_code=4098, maximum_buffer_size=82, maximum_incoming_transfer_size=82, server_mask=11264, maximum_outgoing_transfer_size=82, descriptor_capability_field=<DescriptorCapability.NONE: 0>, *allocate_address=True, *is_alternate_pan_coordinator=False, *is_coordinator=False, *is_end_device=True, *is_full_function_device=False, *is_mains_powered=False, *is_receiver_on_when_idle=False, *is_router=False, *is_security_capable=False)",
  "endpoints": {
    "1": {
      "profile_id": 260,
      "device_type": "0x0820",
      "in_clusters": [
        "0x0000",
        "0x0001",
        "0x0003",
        "0x0004",
        "0x1000"
      ],
      "out_clusters": [
        "0x0003",
        "0x0004",
        "0x0005",
        "0x0006",
        "0x0008",
        "0x000a",
        "0x0019",
        "0x1000"
      ]
    }
  },
  "manufacturer": "_TZ3000_xabckq1v",
  "model": "TS004F",
  "class": "zhaquirks.tuya.ts004f.TuyaSmartRemote004F"
}

The “events” should show just below the “listen” box on the same page. Do you have other buttons which work, then try and listen for their events, they should show.
If they do not show, try and check on the device page when the switch have reported last. It should not be more than 2 horus, otherwise something else is not working?
Your zigbee network, is this working well? Do you have other devices and do they all work well?

I checked on the device page and switch is up and live. So i assume it’s correctly configured in HA.
Zigbee network is working well, i have other devices connected and working without any issues.
Regarding “events” page, could you please confirm which information i’ve to put in “event type” and “listen” ? Just to double check that i correctly perform the event test.

Here is a screenshot of the listen part, right after I pressed the button.

thanks for screenshot.
Despite many test by deleting and adding the switch in HA, nothing is happening.
:frowning:

2022-03-03 11:02:22 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:22 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:22 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:23 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:23 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:02:54 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-03 11:03:18 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'event' when rendering '{{ trigger.event.data.command }}'
2022-03-03 11:03:18 ERROR (MainThread) [homeassistant.components.automation.nouvelle_automatisation_2] Nouvelle automatisation: Error executing script. Error rendering template for variables at pos 1: UndefinedError: 'dict object' has no attribute 'event'
2022-03-03 11:03:18 ERROR (MainThread) [homeassistant.components.automation.nouvelle_automatisation_2] Error while executing automation automation.nouvelle_automatisation_2: UndefinedError: 'dict object' has no attribute 'event'
2022-03-03 11:03:54 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-03 11:04:54 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-03 11:05:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:05:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:05:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139
2022-03-03 11:05:24 WARNING (MainThread) [zigpy_zigate.zigbee.application] Extended error code 139

Then I’m out of ideas. I have not seen this error.
Think the best way forward is to create a new post in the zigbee or zha area and ask. including the error in the headline. Then someone might know. In this thread people will not notice, as the headline is about the switch.

thanks mate for your help and advise.

Hi,

Adressing the url error on installing this blueprint: the correct url is:


https://community.home-assistant.io/t/zha-ts004f-tuya-4-button-dimmer-switch-blueprint/386465

The original url ends with a query for username matching. That’s why you got an error. Anyone else than Luis would got it wrong.

Luis can you update the link accordingly?

Else: thanks!! This blueprint works like a charm. All 4 buttons are responding to short click plus two right buttons are responding to long click

Ps: remember that you need to install a python script before you install this blueprint. Check the recipe here

Many thanks, @Kevin64 ! I should have been more attentive when doing it.

Just fixed it. Can you try now?

Works great Luis !

1 Like

@Kevin64 i followed your advise but error is still happening when i’d like to create automation by using blueprint.

2022-03-05 11:45:12 DEBUG (MainThread) [zigpy.zdo] [0xd729:zdo] ZDO request ZDOCmd.Bind_rsp: [<Status.SUCCESS: 0>]
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy.zdo] [0xd729:zdo] No handler for ZDO request:ZDOCmd.Bind_rsp([<Status.SUCCESS: 0>])
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.uart] Frame received: 8012000a5b00000002d7293e010000
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.api] data received 0x8012 b'00000002d7293e0100' LQI:0
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.zigbee.application] zigate_callback_handler [0, 0, 0, <Address address_mode=ADDRESS_MODE.NWK address=0xd729>, 62]
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.zigbee.application] ZPS Event APS data confirm, message routed to <Address address_mode=ADDRESS_MODE.NWK address=0xd729> 0
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.uart] Frame received: 80110008f800d7290080213e00
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.api] data received 0x8011 b'00d7290080213e' LQI:0
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.zigbee.application] zigate_callback_handler [0, 0xd729, 0, 32801, 62]
2022-03-05 11:45:12 DEBUG (MainThread) [zigpy_zigate.zigbee.application] ACK Data received 62 0
2022-03-05 11:45:35 DEBUG (MainThread) [zigpy_zigate.uart] Frame received: 80080005fb29b609e000
2022-03-05 11:45:35 DEBUG (MainThread) [zigpy_zigate.api] data received 0x8008 b'29b609e0' LQI:0
2022-03-05 11:45:35 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:46:03 ERROR (stream_worker) [homeassistant.components.stream.stream.camera.hikvision_i91bm_mainstream] Error from stream worker: Stream ended; no additional packets
2022-03-05 11:46:35 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:47:21 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'event' when rendering '{{ trigger.event.data.command }}'
2022-03-05 11:47:21 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch] ZHA - Tuya 4-Button Dimmer Switch: Error executing script. Error rendering template for variables at pos 1: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:47:21 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch] Error while executing automation automation.zha_tuya_4_button_dimmer_switch: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:47:35 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:48:35 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:49:26 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'event' when rendering '{{ trigger.event.data.command }}'
2022-03-05 11:49:26 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch] ZHA - Tuya 4-Button Dimmer Switch: Error executing script. Error rendering template for variables at pos 1: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:49:26 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch] Error while executing automation automation.zha_tuya_4_button_dimmer_switch: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:49:34 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:50:05 ERROR (stream_worker) [homeassistant.components.stream.stream.camera.hikvision_i91bm_mainstream] Error from stream worker: Stream ended; no additional packets
2022-03-05 11:50:05 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'event' when rendering '{{ trigger.event.data.command }}'
2022-03-05 11:50:05 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch_2] ZHA - Tuya 4-Button Dimmer Switch: Error executing script. Error rendering template for variables at pos 1: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:50:05 ERROR (MainThread) [homeassistant.components.automation.zha_tuya_4_button_dimmer_switch_2] Error while executing automation automation.zha_tuya_4_button_dimmer_switch_2: UndefinedError: 'dict object' has no attribute 'event'
2022-03-05 11:50:34 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008
2022-03-05 11:51:34 WARNING (MainThread) [zigpy_zigate.api] Received unhandled response 0x8008type or paste code here

image

Are you on latest version of HA? Otherwise you might need the local handler files for the switch. Try and look here ZHA - Tuya 4-button Scene Switch (TS0044) - #25 by mutchun
I have not installed the files manually, just integrated it. Maybe something with version of HA