Tuya TS004F: 4 Button Wireless Switch - Zigbee2MQTT (2023)

A blueprint for Tuya’s TS004F 4-button wireless switch.

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

blueprint:
  name: Zigbee2MQTT - Tuya 4-Button Scene Switch TS004F
  description: Automate your Tuya 4-Button Scene Switch (TS004F) via Zigbee2MQTT. Link - https://www.zigbee2mqtt.io/devices/TS004F.html
  domain: automation
  input:
    switch:
      name: TS004F Switch
      description: Tuya 4-Button Wireless Switch
      selector:
        entity:
          domain: sensor
          integration: mqtt
    button_one_short_press:
      name: Button 1 - Single Press
      description: Action to run on button 1 (bottom-left) single press
      default: []
      selector:
        action: null
    button_one_hold:
      name: Button 1 - Hold
      description: Action to run on button 1 (bottom-left) single press
      default: []
      selector:
        action: null
    button_one_double_press:
      name: Button 1 - Double Press
      description: Action to run on button 1 (bottom-left) single press
      default: []
      selector:
        action: null
    button_two_short_press:
      name: Button 2 - Single Press
      description: Action to run on button 2 (lower-right) single press
      default: []
      selector:
        action: null
    button_two_hold:
      name: Button 2 - Hold
      description: Action to run on button 2 (lower-right) single press
      default: []
      selector:
        action: null
    button_two_double_press:
      name: Button 2 - Double Press
      description: Action to run on button 2 (lower-right) single press
      default: []
      selector:
        action: null
    button_three_short_press:
      name: Button 3 - Single Press
      description: Action to run on button 3 (top-right) single press
      default: []
      selector:
        action: null
    button_three_hold:
      name: Button 3 - Hold
      description: Action to run on button 3 (top-right) single press
      default: []
      selector:
        action: null
    button_three_double_press:
      name: Button 3 - Double Press
      description: Action to run on button 3 (top-right) single press
      default: []
      selector:
        action: null
    button_four_short_press:
      name: Button 4 - Single Press
      description: Action to run on button 4 (top-left) single press
      default: []
      selector:
        action: null
    button_four_hold:
      name: Button 4 - Long Press
      description: Action to run on button 4 (lower-left) long press
      default: []
      selector:
        action: null
    button_four_double_press:
      name: Button 4 - Double Press
      description: Action to run on button 4 (lower-left) long press
      default: []
      selector:
        action: null
  source_url: https://gist.github.com/whalelion/5f19317ea530687a768ed2c34218cbc4
mode: single
max_exceeded: silent
variables:
  command: "{{ trigger.payload }}"
  topic: "{{ trigger.topic }}"
  switch_name: !input "switch"
  deviceName: "{{ state_attr(switch_name, 'friendly_name') }}"
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+/action
action:
  - choose:
      - conditions:
          - "{{ topic.split('/')[1].replace('/', '') == deviceName.split(' ')[0]
            }}"
        sequence:
          - choose:
              - conditions:
                  - "{{ command == '1_single' }}"
                sequence: !input "button_one_short_press"
              - conditions:
                  - "{{ command == '2_single' }}"
                sequence: !input "button_two_short_press"
              - conditions:
                  - "{{ command == '3_single' }}"
                sequence: !input "button_three_short_press"
              - conditions:
                  - "{{ command == '4_single' }}"
                sequence: !input "button_four_short_press"
              - conditions:
                  - "{{ command == '1_double' }}"
                sequence: !input "button_one_double_press"
              - conditions:
                  - "{{ command == '2_double' }}"
                sequence: !input "button_two_double_press"
              - conditions:
                  - "{{ command == '3_double' }}"
                sequence: !input "button_three_double_press"
              - conditions:
                  - "{{ command == '4_double' }}"
                sequence: !input "button_four_double_press"
              - conditions:
                  - "{{ command == '1_hold' }}"
                sequence: !input "button_one_hold"
              - conditions:
                  - "{{ command == '2_hold' }}"
                sequence: !input "button_two_hold"
              - conditions:
                  - "{{ command == '3_hold' }}"
                sequence: !input "button_three_hold"
              - conditions:
                  - "{{ command == '4_hold' }}"
                sequence: !input "button_four_hold"

  1. Short press
  2. Double press
  3. Long press (5 seconds)

It’s a copy of Z2M - TS004F Tuya 4-Gang Switch blueprint with the following additions.

  1. The button names were wrong in the original blueprint.
    Button 1 - Left Bottom
    Button 2 - Right Bottom
    Button 3 - Top Right
    Button 4 - Top Left
  2. This blueprint follows the order
    Button 1 Single Press
    Button 1 Long Press
    Button 1 Double Press
    Button 2 Single Press
    Button 2 Long Press
    Button 2 Double Press

The original blueprint sometimes had inconsistencies in naming convention such as:
Button 1 Single Press
Button 1 Long Press
Button 1 Double Press
Button 2 Hold
Button 2 Double Press
Button 3 Long Press

Notes:

  1. A friendly name has to be set for the button in Z2M
    2. The name cannot contain any spaces or special characters, if it does, the automation won’t work.
8 Likes

this was confusing me earlier today on the original blueprint.

not sure if you noticed but on your version Single Press - Button 4 is worded slightly differently to the other buttons

Good catch! And here I was trying to improve the inconsistencies in the original blueprint :laughing:
I’ve fixed it.

:grin: appreciated!

Hi there,
first of all i’m new at this whole home assistant experience but getting there :stuck_out_tongue:
Thanks for the blueprint, finally i found some help.
i added the blueprint but now my HA on Raspberry 4 just crashed and got back up in safe mode? :frowning:

Hey. I’m so sorry to hear that.
Let’s try two things and see if we can resolve the issue.

  1. If you think the issue is with this blueprint, can you try removing it and using the original blueprint- Z2M - TS004F Tuya 4-Gang Switch blueprint
  2. Can you share your HA log, let’s see if there are any errors. Go to Settings > System > Logs

Hi Sagar,
Thanks for your reply!
after trying a lot of times to get my backup running again, i got my HA back online and working back like normal. Don’ know what i did wrong, but i don’t think it was because of your blueprint.
So i did put the blueprint back in my HA.
everthing still ok but now the entity for the automation (so the button) i can not see in the list of choice?

Yeah I had the same problem. You have to manually enter the name of the entity. It should be in this format sensor.entityname_action.
Make sure your entity name does not have any spaces between them. For example, here’s my entity name sensor.guestroomswitch_action

1 Like

I found the blueprint file and try to put the entity in but i only see the device and no entity’s of it.
So i tried like you said :

    entity: sensor.zigbeebutton1_action

but i get ‘bad indentation of a mapping entry’.
Sorry still a noob at this :stuck_out_tongue:

Hey,

You only have to enter the name of the entity sensor.zigbeebutton1_action in the input box. Are you trying to put entity: sensor.zigbeebutton1_action in the input box?

Hi,
New here.
I try your blueprint but it is not working for me.
It seems that the automation triggered but nothing happens after pressing any of the buttons I configured.
What may be the problem?

Can you share your zigbee2mqtt log?

Hi, thanks a lot for the blueprint !

I have the same problem. Button is triggered. Automation works when I make it run specifically in the application. However the button doesn’t trigger the automation. I finally realized that the problem was the naming of my TS004F in Z2M: it doesn’t like the underscores.

switch.me_4_buttons (doesn’t work)
switch.me4buttons (works)

Do you think you could make it accept the underscores ? It would be very nice as it is more readable ;-))

1 Like

Hello Sagar,
I just used your version and ran into the same problem that others reported, the condition doesn’t work unless the friendly name is without spaces or special characters. I have a syntax for all my devices and didn’t want to deviate. Changing the condition to the following eliminates that problem and allows for free friendly names:

action:
  - choose:
      - conditions:
          - "{{ topic.split('/')[1] + ' action' == deviceName }}"

Other than that, it worked great - thanks for improving this from the original version.

Hope this helps,
Mac

1 Like

Thank you. I have updated the blueprint.

Hi,
It doesn’t seem that the blueprint has been updated.
I changed the code in my HA according to Mkloberg suggestion and works perfectly now, thank you!

Here is corrected blueprint code that works:

blueprint:
  name: Zigbee2MQTT - Tuya 4-Button Scene Switch TS004F
  description: Automate your Tuya 4-Button Scene Switch (TS004F) via Zigbee2MQTT.
  domain: automation
  input:
    switch:
      name: TS004F Switch
      description: Tuya 4-Button Scene Switch to use
      selector:
        entity:
          domain: sensor
          integration: mqtt
          multiple: false
    button_one_short_press:
      name: Single Press - Button 1
      description: Action to run on button 1 (upper-left) single press
      default: []
      selector:
        action: {}
    button_one_hold:
      name: Hold - Button 1
      description: Action to run on button 1 (upper-left) single press
      default: []
      selector:
        action: {}
    button_one_double_press:
      name: Double Press - Button 1
      description: Action to run on button 1 (upper-left) single press
      default: []
      selector:
        action: {}
    button_two_hold:
      name: Hold - Button 2
      description: Action to run on button 2 (upper-right) single press
      default: []
      selector:
        action: {}
    button_two_double_press:
      name: Double Press - Button 2
      description: Action to run on button 2 (upper-right) single press
      default: []
      selector:
        action: {}
    button_two_short_press:
      name: Single Press - Button 2
      description: Action to run on button 2 (lower-right) single press
      default: []
      selector:
        action: {}
    button_three_short_press:
      name: Single Press - Button 3
      description: Action to run on button 3 (lower-left) single press
      default: []
      selector:
        action: {}
    button_three_hold:
      name: Hold - Button 3
      description: Action to run on button 3 (lower-left) long press
      default: []
      selector:
        action: {}
    button_three_double_press:
      name: Double Press - Button 3
      description: Action to run on button 3 (lower-left) long press
      default: []
      selector:
        action: {}
    button_four_short_press:
      name: Single Press - Button 4
      description: Action to run on button 4 (lower-right) single press
      default: []
      selector:
        action: {}
    button_four_hold:
      name: Long Press - Button 4
      description: Action to run on button 4 (lower-right) long press
      default: []
      selector:
        action: {}
    button_four_double_press:
      name: Double Press - Button 4
      description: Action to run on button 4 (lower-right) long press
      default: []
      selector:
        action: {}
  source_url: https://gist.github.com/edememinov/1ae89374ece50d2aa4beee4bf8f3fd76
mode: single
max_exceeded: silent
variables:
  command: "{{ trigger.payload }}"
  topic: "{{ trigger.topic }}"
  switch_name: !input switch
  deviceName: "{{ state_attr(switch_name, 'friendly_name') }}"
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/+/action
action:
  - choose:
      - conditions:
          - "{{ topic.split('/')[1] + ' action' == deviceName }}"
        sequence:
          - choose:
              - conditions:
                  - "{{ command == '1_single' }}"
                sequence: !input button_one_short_press
              - conditions:
                  - "{{ command == '2_single' }}"
                sequence: !input button_two_short_press
              - conditions:
                  - "{{ command == '3_single' }}"
                sequence: !input button_three_short_press
              - conditions:
                  - "{{ command == '4_single' }}"
                sequence: !input button_four_short_press
              - conditions:
                  - "{{ command == '1_double' }}"
                sequence: !input button_one_double_press
              - conditions:
                  - "{{ command == '2_double' }}"
                sequence: !input button_two_double_press
              - conditions:
                  - "{{ command == '3_double' }}"
                sequence: !input button_three_double_press
              - conditions:
                  - "{{ command == '4_double' }}"
                sequence: !input button_four_double_press
              - conditions:
                  - "{{ command == '1_hold' }}"
                sequence: !input button_one_hold
              - conditions:
                  - "{{ command == '2_hold' }}"
                sequence: !input button_two_hold
              - conditions:
                  - "{{ command == '3_hold' }}"
                sequence: !input button_three_hold
              - conditions:
                  - "{{ command == '4_hold' }}"
                sequence: !input button_four_hold

I have updated the blueprint and can confirm that it works.

During the import from this URL https://gist.github.com/whalelion/5f19317ea530687a768ed2c34218cbc4, i have the following error:

while parsing a block collection in "<unicode string>", line 99, column 11: - ""{{ topic.split('/')[1] + ' a ... ^ expected <block end>, but found '{' in "<unicode string>", line 99, column 15: - ""{{ topic.split('/')[1] + ' actio ... ^

Same issue here