Sonoff SwitchMan M5 3C 3 Gang switch with ESPHome

Not much, but if someone needs an example yaml file for Sonoff SwitchMan M5 3C you can find it here

Works with the ESPHome integration of Home Assistant.

I have M5 1C but the button is not switching on/off the light (physical or through HA). Not sure what I am doing wrong.

Hi, if you were following this guide, Flashing Sonoff SwitchMan M5 with ESPHome
the settings for one button are wrong, switch is not on pin: 22 it must be on 23:

switch:
  - platform: gpio
    id: relay_1
    name: "Button"
    pin: 23
1 Like

Yep, figured that out while testing different PINs. Do you know how can I add a light instead of switch here?

I think newest HA update let you change swtich to light entity manually.

https://www.home-assistant.io/integrations/switch_as_x/

I have 2 gang switch and only 1 LED (left one) shows status when relay is on. Right LED does not show status when LED is on, anyone got same problem?

2 Likes

Hey, Have you figured this out? I have the same issue. I play around a bit with the gpios but no luck, just realized that the Right led works when pinning the relay to 22 instead of 19, but then the relay doesnt. :confused:

nothing yet

hi! somebody managed out something with the right led ? thanks

You need to create a gpio output with the pin number for each relay.

output:
  - platform: gpio
    id: relay_1
    pin: 23

Then instead of a switch, create a light changing the platform to binary and linking it to the corresponding output.

light:
  - platform: binary
    id: switch_1
    name: "Light 1"
    output: relay_1

For the phisical buttons to work, just change the on_press method from switch.toggle to light.toggle and put the corresponding light id.

binary_sensor:
  - platform: gpio
    id: left_button
    name: "kitchen left button"
    pin:
      number: 4
      mode: INPUT_PULLUP
    on_press:
      - light.toggle: switch_1

I’ll link you my full config:

I have made some progress on the 2nd LED for the 2 button switch. The second Switch LED is controlled by pin 22. I have created a virtual relay_4 tied to this pin and set it to toggle when pressing button 2. This works great on the physical switch (led goes on and off as expected) but doesnt work when triggering through HA. (switch goes on and off, but LED doesn’t) Here is my yaml, appreciate any ideas on this. (I have seen elsewhere that this could be done as a rule in HA? Not sure how to do that…

switch:
  - platform: gpio
    id: relay_1
    name: "Left Button"
    pin: 23
  - platform: gpio
    id: relay_2
    pin: 19
    name: "Right Button"
    
    
output:
  - platform: ledc
    pin: 18
    frequency: 1000 Hz
    id: pwm_output
    
  - platform: gpio
    id: relay_4
    pin: 22
    
light:
  - platform: monochromatic
    output: pwm_output
    name: "LEDs"
  - platform: status_led
    name: "Switch state"
    pin:
      number: 5
      inverted: true
      
  - platform: binary
    id: led2
    name: "LED 2"
    output: relay_4

binary_sensor:
  - platform: gpio
    id: left_button
    pin:
      number: 4
      mode: INPUT_PULLUP
    on_press:
      - switch.toggle: relay_1
  - platform: gpio
    id: right_button
    pin:
      number: 15
      mode: INPUT_PULLUP
    on_press:
      - switch.toggle: relay_2
      - light.toggle: led2
3 Likes

Thanks, your solution save my day!

Good evening Simon,

Thanks to you as well as some guys over on github working the Tasmota side of things, I’ve put together a solution that solves your issues with the LED working only when actually pressing the button.

Thread is here: Sonoff SwitchMan M5 Ledlink_i · Discussion #15757 · arendst/Tasmota · GitHub

Bottom comment you may have to click “show more”. The 1 gang is there but 2 gang should be easy to put together. Let me know if you need it directly and I will post here.

Thanks for the pin number (22).
In my opinion more elegant way to sync led with relay is to use power_supply (see buttom) in output (last line):

binary_sensor:

  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
    id: ${hostname}_switch_1
    on_press:
      then:
        - light.toggle:
            id: ${hostname}_light_1

  - platform: gpio
    pin:
      number: GPIO15
      mode: INPUT_PULLUP
    id: ${hostname}_switch_2
    on_press:
      then:
        - light.toggle:
            id: ${hostname}_light_2

output:

  - platform: gpio
    pin: GPIO23
    id: ${hostname}_relay_1

  - platform: gpio
    pin: GPIO19
    id: ${hostname}_relay_2
    power_supply: ${hostname}_led_2

light:

  - platform: binary
    name: "${name_1}"
    id: ${hostname}_light_1
    output: ${hostname}_relay_1

  - platform: binary
    name: "${name_2}"
    id: ${hostname}_light_2
    output: ${hostname}_relay_2

power_supply:

  - id: ${hostname}_led_2
    enable_time: 0s
    keep_on_time: 0s
    pin:
      number: GPIO22
      inverted: False
1 Like

Thanks for sharing this, it worked for me.

Any idea how to implement with 1 button? I do not get the LED when turning on the light.

Hi, I would like to use my Sonoff Switchman M5 as a smart switch for the yeelight lamp and not as a power cutter. Installed esphome, configuration as below, I searched the entire forum and google, however, I did not find any relevant clues on this topic.

esphome:
  name: switchman-entrance

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: ""

ota:
  password: ""

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Switchman-Entrance"
    password: ""

captive_portal:
    
output:
  - platform: ledc
    pin: 18
    frequency: 1000 Hz
    id: pwm_output
    
  - platform: gpio
    id: relay_1
    pin: 23
    
  - platform: gpio
    id: relay_2
    pin: 19
    
light:
  - platform: monochromatic
    output: pwm_output
    name: "LEDs"
  - platform: status_led
    name: "Swtich state"
    pin:
      number: 5
      inverted: true
    
  - platform: binary
    id: corridor_switch_1
    name: "Left Switch"
    output: relay_1
    
  - platform: binary
    id: corridor_switch_2
    name: "Right Switch"
    output: relay_2

    
binary_sensor:
  - platform: gpio
    id: left_button
    pin:
      number: 4
      mode: INPUT_PULLUP
    on_press:
      - light.toggle: corridor_switch_1
      
  - platform: gpio
    id: right_button
    pin:
      number: 15
      mode: INPUT_PULLUP
    on_press:
      - light.toggle: corridor_switch_2
      

what am i doing wrong, triggering gpio 22 changes RIGHT led for me, not the left one

  • #18 - controls both leds
  • #relay 1 [23] - linked to left LED!
  • #18 off, 22 on = right only (when relay1 is off)
  • #22 controls right one
  • #18 controls both, but relay 1 on = only left on

No way to have ONLY left led on without latching the relay

I thought I would share my config for my 3C switches. Feel free to use it or parts of it if you want (I spent way too much time coming up with this, so other people using it only feels right)

Features and comments:

  • PWM on both the red leds (when the relay is off) and the blue LED. This meant i opted not to have the blue led as a status led (you can opt the other way around with minimal changes, though)
  • Events for single click, double click and long press
  • Upon double click, the blue led flashes once to give some immediate feedback. Upon a long press, it flashes twice.
  • To make sure that the single click wouldn’t also trigger for a double click, I built in a delay before triggering the single click. I went with 200ms after trying some values, as it was both something I would even trust guests with, and while it was noticeable when paying attention, it wasn’t bothersome to me in normal usage. You could make the delay shorter, but I wouldn’t recommend going below 100ms
substitutions:
  deviceName: switch-1
  encryptionKey: "********"
  accessPointPassword: "*********"
  otaPassword: "******"

esphome:
  name: "${deviceName}"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "${encryptionKey}"

ota:
  password: "${accessPointPassword}"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${deviceName} AP"
    password: "${accessPointPassword}"

captive_portal:

script:
  - id: blink_blue
    then:
      - light.toggle: 
          id: blue_led
          transition_length: 0s
      - delay: 200ms
      - light.toggle: 
          id: blue_led
          transition_length: 0s
  - id: blink_blue_twice
    then:
      - light.toggle: 
          id: blue_led
          transition_length: 0s
      - delay: 200ms
      - light.toggle: 
          id: blue_led
          transition_length: 0s
      - delay: 200ms
      - light.toggle: 
          id: blue_led
          transition_length: 0s
      - delay: 200ms
      - light.toggle: 
          id: blue_led
          transition_length: 0s

switch:
    - platform: gpio
      id: relay_1
      name: "Left Light"
      pin: 23
    - platform: gpio
      id: relay_2
      name: "Middle Light"
      pin: 19
    - platform: gpio
      id: relay_3
      name: "Right Light"
      pin: 22

output:
    - platform: ledc
      pin: 18
      frequency: 1000 Hz
      id: pwm_output
    - platform: ledc
      pin:
        number: 5
        inverted: true
      frequency: 1000 Hz
      id: pwm_blue

light:
    - platform: monochromatic
      output: pwm_output
      name: "Red LEDs (when lights are off)"
    - platform: monochromatic
      name: "Blue LED"
      output: pwm_blue
      id: blue_led

binary_sensor:
    - id: left_button
      platform: gpio
      pin:
        number: 4
        mode: INPUT_PULLUP
        inverted: true
      on_multi_click:
        - timing:
            - ON for 50ms to 350ms
            - OFF for at least 201ms
          then:
            - homeassistant.event:
                event: esphome.switch_click
                data:
                  device: "${deviceName}"
                  button: left
            - switch.toggle: relay_1
        - timing:
            - ON for 50ms to 350ms
            - OFF for 5ms to 200ms
            - ON for 50ms to 350ms
            - OFF for at least 1ms
          then:
            - homeassistant.event:
                event: esphome.switch_double_click
                data:
                  device: "${deviceName}"
                  button: left
            - script.execute: blink_blue
        - timing:
            - ON for at least 500ms
          then:
            - homeassistant.event:
                event: esphome.switch_long_press
                data:
                  device: "${deviceName}"
                  button: left
            - script.execute: blink_blue_twice
          
    - id: middle_button
      platform: gpio
      pin:
        number: 0
        mode: INPUT_PULLUP
        inverted: true
      on_multi_click:
        - timing:
            - ON for 50ms to 350ms
            - OFF for at least 201ms
          then:
            - homeassistant.event:
                event: esphome.switch_click
                data:
                  device: "${deviceName}"
                  button: middle
            - switch.toggle: relay_2
        - timing:
            - ON for 50ms to 350ms
            - OFF for 5ms to 200ms
            - ON for 50ms to 350ms
            - OFF for at least 1ms
          then:
            - homeassistant.event:
                event: esphome.switch_double_click
                data:
                  device: "${deviceName}"
                  button: middle
            - script.execute: blink_blue
        - timing:
            - ON for at least 500ms
          then:
            - homeassistant.event:
                event: esphome.switch_long_press
                data:
                  device: "${deviceName}"
                  button: middle
            - script.execute: blink_blue_twice
    - id: right_button
      platform: gpio
      pin:
        number: 15
        mode: INPUT_PULLUP
        inverted: true
      on_multi_click:
        - timing:
            - ON for 50ms to 350ms
            - OFF for at least 201ms
          then:
            - homeassistant.event:
                event: esphome.switch_click
                data:
                  device: "${deviceName}"
                  button: right
            - switch.toggle: relay_3
        - timing:
            - ON for 50ms to 350ms
            - OFF for 5ms to 200ms
            - ON for 50ms to 350ms
            - OFF for at least 1ms
          then:
            - homeassistant.event:
                event: esphome.switch_double_click
                data:
                  device: "${deviceName}"
                  button: right
            - script.execute: blink_blue
        - timing:
            - ON for at least 500ms
          then:
            - homeassistant.event:
                event: esphome.switch_long_press
                data:
                  device: "${deviceName}"
                  button: right
            - script.execute: blink_blue_twice
5 Likes

The LH Red LED is connected directly to the relay switch, so it should act correctly. For some reason the RH Red LED is activated separately ( pin 22)

This is my config and it works as it should.

switch:
  - platform: gpio
    id: relay_1
    name: "light 1"
    pin: 23
  - platform: gpio
    id: relay_2
    pin: 19
    name: "light 2"  
  - platform: gpio
    id: relay_3
    pin: 22
    name: "$RH Red Light"
    
output:
  - platform: ledc
    pin: 18
    frequency: 1000 Hz
    id: pwm_output
 
light:
  - platform: monochromatic
    output: pwm_output
    name: "LEDs"
  - platform: status_led
    name: "Swtich state"
    pin:
      number: 0
      inverted: true

binary_sensor:
  - platform: gpio
    id: left_button
    pin:
      number: 4
      mode: INPUT_PULLUP
    on_press:
      - switch.toggle: relay_1
  - platform: gpio
    id: right_button
    pin:
      number: 15
      mode: INPUT_PULLUP
    on_press:
      - switch.toggle: relay_2
      - switch.toggle: relay_3

Thanks for this, i’m using it for my switch with some basic modifications.

I have a bit of trouble figuring out the best way to implement double click and long press into Home Assistant.
I’d like to have LB double click, LB long press, RB double click, RB long press buttons exposed to HA so i can use the buttons to toggle other lights or automations in my house.