SONOFF TX Ultimate switch

Hi,
Is the SONOFF TX Ultimate switch compatible with home assistant?
If so do I need to flash it similar to the Switchman M5 switches?

4 Likes

Interested in this topic too as I just got myself a TX Ultimate but need it to be in Home Assistant before I can actually use it. I would prefer the flashing ESPHome option so I can uncouple the relays from the buttons.

I got this working with ESPHome by using the code from github below

To flash it with ESPHome see this image


USB –> TX 5
3V –> R15
RXD –> TXD
TXD –> RXD
GND –> GND
BOOT –> GND

Not got the sound working yet though.

1 Like

That’s great news!! I’ve just bought mine.

Question, What can you control with that code? The relays obviously but what about the LEDs and vibrations etc?? Thanks in advance…

So I uncoupled 2 of the 3 buttons
Button 1 turns on hallway light home assistant
Button 2 turns on toilet light in home assistant
Button 3 turns on relay for dehumidifier.

I also control the lights that light up around each light that is powered on
You can control all 28 led’s individually or together as shown in the code below.
The one thing it doesn’t control is that annoying button press on and off sound

Here is the code I used:
Dont forget to download the 2 additional files and have them in your ESPHome directory
touch_panel.hpp
touch_panel.cpp

# Built using:
# https://gist.github.com/wolph/42024a983e4dfb0bc1dcbe6882979d21

substitutions:
  name: 'tx-hallway'
  # Brightness and night light brightness
  brightness_on: 80%
  brightness_nightlight: 0%

esphome:
  name: $name
  platform: ESP32
  board: esp32dev

  on_boot:
    priority: -100.0
    then:
      - button.press: light_relays

  includes:
    - touch_panel.hpp
    - touch_panel.cpp

# Enable logging
logger:
#  level: DEBUG

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Tx-Hallway"
    password: !secret wifi_password

# Enable Bluetooth Proxy
esp32_ble_tracker:
    scan_parameters:
      interval: 1100ms
      window: 1100ms
      active: true

bluetooth_proxy:
  active: true

# Home Assistant Light State change
text_sensor:
  # turn on TX light when halway light in HA is turned on
  - platform: homeassistant
    name: "Hallway Light"
    id: hallway_light
    entity_id: light.hallway_light
    on_value:
      button.press: light_relays


  # turn on TX light when Toilet light in HA is turned on
  - platform: homeassistant
    id: toilet_light
    entity_id: light.toilet_light
    on_value:
      button.press: light_relays


  # Bedtime routine here reserved for future
  - platform: homeassistant
    id: night_mode
    entity_id: input_boolean.gone_to_bed

uart:
  id: uart_bus
  tx_pin: 19
  rx_pin: 22
  baud_rate: 115200

button:
  - platform: template
    name: light relays
    id: light_relays
    on_press:
      - if:
          condition:
              text_sensor.state:
                id: hallway_light
                state: 'on'
          then:
            - light.turn_on:
                id: light_left_both
                brightness: ${brightness_on}
                red: 0%
                green: 100%
                blue: 0%
                transition_length: 500ms
          else:
            - light.turn_on:
                id: light_left_both
                brightness: ${brightness_nightlight}
                red: 0%
                green: 0%
                blue: 0%
                transition_length: 2s
      - if:
          condition:
              text_sensor.state:
                id: toilet_light
                state: 'on'
          then:
            - light.turn_on:
                id: light_middle_both
                brightness: ${brightness_on}
                red: 0%
                green: 100%
                blue: 0%
                transition_length: 2s
          else:
            - light.turn_on:
                id: light_middle_both
                brightness: ${brightness_nightlight}
                red: 0%
                green: 0%
                blue: 0%
                transition_length: 2s
      - if:
          condition:
            switch.is_on: relay3
          then:
            - light.turn_on:
                id: light_right_both
                brightness: ${brightness_on}
                red: 0%
                green: 100%
                blue: 0%
                transition_length: 2s
          else:
            - light.turn_on:
                id: light_right_both
                brightness: ${brightness_nightlight}
                red: 0%
                green: 0%
                blue: 0%
                transition_length: 2s

binary_sensor:
  - platform: custom
    lambda: |- 
        auto touch_panel = new touch_panel::TouchPanel(id(uart_bus));
        App.register_component(touch_panel);
        return {
          touch_panel->left,
          touch_panel->middle,
          touch_panel->right,
          touch_panel->two_finger,
          touch_panel->dragged_ltr,
          touch_panel->dragged_rtl,
        };
    binary_sensors:
      - id: button_left
        name: "Left Button"
        on_press: # Turn on Hallway light in HA
          - homeassistant.service:
              service: "light.toggle"
              data:
                entity_id: "light.hallway_light"
          - switch.turn_on: haptics
          - delay: 2s
          - button.press: light_relays
      - id: button_middle
        name: "Middle Button"
        on_press: # Turn on Toilet light in HA
          - homeassistant.service:
              service: "light.toggle"
              data:
                entity_id: "light.toilet_light"
          - switch.turn_on: haptics
          - delay: 2s
          - button.press: light_relays
      - id: button_right
        name: "Right Button"
        on_press: # Turn on relay for dehumidifier
          - switch.toggle: relay3
          - switch.turn_on: haptics
          - delay: 2sec
          - button.press: light_relays
      - id: button_two_finger
        name: "Two Fingers"
      - id: button_dragged_ltr
        name: "Dragged Left to Right"
      - id: button_dragged_rtl
        name: "Dragged Right to Left"


# Switch Relays
switch:
  - platform: gpio
    name: "relay left"
    pin: GPIO18
    id: relay1
    restore_mode: ALWAYS_ON # Smart light bulb so must be always on
    on_turn_on:
      button.press: light_relays
    on_turn_off:
      button.press: light_relays

  - platform: gpio
    name: "relay middle"
    pin: GPIO17
    id: relay2
    restore_mode: ALWAYS_OFF # Nothing connected to this
    on_turn_on:
      button.press: light_relays
    on_turn_off:
      button.press: light_relays

  - platform: gpio
    name: "relay right"
    pin: GPIO27
    id: relay3
    restore_mode: ALWAYS_OFF # Dehumidifier default off
    on_turn_on:
      button.press: light_relays
    on_turn_off:
      button.press: light_relays

  - platform: gpio
    name: "relay right"
    pin: GPIO23
    id: relay4
    on_turn_on:
      button.press: light_relays
    on_turn_off:
      button.press: light_relays
  - platform: gpio
    name: "sound amplifier power"
    pin: GPIO26
    id: pa_sw
  - platform: gpio
    name: "touch panel power"
    pin:
      number: GPIO5
      inverted: true
    id: ca51_pow
    restore_mode: RESTORE_DEFAULT_ON
  - platform: gpio
    pin: GPIO21
    name: "Haptics"
    id: "haptics"
    restore_mode: ALWAYS_OFF
    on_turn_on:
      - delay: 400ms
      - switch.turn_off: haptics

# Light
light:
  - platform: neopixelbus
    type: GRB
    variant: WS2812
    pin: GPIO13
    num_leds: 1
    name: "NeoPixel 13"
    internal: true
  - platform: neopixelbus
    type: GRB
    variant: WS2812
    # pin: GPIO20
    pin: GPIO33
    num_leds: 28
    name: "Ambience Nightlight"
    id: rgb_light
    effects: # Found additional effects but not tested them all
      - addressable_rainbow:
          name: 'rainbow fast'
          speed: 50
      - pulse:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s
      - addressable_scan:
      - addressable_scan:
          name: Scan Effect With Custom Values
          move_interval: 100ms
          scan_width: 3
      - addressable_twinkle:
      - addressable_twinkle:
          name: Twinkle Effect With Custom Values
          twinkle_probability: 5%
          progress_interval: 4ms
      - addressable_random_twinkle:
      - addressable_random_twinkle:
          name: Random Twinkle Effect With Custom Values
          twinkle_probability: 20%
          progress_interval: 32ms
      - addressable_fireworks:
      - addressable_fireworks:
          name: Fireworks Effect With Custom Values
          update_interval: 32ms
          spark_probability: 10%
          use_random_color: false
          fade_out_rate: 120
      - addressable_flicker:
      - addressable_flicker:
          name: Flicker Effect With Custom Values
          update_interval: 16ms
          intensity: 5%

  - platform: partition
    id: light_left_both
    name: "light left both"
    segments:
      - id: rgb_light
        from: 11
        to: 21

  - platform: partition
    id: light_middle_both
    name: "light middle both"
    segments:
      - id: rgb_light
        from: 22
        to: 24
      - id: rgb_light
        from: 8
        to: 10

  - platform: partition
    id: light_right_both
    name: "light right both"
    segments:
      - id: rgb_light
        from: 0
        to: 7
      - id: rgb_light
        from: 25
        to: 27

  - platform: partition
    id: light_right
    name: "light right"
    segments:
      - id: rgb_light
        from: 0
        to: 5

  - platform: partition
    id: light_left
    name: "light left"
    segments:
      - id: rgb_light
        from: 13
        to: 19

I have just put this together to hopefully he

Perhaps this is an option too

substitutions:
  name: "shys-tx-ultimate"
  friendly_name: "SHYS TX Ultimate"
  relay_count: "2"

packages:
  smarthomeyourself.tx-ultimate: github://SmartHome-yourself/sonoff-tx-ultimate-for-esphome/tx_ultimate.yaml@main
  
esphome:
  name: ${name}
  name_add_mac_suffix: false

api:
ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  ap:
    ssid: ${friendly_name} AP
    password: "top_secret"

tx_ultimate_touch:
  id: tx_touch
  uart: my_uart

  on_press:
    - lambda: >
        ESP_LOGD("tx_ultimate_touch.on_press", "My Event: Position: %d / State: %d", touch.x, touch.state);
	
  on_release:
    - light.turn_off: leds

The on_ actions are optional to extend main functions. But for minimal use, you only need the code above the tx_ultimate component.

1 Like

Good morning, yesterday I was trying to flash my TX with Chris firmware, but after some more reading your firmware seams a lot easier to configure after in HA for a noob like me :wink:

I finally got it working, thanks for your share.
The vibration options, can I turn it off on HA or do I need to re-flash it again?

Thanks, and have a nice Sunday! :wink:

Re-flashing with ESPtools in HA is really easy.

May I ask you if its possible to use the switch as a blessing tracker? I have some ESPrensense and if I could use the switch to do the both things would be awesome.
Thanks.

Once you have flashed it with ESPHome and you have ESPHome on your home assistant you can just comment out all the lines with replace all

  • switch.turn_on: haptics
    #- switch.turn_on: haptics

Then Install & chose wireless


That will do an over the air update so not having to connect the cables again

ESPrensense only allows specific IO inputs like Temp, Motion. So until ESPHome or someone releases there own Prensense detection then no.

Hello!
I would like to buy more TX Ultimate switches. I have small children and they will be delighted. At the same time, I want to buy some smart G10 bulbs from Shelly with integrated Wifi, RGB and dimmer. I would like to create scenarios with the TX Ultimate switch, for example to be able to dim the brightness of all bulbs or only certain bulbs and to be able to change the colors. Is it possible if I integrate in HA? Will I have to rewrite the firmware with ESP Home? The manufacturer does not specify exactly whether these scenarios can be realized through the Ewelink application. Anyway, probably only with Sonoff bulbs, but they don’t have the G10 bulb type.Thank you!

The way I do this is just to program the lights in HA. Import the switch states from the Sonoff and use those to controll any light you want. I am not a fan of coding everything in an ESPHome device. Just sensors and actuators, and leaving the control to HA

Hello everyone.

Out of curiosity, after flashing esphome, is there any way to reflash the original fw?

Thank you.

Yes, you can flash your backed up firmware over serial. You did back it up before flashing esphome, right?

My knowledge on the subject is very limited, basically following videos, and was not aware of that step of the process, and was hoping that if needed the binaries could be found somewhere in the internet.

I still have 5 wall switches in the box, so i’ll search how to get a backup from one of them.

Thank you for making me aware of this.

My 2 switches arrived today so will also be looking figure out how to back up their stock firmware before I start trying to get ESPHome onto them :slight_smile:

My switches are the “US” version so are a portrait rectangle and not a square.
I flashed it using this link below but all the LEDs don’t correlate to the switch being pressed etc…
Maybe someone has got the “US” version working?

Has anyone got this working nicely on the US rectangle version?

Having had a fairly extensive play with the TX Ultimate combined with your code and some variations by others and my own I have come to the conclusion that the switch is very clunky due to, I assume, sonoff onboard code.

Examples:

  • Swipe Left and Right have to be done at exactly the right speed and pressure level to register.
  • My attempts to implement the recognition of a double click have been too hard to get user friendly - timing and touch pressure sensitivity.
  • There are a range of light effects which seem embedded - eg following a successful swipe left or right, the top lights put on a colourful show that is not under the control of esphome code.
  • A long press duration is not adjustable.

Am I missing something or do others agree ?