esphome / esphome-core

🚨 No longer used 🚨 - The C++ framework behind ESPHome
https://esphome.io/
GNU General Public License v3.0
545 stars 113 forks source link

Festnight Sonoff Smart Switch Wifi (Sonoff Basic clone) #343

Closed Styx85 closed 5 years ago

Styx85 commented 5 years ago

What new integration would you wish to have? I don't know if new integration is the right thing, but I'd like to report the experience. If I get some guidance I can also write a short manual for the module.

I recently bought a Sonoff Basic Smart Switch clone (Amazon). It differs from the original Sonoff that it is Tuya/Smart Life based. Also flashing the board is a bit different.

If possible, provide a link to an existing library for the integration: All things are set in esphomelib.

Please describe your use case for this integration and alternatives you've tried: I just bought this aforementioned clone. There are sone Problems you have to think about.

Additional context: There is a different PIN-out, like already reported elswhere for Sonoff clones (PowStro and SmartHome): Relay is GPIO4 Status LED is GPIO13 First LED (blue) is GPIO15 Button(hardware switch) is GPIO12

I used following template to get it up and running. The relay, button and the blue LED are working, the red one is dimmable but I couldn't find the trick so far to turn it off completely:

## Quick GPIO reference
## https://esphomelib.com/esphomeyaml/devices/sonoff_basic.html
#
status_led:
  ## https://esphomelib.com/esphomeyaml/components/status_led.html
  pin: GPIO13

sensor:
  ## https://esphomelib.com/esphomeyaml/components/sensor/uptime.html
  - platform: uptime
    name: "esp_xxx_system_uptime"
    id: esp_xxx_system_uptime

  ## https://esphomelib.com/esphomeyaml/components/sensor/wifi_signal.html
  - platform: wifi_signal
    name: "esp_xxx_system_wifi_signal"
    id: esp_xxx_system_wifi_signal
    update_interval: 15s

## https://esphomelib.com/esphomeyaml/guides/automations.html
## switch - use build in API functions to manage local device pins without mqtt

light:
  # Status LED 1 (red)
  - platform: monochromatic
    name: "esp_xxx_gpio13"
    output: esp_xxx_gpio13

  # Status LED 2 (blue)
  - platform: monochromatic
    name: "esp_xxx_gpio15"
    output: esp_xxx_gpio15

  ## OUT 1 - (Relay)
  - platform: binary
    name: "esp_xxx_gpio04"
    id: esp_xxx_gpio04
    output: output_esp_xxx_gpio04

binary_sensor:
  ## https://esphomelib.com/esphomeyaml/components/binary_sensor/status.html
  - platform: status
    name: "esp_xxx_system_status"
    id: "esp_xxx_system_status"

  ## button - toggle relay
  - platform: gpio
    name: "esp_xxx_gpio12"
    pin:
      number: GPIO12
## set as inverted to be off by default
#      inverted: true
      mode: INPUT_PULLUP
    on_press:
      then:
        - light.toggle: esp_xxx_gpio04

## relays used as lights
## set as invertered to be on by default, it messes with the HA-Light component (ON means OFF; OFF means ON)
output:
  - platform: gpio
    id: "output_esp_xxx_gpio04"
    pin:
      number: GPIO04
#      inverted: true

  # Register the blue LED as a dimmable output ....
  - platform: esp8266_pwm
    id: esp_xxx_gpio15
    pin: GPIO15
    inverted: true

  # Register the red LED as a dimmable output ....
  - platform: esp8266_pwm
    id: esp_xxx_gpio13
    pin: GPIO13

Than version 1.9.0 to 1.9.3 is not working, the clone becomes unresponsive. To be fair I only tested version 1.9.0 and 1.9.3 and no versions in between. Regarding the tip by @OttoWinter in Issue #340 bleeding edge/dev version with the mentioned wifi changes is running stable now for three hours, but is breaking OTA for my Wemos D1 mini PIR #353. Version 1.8.2 is working flawlessly, but the old OTA method is a pain.

Another thing I tested was the Sonoff basic instruction from the manual, but with that configuration OTA updates crashes reproducible. Maybe it is not that good an idea to have GPIO4 as INPUT_PULLUP (it really isn't in the aforementioned configuration of the switch). That maybe blocked the ESP or it turned into a bootloop. But I think with ne now correct pinout it should work (not tested).

binary_sensor:

  • platform: gpio pin: number: GPIO12 mode: INPUT_PULLUP inverted: True name: "Sonoff Basic Button"
  • platform: status name: "esp_xxx_system_status" id: "esp_xxx_system_status"

switch:

  • platform: gpio name: "Sonoff Basic Relay" pin: GPIO04

output:

  • platform: esp8266_pwm id: basic_blue_led pin: number: GPIO15

  • platform: esp8266_pwm id: red_status_led pin: number: GPIO13
    light:

  • platform: monochromatic name: "Sonoff Basic Blue LED" output: basic_blue_led

  • platform: monochromatic name: "Sonoff Red Status LED" output: red_status_led

meijerwynand commented 5 years ago

Greetings,

I was able to get my Sonoff devices stable by installing and defining each versions and then update them version by version see update on issue.

If possible, use the method describe here and check if that improves / changes the stability for you devices.

Styx85 commented 5 years ago

And some pictures for the ones interested.

img_20181231_162858 img_20181231_150635 wire 2019-01-01 at 16 09 36 wire 2019-01-01 at 16 09 42 bildschirmfoto von 2019-01-02 20-37-51

Styx85 commented 5 years ago

Greetings,

I was able to get my Sonoff devices stable by installing and defining each versions and then update them version by version see update on issue.

If possible, use the method describe here and check if that improves / changes the stability for you devices.

Thanks for the info @meijerwynand , I switched to bleeding edge for my sonoff clone and that one is running flawlessly so far but breakung OTA for my Wemos D1 mini.

BTW, have a look at my modified yaml. I took yours for inspiration and modified the "button" part.

brandond commented 5 years ago

What the heck do you have going on with the relay configured as a light!? I think what you want is to use a template switch to tie the button and LED together. Try this (change your board/ssid/broker):

esphomeyaml:
  name: sonoff
  platform: ESP8266
  board: esp01_1m
  arduino_version: 2.4.2
  esphomelib_version: dev

wifi:
  ssid: 'ssid'
  password: 'password'

mqtt:
  broker: 'broker'
  discovery_prefix: homeassistant
  username: ''
  password: ''

logger:
  level: DEBUG

debug:

ota:

sensor:
  - platform: wifi_signal
    name: "Sonoff WiFi Signal"
    filters:
      - sliding_window_moving_average:
          window_size: 4
          send_every: 4

switch:
 - platform: gpio
   id: switch0
   pin:
     number: 4
     mode: OUTPUT
 - platform: template
   id: template0
   icon: "mdi:power-plug"
   name: "Sonoff Power"
   restore_state: yes
   turn_off_action:
     - light.turn_off:
         id: led0_light
     - switch.turn_off:
         id: switch0
   turn_on_action:
     - light.turn_on:
         id: led0_light
     - switch.turn_on:
         id: switch0
   lambda: "return id(switch0).state;"

binary_sensor:
  - platform: gpio
    id: button0
    pin:
      number: 12
      mode: INPUT
    on_press:
      then:
        - switch.toggle:
            id: template0

status_led:
  pin:
    number: 13
    mode: OUTPUT
    inverted: true

output:
  - platform: esp8266_pwm
    id: led0_output
    pin:
      number: 15
      mode: OUTPUT
      inverted: true

light:
  - platform: monochromatic
    id: led0_light
    output: led0_output
    default_transition_length: 0.5s
meijerwynand commented 5 years ago

@Styx85

I switched to bleeding edge for my sonoff clone and that one is running flawlessly so far but breakung OTA for my Wemos D1 mini.

I am running a Wemos D1 Mini Pro on v1.9.3 without any issue. However after copy/paste from other configs I noticed I flashed the Wemos possibly incorrect (but it's functional and stable for almost a week now). That includes a functional OTA.

The "invalid" but functional Wemos config - I have the board as nodemcuv2

esphomeyaml:
  name: !secret 5ccf7f2c0f45
  platform: ESP8266
  board: nodemcuv2
  esphomelib_version: 1.9.3

@brandond

What the heck do you have going on with the relay configured as a light!?

Thank you so much for the config you posted. I will surely be able to better my configs and make a more appropriate use of the template options/features.

The initial reasoning for using the relay as a light was:

  1. There is a lamp connect to the sonof's relay
  2. The HA UI looks and works better with the light component
  3. I am still learning all the in's and out's

Your submission of configs as above really helps out, it is much appreciated.

Styx85 commented 5 years ago

What the heck do you have going on with the relay configured as a light!? I think what you want is to use a template switch to tie the button and LED together. Try this (change your board/ssid/broker):

Well I integrated these simple relays in my wall mounted lights and want them to behave as such. I personally don't think it matters if you configure the switch as light directly in the esp or in home assistant.

One thing I like to have is that the light just turns on if someone uses the regular wall mounted switch, therefore I integrate some boot command. That just works and with the given priority is independent from wireless and mqtt connection. So before it becomes smart its a simple light ;)

  on_boot:
    priority: 50
    then:
      - light.turn_on: esp_xxxl_relay
brandond commented 5 years ago

Here's what I ended up using for a Shelly 1, which is similar but lacks a LED. If you set up a switch and use icon: mdi:light a switch and a binary light are essentially the same thing except for appearing in a different domain in HA.

esphomeyaml:
  name: fixture00
  platform: ESP8266
  board: esp01_1m
  arduino_version: 2.4.2
  esphomelib_version: dev
  board_flash_mode: dout
  build_path: esp8266
  on_boot:
    if:
      condition:
        binary_sensor.is_on: switch0
      then:
        light.turn_on: relay0
      else:
        light.turn_off: relay0

wifi:
  ssid: 'x'
  password: 'x'
  fast_connect: True

mqtt:
  broker: '10.0.1.27'

logger:
  level: DEBUG

debug:

ota:

sensor:
  - platform: wifi_signal
    name: "Fixture 00 WiFi Signal"
    filters:
      - sliding_window_moving_average:
          window_size: 4
          send_every: 4

output:
  - platform: gpio
    id: relay_output
    pin:
      number: 4
      mode: OUTPUT

light:
  - platform: binary
    name: "Fixture 00 Light"
    id: relay0
    output: relay_output

binary_sensor:
  - platform: gpio
    id: switch0
    pin:
      number: 5
      mode: INPUT
    on_state:
      then:
        light.toggle: relay0
OttoWinter commented 5 years ago

Closing because this doesn't seem to be an issue, just documentation on how to set up a device.