How to make a Sonoff Mini R2 act as smart relay?

I have a couple Sonoff Mini R2 relays. I’d like to flash them with ESPHome, put them behind conventional light switches and use them alongside smart bulbs.

In normal operation, I’d like them to remain closed and use the input from the light switches to send commands to the smart bulbs (instead of actually breaking the circuit).

I’d also like to have a default fallback, so that if the relays can’t connect to Home Assistant, they open and close along with the light switch input.

I see that the Sonoff Mini is on the ESPHome Devices site, but not the Mini R2.

First, does anyone have a GPIO pinout for the Sonoff Mini R2, so I might be able to adapt the suggested config files on the ESPHome Devices site?

Second, could someone please help me translate my intentions above into an ESPHome config file for the Mini R2?

Thanks in advance.

Here’s the I/O snippit of my Mini R2 code.

status_led:
  pin:
    number: GPIO13
    inverted: true

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

light:
  - platform: binary
    id: light_1
    name: ${friendly_name} Light
    output: relay_1    

binary_sensor:
  - platform: gpio
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - light.toggle: 
          id: light_1


# This GPIO is the S1/S2 switch contact.
# Toggle the output relay on every state change of this input (IE Off->On & On->Off)
  - platform: gpio
    pin: GPIO04
    id: switch_1
    name: ${friendly_name} Light Switch
    on_press:
      then:
        - light.toggle: 
            id: light_1
    on_release:
      then:
        - light.toggle: 
            id: light_1

That code will just toggle the relay with the switch contacts.

To do what you’re asking, I’d probably use the “Connected” binary sensor, then use it to gate the switching of the relay.

The code below is a sample mockup, and HAS NOT BEEN TESTED. It’s just to give you an idea.

binary_sensor:
  - platform: status
    name: ${friendly_name}  Status
    id: ha_connected_status

# This GPIO is the S1/S2 switch contact.
# Toggle the output relay on every state change of this input (IE Off->On & On->Off)
# Only toggle relay is HA connection status is "Disconnected
  - platform: gpio
    pin: GPIO04
    id: switch_1
    name: ${friendly_name} Light Switch
    on_press:
        then:
          - if:
              condition: # only toggle relay if HA is disconnected
                lambda: 'return (id(ha_connected_status).state == "Disconnected");'
              then:
                - light.toggle: 
                    id: light_1
    on_release:
        then:
          - if:
              condition: # only toggle relay if HA is disconnected
                lambda: 'return (id(ha_connected_status).state == "Disconnected");'
              then:
                - light.toggle: 
                    id: light_1

1 Like

Sounds like “detached mode” :point_down:

You find the GPIO used on the right side here :point_down:

When I started out I used this article by Brian Hanifin and just changed the GPIO numbers to do what you are asking. It worked pretty well but I eventually got rid of those bulbs and just went with dumb bulbs. You can find the config modified for Sonoff Mini R2 in my gists… I haven’t updated it in 2 years, so you will definitely need to change some things around.

1 Like

These are all incredibly helpful comments, thank you.

I’ve been reading over these instructions to prepare, because my understanding is I need to create some sort of binary file via ESPHome in HA, which I will eventually upload to the Tasmota-flashed Mini R2.

I started fiddling with the ESPHome integration in HA and tried to create a configuration for the Mini R2, but got held up by the “Select device type”. What type of board should I select here?

You can use ESP8266 or Espressif Generic esp01_1m.

1 Like

As Drew said, esp01_1m is the right choice, however which ESP8266 board you choose at the screen isnt terribly important. All that option screen does automatically generate some of the configuration for you.

Selecting that board will add something like this to the configuration

esp8266:
  board: esp01_1m

Which you can always change later if needed.

That guide seems a bit overly complicated, especially if you already have tasmota flashed onto it. That guide seems to be for flashing it from the original firmware.

You just need to build your ESPHome firmware, and in the ESPHome page click “Install”, choose “Manual Download”, then “Legacy Format”. It will then compile the code and download a .BIN file to your browser.

Once you have the BIN file, power on the Tasmota device and connect to it’s IP and choose Firmware Upgrade and choose the BIN file from your computer.
ESPHome also has a (very short) guide to migrating.

One caveat is that if you have Tasmota firmware 7.2 or higher on the device, it does a FW verification before flashing, so will fail on ESPHome binary files. To disable this check is easy though.
Open the Console in the Tasmota device webpage, then run the command “SetOption78 1” in the Tasmota console, reboot/restart your device, then you can “upgrade” to ESPHome.

1 Like

I’ve successfully flashed the Mini R2 and used @Didgeridrew’s config as a jumping-off point. Looking at it again, I think that config was written for a momentary switch, or else I can’t understand why there aren’t any automations defined for turning the switch to OFF. I’m going to have to tweak it a little more, perhaps using @JWilson’s suggestion above.

While I’m working on that: I’m planning to put the R2 behind a normal rocker switch, and I’d love to add a little functionality behind “double-clicking” it on (rapidly flipping ON-OFF-ON). This thread on a similar subject suggests using binary sensors.

Does anyone happen to have any examples of pulling this all together—the API connectivity check, plus different actions on a single switch-ON versus a “double-click”? This is my first ESPHome project, so any examples could help me get on the right track.

Thanks for all the help!

I used it with a standard in-wall rocker switch. The conditional logic and functions for turning the relay off are handled in the script section which starts on line 56 in the Gist.

Ah! I missed that on_state triggered on any change in state. Thanks.