Sonoff Ifan04 - ESPHome working code

I’m also thinking about changing the capacitors on the iFan04 (2,5µF and 3µF) by bigger ones : 10µF is the biggest I’ve found. It seems some people who had high speed issues solved the problem by changing to smaller ones. In my case, I need more speed.
What do you think ?

That’s a beast! This device is not rated for that.

House ceiling fans are between 20-80 watts.

1 Like

image

(Pinout the same for Adafruit BME280 & ENS160 + AHT21)
image

Yay! It’s Alive!

Can someone post (or at least post a link to) the latest and greatest known working code for just the iFan04-L that allows the buzzer to be disabled and has the proper speed ranges, please?

It would be nice to have a post of the definitive code for just the basic fan module and remote itself in one place.

And last (I think…) is the 3.3vdc power from an FTDI232 adapter enough to power the chip for flashing or do we need mains power and then use no 3.3vdc from the flashing adapter?

there seems to be a lot of branches to this thread and some (possibly?) unanswered questions about what works and what doesn’t.

Does anyone know what the differences are between the iFan03 and the iFan04 besides the different capacitors that prevents us from just using that code?

All your questions are answered in the thread above in chronological order. The newest stuff would be in that order.

Some fans act differently than others so it’s kinda subjective to what fan you have to how you have to trigger the relays but if it currently is too slow on MED etc then use the code below.

I have one fan that goes insanely fast if I enable the two MED/HIGH relays so test away and see what happens with yours.

If this happens and you need to customize the relays just fork my code base on github then modify the relay then in the yaml code change the github from mine to yours.

Only TTL USB Adapters with LDO regulators on the board.

If the remote doesn’t work right away and you never programmed the RF remote for the controller you will require an AC connection.

I have read this thread multiple times, there is none of this, it works. The only unanswered things are edge case stuff.

Not mentioned in any place in the thread but the RF remote sometimes doesn’t register it’s being pressed which you then have to press the button multiple times to wake it up. That is why I added the I2C I/O expander. I can just use the old light switch on the wall plugged into the I/O expander. The 120V direct to the controller with no light switch between to always keep the controller online to accept input. (Make sure your switch is separated from the power lines and doesn’t have 120V going through it)

There’s no difference besides the change of capacitor sizes to be more compatible with North America fans ie why the iFan04-L/H model exists. The circuit board was laid out differently to have more reliability but essentially it is identical in components. The only way this code wouldn’t work on the iFan03 is if they use different data pins on the ESP8266. If that is so then you would just have to modify those pins in the code to make it compatible and do the capacitor swap. You can compare the Pinout of the iFan03 here Sonoff iFan03 220V Light and Ceiling Fan Controller (IM190409021) Configuration for Tasmota with my post here which I confirmed with iTead the board manufacture Sonoff Ifan04 - ESPHome working code - #76 by NonaSuomy (Just checked both have the exact same pinout)

Note from the tasmota website about them:

The capacitors in the iFan03 do not set the speed of North America fans correctly. To correct the speeds for US ceiling fans, you need to remove the existing capacitors from the iFan03 and replace them with equivalently electrically rated 5uF (microfarad) capacitors. **Do NOT do this if you are not comfortable using a soldering iron as improperly performing this action could increase the risk of bodily injury or property damage.** 

Hope this helps.

1 Like

image

Latest YAML

Uncomment I2C stuff if you are playing with that.

ESPHome

# Primary Bedroom Fan
substitutions:
  name: ifan-bedroom-001
  friendly_name: Primary Bedroom

external_components:
  - source: github://nonasuomy/custom_components@master
    refresh: 0s
  - source: github://pr#4243
    components:
      - ens160

esphome:
  name: ${name}
  comment: Sonoff iFan04-L
  # This will allow for (future) project identification,
  # configuration and updates.
  project:
    name: cpyarger.sonoff-ifan04-l
    version: "1.0"

esp8266:
  board: esp01_1m

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

# Enable Home Assistant API
api:
  services:
    - service: fan_cycle
      then:
        - fan.cycle_speed: the_fan
  encryption:
    key: !secret encryption_key001

ota:
  password: !secret ota_pass001

uart:
  tx_pin: GPIO01
  rx_pin: GPIO03
  baud_rate: 9600
  debug:
    direction: BOTH
    dummy_receiver: false
    after:
      delimiter: "\n"
    sequence:
      - lambda: UARTDebug::log_string(direction, bytes);

# Define I2C device
# for an ESP8266/32 SDA is D2 and goes to Arduino's A4
#                   SCL is D1 and goes to Arduino's A5
#i2c:
#  id: i2c_component
#  sda: 4
#  scl: 5
#  scan: true

# PCF8575 light switch input
#pcf8574:
#  - id: 'pcf8574_hub'
#    address: 0x20
#    pcf8575: true

dashboard_import:
  package_import_url: github://cpyarger/esphome-templates/sonoff-ifan04-l.yaml@main

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: !secret use_address001
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ifan04-001 Fallback Hotspot"
    password: !secret fallbackhotspot001

web_server:

captive_portal:

# Define ENS160 + AHT21 Sensors
#sensor:
#  - platform: aht10
#    id: aht21_sensor
#    address: 0x38
#    temperature:
#      name: "AHT21 Sensor Temperature"
#      id: aht21_sensor_temp
#    humidity:
#      name: "AHT21 Sensor Humidity"
#      id: aht21_sensor_humi
#    update_interval: 55s

#  - platform: ens160
#    id: ens160_sensor
#    address: 0x53
#    eco2:
#      name: "ENS160 Sensor eCO2"
#      id: ens160_sensor_eco2
#    tvoc:
#      name: "ENS160 Sensor Total Volatile Organic Compounds"
#     id: ens160_sensor_tvoc
#    aqi:
#      name: "ENS160 Sensor Air Quality Index"
#      id: ens160_sensor_aqi
    # Inject the temp/humi data from the AHT21 into the ENS160 attached module for compensation.
    # Makes the gas results more accurate.
#    temperature: aht21_sensor_temp
#    humidity: aht21_sensor_humi
#    update_interval: 60s

binary_sensor:
  - platform: gpio
    id: button_light
    pin: GPIO0
    on_press:
      then:
        - light.toggle: fan_light
#  - platform: gpio
#    name: "Input_0"
#    pin:
#      pcf8574: pcf8574_hub
#      number: 0
#      mode: INPUT
#      inverted: true
    # Method 1: If the light is on turn it off, if the light is off turn it on, if another source changes the light  
    #           and the physical is in opposite state ignore and leave the current state.
#    on_press:
#      then:
#        - light.turn_on: fan_light
#    on_release:
#      then:
#        - light.turn_off: fan_light
    # Method 2: 3-Way Light Switch Emulation.
    #on_press:
    #  then:
    #    - light.toggle: fan_light
    #on_release:
    #  then:
    #    - light.toggle: fan_light
    # Method 2.1: Same as 2 just a different process. 
    # on_state:
    #  then:
    #    - light.toggle: fan_light

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

light:
  - platform: ifan
    id: fan_light
    name: "${friendly_name} Light"
  - platform: monochromatic
    id: led1
    output: led_pin
    default_transition_length: 0s
    restore_mode: always off

button:
  - platform: template
    name: ${friendly_name} Cycle Fan
    on_press:
      then:
        - fan.cycle_speed: the_fan

fan:
  - platform: ifan
    id: the_fan
    name: "${friendly_name} Fan"
    remote_enable: false
    buzzer_enable: false

ifan04:
  on_fan:
    - lambda: |-
          auto call = speed ? id (the_fan).turn_on() :  id (the_fan).turn_off();
          call.set_speed(speed);
          call.perform();
          ESP_LOGD("IFAN04", "speed is %d", speed);
  on_light:
    - light.toggle: fan_light
    - lambda: ESP_LOGD("IFAN04", "light trigger");
  on_buzzer:
    - lambda: ESP_LOGD("IFAN04", "buzzer trigger");

# Restart Device Switch
switch:
  - platform: restart
    name: "Primary Bedroom Fan Restart"

Home Assistant Card

type: grid
square: false
columns: 1
cards:
  - type: custom:mod-card
    card_mod:
      style:
        hui-vertical-stack-card $: |
          div#root > * {
            --ha-card-border-width: 0px;
          }
        .: |
          hui-vertical-stack-card {
            --vertical-stack-card-margin: 3px;
          }
          ha-card {
            background-color: rgba(50,50,50,0.4);
            box-shadow: var(--ha-card-box-shadow);
            border: var(--ha-card-border-color,var(--divider-color,#e0e0e0)) solid var(--ha-card-border-width,1px);
            --ha-card-header-color: white;
            --ha-card-background: rgba(50,50,50,0.4);
          }
    card:
      type: vertical-stack
      title: Primary Bedroom
      cards:
        - type: custom:mod-card
          card_mod:
            style:
              hui-horizontal-stack-card$: |
                hui-tile-card {
                  margin-left: 0px !important;
                  margin-right: 0px !important;
                  margin-bottom: 0px !important;
                  width: 150%;
                  flex: auto !important;
                }
                hui-button-card {
                  margin-left: 10px !important;
                  margin-right: 10px !important;
                  margin-top: 63px !important;
                  margin-bottom: 0px !important;
                  height: 43px;
                  width: 115px;
                  flex: auto !important;
                }
              .: |
                ha-card {

                }
          card:
            type: horizontal-stack
            cards:
              - type: tile
                entity: fan.primary_bedroom_fan
                name: Ceiling Fan & Light
                features:
                  - type: fan-speed
              - type: button
                entity: light.primary_bedroom_light
                name: Ceiling Fan Light
                show_state: false
                show_name: false
 #       - type: entities
 #         entities:
 #           - entity: sensor.aht21_sensor_temperature
 #             name: Temperature
 #           - entity: sensor.aht21_sensor_humidity
 #           - entity: sensor.ens160_sensor_air_quality_index
 #           - entity: sensor.ens160_sensor_eco2
 #           - entity: sensor.ens160_sensor_total_volatile_organic_compounds
1 Like

Hi All,

Thank you so much for your work on this :slight_smile:
I currently flashed: Sonoff Ifan04 - ESPHome working code - #28 by NigelHA

Before I install this in the fan and 3d print a few brackets and such I have this device:

Its a Tuya enabled thing and Noticed the following:

  • The ifan has 4 relays [lamp, low, med, high], The Lucci has 3 [lamp, low, med]? Any recommendations on how to control / copy this successfully ? ( I assume the “high” state combines the low + med but not sure?)

  • The Ifan has 2 caps of 2.5 and 3 uFJ, the luci has 2 of 2.0uFJ, should this concern me ?

Thanks in advance!

Why would anyone buy the lucci for that price when the iFan04 is less than 1/4th the price?

Do you have any internal pictures of the device for fun?

You may want to start your own thread about it.

Why be concerned if it was built for your power market I’m sure they did testing with those capacitor values. The iFan04 has two versions H and L with different capacitor values for each power market.

Well at that point in time (3 years ago) I did not know about HA and still wanting to have some control over it it seemed OK. Now its garbage obviously but I am sure not too many people are inclined to open up their fan controller and upload firmware… it has its place in the market…

Ill have to dig around I thought I did take a few…

Fair enough.

Well, I cannot imagine sonoff having all the fans to test, Nor am I willing to blow the fan / breaker while playing around with it, so better to double check the different values must be there for a reason one might assume…

Anyway I have to 3d print a extra spacer for this device as its too big to fit the current fan (another reason to buy the lucci I guess) so no rush :slight_smile:

3D print your own enclosure for it. There is a lot of wasted space on the edges as of its weird shape. Not sure how small the space is in your fan but I had to sand down the sides of mine to fit (posted above) Sonoff Ifan04 - ESPHome working code - #83 by NonaSuomy.

It took a while before I had the nerve to install it but here goes:
I ended p printing a spacer between the fan and the cieling (way too high, but ok)

Here it is all installed with WAGO connectors as that just feels way more safe than te screw terminals

For the curious, here the old LUCCI fan controller: (which is now going into the trash JEEJ)


1 Like

Before I forget, What is currently the latest “Working YAML” for ESP home ?

Thanks for the pictures. Instead of throwing it out… If you were able to get custom firmware on it you could technically use it for a table fan or a bathroom fan etc if it still works.

1 Like

I produced what I think is a tidy .yaml-only iFan04-L package:

bedroom-fan.yaml (example)

api:
  encryption:
    key: !secret encryption_key

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

esphome:
  name: bedroom-fan
  friendly_name: "Bedroom Fan"

packages:
  ifan04: github://gagebenne/esphome/packages/ifan04.yaml # !include ifan04.yaml (locally)

That’s it. No other dependencies. The remote works fine. There’s some beeps too if you want. Light works.

If you dislike downloading the package from GitHub, downloading the single .yaml file is just as easy, using !include ifan04.yaml instead.

6 Likes

I just wanted to chime in here since this thread is the first result on google and has been extremely helpful to me. I had trouble getting my ifan04 in programming mode and wanted to post a solution for anyone else that may visit this thread. The ifan04 would power up fine, but when holding gpi0, I would get nothing. My serial flasher (ch340g) would blink the transmit light but never receive. I bought a FT232 and better jumpers since I had been trying to use some thin breadboard cables and still nothing changed.

The solution was powering the ifan with one programmer and flashing with the other.

Ft232 TX/RX to Ifan04 RX/TX
CH340G VCC to Ifan04 3.3v
All 3 grounds connected together.

I saw that some in this thread were able to flash it by connecting it to mains but I wanted to post an alternative that is a little more safe. I also could not get this to work from the web flasher but flashing from the server did.

1 Like

You just need a TTL board with a proper regulator on it which is covered above.

The AC bit is normally only for programming the RF remote if it is not already synced with the fan controller.

Great work!
Will test soon, does your remote respond better now?
Mine is very intermitted clicking with the old way. (Sometimes having to click it 5+ times to get it to respond, even though the battery seems to be in good condition and the led emits on the remote ok)

esphome AttributeError: ‘NoneType’ object has no attribute ‘replace’
You seem to be missing a name: “” for the fan speed-setting entity.

fan:
  - platform: speed
    id: ifan04
    name: ${friendly_name} Fan

The remote seems to be even less responsive like it needs a cooldown before the next button press and multiple clicking as well not sure why or if you get the same response.

Seem to have instances as well where I click something and it does something random. I started by clicking speed 1 and it started on 3, I pressed speed 2 and it turned the light off. I’m not sure if the speed cycling is just an error with fan control as I was having that issue before as well.

There’s definitely a cooldown for the remote for me as well, I think that I guess is just the downside of .yaml-only configuration? Others in this thread have explored more robust remote handling, but then you get into C++ files, and I guess I don’t utilize the remote enough to desire that.

Also, strange that the empty string name doesn’t work. My .yaml is as follows and compiles fine:

esphome:
  name: bedroom-fan
  friendly_name: "Bedroom Fan"

packages:
  common: !include common.yaml
  ifan04: github://gagebenne/esphome/packages/ifan04.yaml

So, in Home Assistant the entities are Bedroom Fan (the fan) and Bedroom Fan Light (light). Otherwise the entity names get repetitive (Bedroom Fan Fan).

Hi there, I hope that someone can help me out.

This is all new to me but I’ve successfully flashed my iFan04-H with Tasmoto 13.0.0. Thanks to you people!!!

Unfortunately the fan is spinning to fast (rotations) for me in preset 1 (LOW). It is not comfortable. I would like to use a percentage-slider (custom setting) to slow it down to let say 20% of maybe even 15% (considering that LOW = 33%). I will never use preset MEDIUM of HIGH.

I’m hoping to be able to do so by using ESPHome. So i tried installing/flashing my iFan to ESPHome. But what ever I try, the flashing itself works (progress bar is showing 100% and the end messages are hopeful).

After flashing I always end up with the error message: An error occurred. Improv Wi-Fi Serial not detected. Then the iFan is unreachable, no connection to WiFi and no little blue light on the board or clicks on booting etc…

I tried:

  1. On my Windows 10 laptop: Flashing to ESPHome from Tasmoto webui (IP address of the iFan, than option ‘Firmware upgrade’
  2. On my Windows 10 laptop: Use Tasmotizer to install the bin file from HA.
  3. Install the ESPHome Add-on in HA and then try to add a new device via de webuser UI.
  4. Attachted the TTL directly on my HA device (Mac mini) and then try to install the bin-file from HA

To be sure I have HA up and running with MQTT and Tasmoto. Both working great. Also the remote of the iFan is working great. So that would be enough (no need for ESPHome), when the fan would spin/rotate less…

I once had ESPHome add-on installed on HA and then it discovered the iFan (something like 'ESP+last digits of the mac address), that is where I got the bin-file from. But whatever I try, ESPHome does not find the iFan anymore and I’m able to add a device manually…

Can someone help me?

Thank you in advance.

Review my posts above on flashing from the esptools and make sure your serial dongle has a regulator on it.