ZHA - Tuya 3-button Scene Switch (TS0043)

An obvious rip off from Stringer and his 4 button switch, with a littlequite a lot of inspiration from carlywarly and his 2 button rip off :sunglasses:

It’s quite similar to the 3 button remote blueprint from vigonotion, but his didn’t include the long press. (Sorry, no link – as I’m a new user, I’m only allowed two per post…)

Supported actions for each button:

  1. Short press
  2. Double press
  3. Long press (5 seconds)
blueprint:
  name: ZHA - Tuya 3-Button Scene Switch
  description: Automate your Tuya 3-Button Scene Switch using ZHA events.
  domain: automation
  input:
    tuya_3button_scene_switch:
      name: Tuya 3-Button Scene Switch
      description: Tuya 3-Button Scene Switch to use
      selector:
        device:
          integration: zha
          manufacturer: _TZ3000_qzjcsmar
          model: TS0043
    button_one_short_press:
      name: Single Press
      description: Action to run on button 1 (left) single press
      default: []
      selector:
        action: {}
    button_one_double_press:
      name: Double Press
      description: Action to run on button 1 (left) double press
      default: []
      selector:
        action: {}
    button_one_long_press:
      name: Long Press
      description: Action to run on button 1 (left) long press
      default: []
      selector:
         action: {} 
    button_two_short_press:
      name: Single Press
      description: Action to run on button 2 (center) single press
      default: []
      selector:
        action: {}
    button_two_double_press:
      name: Double Press
      description: Action to run on button 2 (center) double press
      default: []
      selector:
        action: {}
    button_two_long_press:
      name: Long Press
      description: Action to run on button 2 (center) long press
      default: []
      selector:
         action: {}  
    button_three_short_press:
      name: Single Press
      description: Action to run on button 3 (right) single press
      default: []
      selector:
        action: {}
    button_three_double_press:
      name: Double Press
      description: Action to run on button 3 (right) double press
      default: []
      selector:
        action: {}
    button_three_long_press:
      name: Long Press
      description: Action to run on button 3 (right) long press
      default: []
      selector:
         action: {} 
mode: restart
max_exceeded: silent
trigger:
- platform: event
  event_type: zha_event
  event_data:
    device_id: !input 'tuya_3button_scene_switch'
action:
- variables:
    command: '{{ trigger.event.data.command }}'   
    endpoint_id: '{{ trigger.event.data.endpoint_id }}'
- choose:
  - conditions: "{{ command == 'remote_button_short_press' }}"
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_one_short_press'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_two_short_press'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_three_short_press'
  - conditions: "{{ command == 'remote_button_double_press' }}"
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_one_double_press'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_two_double_press'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_three_double_press'
  - conditions: "{{ command == 'remote_button_long_press' }}"
    sequence:
    - choose:
      - conditions: '{{ endpoint_id == 1 }}'
        sequence: !input 'button_one_long_press'
      - conditions: '{{ endpoint_id == 2 }}'
        sequence: !input 'button_two_long_press'
      - conditions: '{{ endpoint_id == 3 }}'
        sequence: !input 'button_three_long_press'
5 Likes

I get this error and I have no idea how to fix it.

Do you know a solution?

Never mind, this was because i triggered the automation via the UI.

But even if i press buttons on the tuya switch, nothing happens, i copied your code just changed the manufacturer bit to the name of mine.

Hello, thanks for this great automation. I’ve been using it for a while with my switches. My switches do not have the same manufacturer code, so I made a version here that removes the manufacturer restriction.

I’d like to report an issue I started having after the last HA upgrade to 2023.12, and the workaround I have found in case it is useful for others:

The automation this stopped working for me after the upgrade - none of the scripts seemed to be working. On deeper investigation, it turned out to be due to a change in the zha events that are being generated. I am now seeing a second event generated on each button press.

Event 1 command: remote_button_short_press
event_type: zha_event
data:
  device_ieee: [...]
  unique_id: [...]
  device_id: [...]
  endpoint_id: 1
  cluster_id: 6
  command: remote_button_short_press
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-12-27T13:35:05.203487+00:00"
context:
  id:  [...]
  parent_id: null
  user_id: null
Event 2 command: press_type
event_type: zha_event
data:
  device_ieee: [...]
  unique_id: [...]
  device_id: [...]
  endpoint_id: 1
  cluster_id: 6
  command: press_type
  args:
    - 0
  params:
    press_type: 0
origin: LOCAL
time_fired: "2023-12-27T13:35:05.203964+00:00"
context:
  id: [...]
  parent_id: null
  user_id: null

The Trace Timeline log is not very useful:

Stopped because of unknown reason "null" at 27 December [...] (runtime: 0.03 seconds)

I found that the problem is caused by the script being interrupted by the second event. Due to the “mode: restart”, the processing of the first event is interrupted before the script can complete.

I worked around this by changing the mode to single:

mode: single

This has the big disadvantage that long running scripts will no longer be interrupted if the switch is operated a second time. But for my particular use case, it is good enough for now.