Sonoff Ifan04 - ESPHome working code

I don’t believe so - it worked first time for me. Note I do not use the remote so can’t comment on that.

1 Like

My USB - TLL adapter came in and I took your Yaml and updated it with my own information. When compiling I get this error. Has anyone seen it before?

Have you confirmed the yank is valid?

This is what kept if from being valid. I currently have it commented out and it validated and loaded fine. I noticed it was from the section that translates the remote actions back to HA so I figured I could go without it for a while. For some reason it was hanging up when others seemed to get it easily (IE you haha)

sorry what i mean is run your yaml file through a yaml validator

Yep, I validated it. It checks out when I comment out the bottom section.

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
1 Like

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

I believe we need a generic iFan external component now, because custom components are deprecated which all existing configs for these devices use.

Correct, my latest post with code already is migrated towards it and I am sure if you scroll further up you will find more examples that already are migrated :slight_smile:

Hello, I’m using the same flasher as you and wired up the same but for some reason i can’t connect. When do you press the button and for how long? It’s the button next to where we wire in correct?

The button you need to press for flashing is circled in the below photo:

sonoff

You need to be holding it down as you are plugging the flasher into the USB and first applying power to the board, keep holding it after connecting it to the flasher for a few seconds, and then release.

I just flashed one of these, and when I didn’t hold the button down, it was beeping at me when I connected it, which I believe is some sort of alarm that is in place to alert to potential flashing without the button being held. Having to hold the button as far as I know is a security feature to prevent installation of firmware without the user’s knowledge. Once ESP is flashed you can then apply updates over the air without needing to hold the button down.

I held down the button but it just keeps circling on connecting but never connects. I did it once with tx to rx and rx to rx and once with tx to tx and rx to rx. Did you have it powered with 120v as well while flashing?

No only connected to the power of the usb to uart else things might break.

1 Like

I ordered two more from aliexpress since the first one works so nicely but now I’m having issues flashing using the exact same setup. It seems to not be powering the board or something. Does anyone have any advice?

My repo name was a historical artifact and became misleading. It has been renamed, but github will still redirect the old name. The new name is esphome_components.