Has anyone got a Sonoff Mini Code that you could share?

You don’t need to pass by Tasmota, you can prepare a esphome bin, compile, download binary and upload it through the DIY interface. I did it for my 12 sonoff mini in my home :slight_smile:

1 Like

I used the same method created a mini bin file with only wifi, uploaded that in DYI mode, then OTA for the rest.

@dewgenenny it seems entirely possible, there are loads of free GPIOs.

What kind of up/down switch are you talking about?

Can someone share the code and how to, if I want to flash esp home to it while still using the physical button of the light switches? Thx!

This code will turn the connected devices on when power is provided to the Mini. It should do what you wan. The trick is the restore_mode: restore_default_on command:

esphome:
  name: $hostname
  platform: ESP8266
  board: esp8285

###change host name to desired unique name
substitutions:
  hostname: "sonoff_mini_01"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pwd

logger:

api:

ota:

status_led:
  pin:
    number: GPIO13
    inverted: true

binary_sensor:
  - platform: gpio
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - delayed_off: 10ms
    on_press:
      - switch.toggle: 
          id: relay_01

  - platform: gpio
    name: "$hostname switch"
    pin: GPIO04
    id: switch_01
    on_press:
      then:
        - switch.turn_off:
            id: relay_01
    on_release:
      then:
        - switch.turn_on:
            id: relay_01

switch:
  - platform: gpio
    name: "$hostname relay"
    icon: "mdi: lightbulb_outline"
    pin: GPIO12
    id: relay_01
    restore_mode: restore_default_on
1 Like

I think perfect setup should be done with binary sensor for GPIO4 to be:

  - platform: gpio
    name: "$hostname switch"
    pin: GPIO04
    id: switch_01
    on_press:
      then:
        - switch.toggle:
            id: relay_01
    on_release:
      then:
        - switch.toggle:
            id: relay_01

trick is to use switch.toggle in both on_press and on_release. This way you can achive 2 way switch between homeassistant and physical switch.

On_state covers both on_press and on_release

Didn’t work for me. I also needed to flash with Tasmota first. esphome wouldn’t compile a dout compatible firmware the DIY interface would take.

Yes, toggle for both on_press and on_release worked for me; the use case is a standard light switch. My code is:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: true
    name: "Office Wall Switch"
    on_press:
      - switch.toggle: relay
  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "Office Wall Switch"
    on_press:
      - switch.toggle: relay
    on_release:
      - switch.toggle: relay
switch:
  - platform: gpio
    name: "Office Wall SW Relay"
    pin: GPIO12
    id: relay

status_led:
  pin:
    number: GPIO13
    inverted: true

Hi, you can ditch the relay type configuration. No one wants a relay in HA where you should have a light, usually you want a light, right?
This config (I adopted it everywhere) uses a GPIO output as an internal relay, and a binary light that is then passed to HA.

esphome:
  name: sonoff_mini_cucina
  platform: ESP8266
  board: esp01_1m

wifi:
  networks:
  - ssid: 'YOURSSID'
    password: 'YOURPASS'
  - ssid: 'YOURBACKUPSSID'
    password: 'YOURBACKUPASSWD'
#  manual_ip:
#    gateway: 192.168.1.254
#    subnet: 255.255.255.0
#    static_ip: 192.168.1.218

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: 'Sonoff Mini Cucina'
    password: 'x3LQqBHyNYGh'

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

status_led:
  pin:
    number: GPIO13
    inverted: true

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

light:
  - platform: binary
    id: light_1
    name: 'Luce cucina'
    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

  - platform: gpio
    name: 'Interruttore luce cucina'
    pin: GPIO04
    id: switch_1
    on_press:
      then:
#        - switch.turn_on:
        - light.toggle: 
            id: light_1
    on_release:
      then:
#        - switch.turn_off:
        - light.toggle: 
            id: light_1
3 Likes

Is the board type not important? I see some people are using esp01_1m and some are using esp8285?

I think it’s trivial, however I created this code via ESPHome Dashboard, so it should be good. I updated everything to latest version and it’s still good today.

Ok thanks, still learning :slight_smile:

Is it possible to use the GPIO16pin as data pin for aan PIR sensor? 3v and ground are available.
Thnxs!

Take a look here https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/

EDIT - looks like it can be https://templates.blakadder.com/sonoff_mini.html and https://esphome-configs.io/devices/sonoff-mini-relay/

I dropped this onto my sonoff Basic via Arduino IDE and talk to it over MQTT

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

// Update these with values suitable for your network.
const char* ssid = “insert SSID here”;
const char* password = “password”;
const char* mqtt_server = “ip address of MQTT”;

WiFiClient espClient;
PubSubClient client(espClient);
String switch1;
String strTopic;
String strPayload;

const int switchPin1 = 12;
const int ledpin = 13;

char const* switchTopic1 = “/sonoff1/switch1/”;
//
//
//
int LEDState=0;
int buttonPin=0;
int buttonNew;
int buttonOld=1;
int dt=100;

void setup_wifi() {

delay(10);
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
}

void callback(char* topic, byte* payload, unsigned int length) {

String topicStr = topic;

if (topicStr == “/sonoff1/switch1/”)
{

 //turn the switch on & light the LED if the payload is '1' and publish to the MQTT server a confirmation message
 if(payload[0] == '1'){
   digitalWrite(switchPin1, HIGH);
   digitalWrite(ledpin, LOW);
   client.publish("/sonoff1/switchConfirm1/", "1");
   }

  //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
 else if (payload[0] == '0'){
   digitalWrite(switchPin1, LOW);
   digitalWrite(ledpin, HIGH); 
   client.publish("/sonoff1/switchConfirm1/", "0");
   }
 }    

}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“sonoff1”)) {
Serial.println(“connected”);
// Once connected, publish an announcement…
client.subscribe("/sonoff1/#");
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

pinMode(buttonPin, INPUT);
pinMode(switchPin1, OUTPUT); // Relay Switch
pinMode(ledpin, OUTPUT); // Indicator LED
digitalWrite(switchPin1, LOW);
digitalWrite(ledpin, HIGH);

}

void loop()
{
buttonNew=digitalRead(buttonPin);
if(buttonOld==0 && buttonNew==1){
if (LEDState==0){
digitalWrite(ledpin,HIGH);
LEDState=1;
digitalWrite(switchPin1, LOW);
}
else{
digitalWrite(ledpin, LOW);
LEDState=0;
digitalWrite(switchPin1, HIGH);
}
}
buttonOld=buttonNew;
delay(dt);
if (!client.connected()) {
reconnect();

}
client.loop();
}

How can I add option to use double press to trigger another light that is connected to HA but not to this esphome?

This is code from my another ESPhome instance where I use single switch to trigger 2 different relays. But both relays are connected to the same ESP.

  - platform: gpio
    name: "fan_button"
    pin: D8
    on_double_click:
      min_length: 50ms
      max_length: 450ms
      then:
        - switch.toggle: r3

But now I need an option that single switch would turn on the light, but double press would turn on the fan that is using complettely different instance in HA.

I think it should work with the template binary sensor:

on_...:
  - binary_sensor.template.publish:
      id: template_bin
      state: ON

Look at this page

And this part in particular

homeassistant.service Action

It looks like you can change the name in other parts of the code. May I ask where?
Thank you so much!