Sonoff Ifan04 - ESPHome working code

I’m also having the same issue where I’m getting a “successful” install but when it tries to connect I get: An error occurred. Improv Wi-Fi Serial not detected. I’m using the same USB to TTL adapter @haydon is using. Have folks soldered on the leads to the board? I have clips right now.

I soldered to mine, don’t have clips…

I’m not sure why you’re getting that error though, I wonder if the code has changed…

1 Like

Ok, I found this and it worked! New device: An error occurred. Improv Wi-Fi Serial not detected - #15 by Vahaldor

Yesterday I got fan NR2 in the house:

The entire installation went smooth :slight_smile: glad that annoying beep is gone.

The only thing I had to remember is when and for how long to hold the boot button

I reused the entire code of the old fan and changed a few wifi things.
Here is the code again so a new person does not have to scroll too far :slight_smile: :

substitutions:
  name:  ifan04-h-office
  device_description: Sonoff iFan04-H
  friendly_name: ifan04-h-office

globals:
  - id: target_fan_speed
    type: int
  - id: start_time_offset
    type: int

esphome:
  name: ${name}
  comment: ${device_description}
  platform: ESP8266
  board: esp01_1m
  on_boot:
    priority: 225
    # turn off the light as early as possible
    then:
      - light.turn_off: fan_light
  on_loop:
    lambda: |-
      if (id(start_time_offset) && (millis() > id(start_time_offset))) {
        ESP_LOGD("IFAN04", "Setting target speed: %d", id(target_fan_speed));
        auto call = id(the_fan).turn_on();
        call.set_speed(id(target_fan_speed));
        call.perform();
        id(start_time_offset) = 0;
      }

# Disable logging on serial as it is used by the remote
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: !secret ifan_api_2

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret lot_wifi_ssid
  password: !secret lot_wifi_password
  fast_connect: true
  power_save_mode: light
  use_address: !secret ip-fan-moff
  ap:
    ssid: "Ifan04-H-Office"
    password: !secret ifan_fallback
captive_portal:

external_components:
  - source: github://ssieb/custom_components
    components: [ ifan04 ]

uart:
  tx_pin: GPIO01
  rx_pin: GPIO03
  baud_rate: 9600

ifan04:
  on_fan:
    - lambda: |-
        if (speed) {
          auto call = id(the_fan).turn_on();
          call.set_speed(speed);
          call.perform();
          if (id(buzzer_dummy).state) {
            switch(id(target_fan_speed)) {
              case 3:
                id(buzzer_pin).turn_on();
                delay(50);
                id(buzzer_pin).turn_off();
                delay(50);
              case 2:
                id(buzzer_pin).turn_on();
                delay(50);
                id(buzzer_pin).turn_off();
                delay(50);
              case 1:
                id(buzzer_pin).turn_on();
                delay(50);
                id(buzzer_pin).turn_off();
            }
          }
        } else {
          id(target_fan_speed) = 0;
          id(start_time_offset) = 0;
          auto call = id(the_fan).turn_off();
          call.perform();
        }
  on_light:
    - light.toggle: fan_light
  on_buzzer:
    - switch.toggle: buzzer_dummy

sensor:
  - platform: wifi_signal
    name: ${friendly_name} Signal
    update_interval: 60s

text_sensor:
  - platform: version
    hide_timestamp: true
    name: "${friendly_name} ESPHome Version"
    entity_category: diagnostic
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"
      icon: mdi:wifi
      entity_category: diagnostic
    ssid:
      name: "${friendly_name} Connected SSID"
      icon: mdi:wifi-strength-2
      entity_category: diagnostic

binary_sensor:
  - platform: gpio
    id: button
    pin: GPIO0
    on_press:
      then:
        - light.toggle: fan_light

interval:
  - interval: 500ms
    then:
      - if:
          condition:
            not:
              wifi.connected:
          then:
            - light.turn_on:
                id: led1
                brightness: 100%
                transition_length: 0s
            - delay: 250ms
            - light.turn_off:
                id: led1
                transition_length: 250ms

output:
  - platform: template
    id: fanoutput
    type: float
    write_action:
      - lambda: |-
          if (state) {
            int speed = int(state / 0.33);
            if (!id(target_fan_speed) && (speed < 3)) {
              ESP_LOGD("IFAN04", "Fan currently off, boosting speed");
              id(target_fan_speed) = speed;
              state = 1.0;
              id(start_time_offset) = millis() + 5000; // 5 second delay
            } else {
              id(start_time_offset) = 0;
              id(target_fan_speed) = speed;
            }
          }
          if (state < 0.3) {
            // OFF
            id(target_fan_speed) = 0;
            id(start_time_offset) = 0;
            id(fan_relay1).turn_off();
            id(fan_relay2).turn_off();
            id(fan_relay3).turn_off();
          } else if (state < 0.6) {
            // low speed
            id(fan_relay1).turn_on();
            id(fan_relay2).turn_off();
            id(fan_relay3).turn_off();
          } else if (state < 0.9) {
            // medium speed
            id(fan_relay1).turn_off();
            id(fan_relay2).turn_on();
            id(fan_relay3).turn_off();
          } else {
            // high speed
            id(fan_relay1).turn_off();
            id(fan_relay2).turn_off();
            id(fan_relay3).turn_on();
          }

  - platform: gpio
    id: light_relay
    pin: GPIO9
    inverted: true

  - platform: gpio
    id: buzzer_pin
    pin: GPIO10
    inverted: true

  - platform: esp8266_pwm
    id: led_pin
    pin: GPIO13
    inverted: true

light:
  - platform: binary
    id: fan_light
    name: "${friendly_name} Light"
    output: light_relay

  - platform: monochromatic
    id: led1
    output: led_pin
    default_transition_length: 0s
    restore_mode: always off

switch:
  - platform: restart
    name: "${friendly_name} Restart"

  - platform: template
    id: buzzer_dummy
    name: "Buzzer"
    optimistic: True

  - platform: gpio
    id: fan_relay1
    pin: GPIO14

  - platform: gpio
    id: fan_relay2
    pin: GPIO12

  - platform: gpio
    id: fan_relay3
    pin: GPIO15

fan:
  - platform: speed
    id: the_fan
    name: "${friendly_name} Fan"
    output: fanoutput
    speed_count: 3

Hi there. When using your YAML I am getting this error. This is only my 2nd esphome device so I am a relative newbie. Any advice?

ERROR Error while reading config: Invalid YAML syntax:
Secret ‘ifan_api_2’ not defined
in “/config/esphome/sonoff-fan-jeff-office.yaml”, line 39, column 10

You need to create this secret in the secrets file then it will compile for you

(post deleted by author)