Sonoff Ifan04 - ESPHome working code

Hi All

Does anyone have a current working ESPHome code example they can share for the iFan04 with/without buzzer & remote control working please.

Thanks

You should be able to work it out from here.

Ditched my post from yesterday as it will only add confusion

I have it working now with RF & Buzzer under ESPHome1.19.4 - I can’t take any credit for the code, its a mashup from two other users that did the work @sliwma & @netadmindave - thanks !

yaml

substitutions:
  device_name: ifan04_test
  friendly_name: iFan04-test
  userpass: ********
  wifi_pass: ********
  ssid: *********

  
wifi:
  ssid: "${ssid}"
  password: "${wifi_pass}"
  manual_ip:
   static_ip: *********
   gateway: *********
   subnet: 255.255.255.0
   
  ap:
    ssid: "${friendly_name}"
    password: "${userpass}"

captive_portal:

logger:

api:

ota:

web_server:
  port: 80
  
time:
  - platform: homeassistant
    id: homeassistant_time

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m
  includes:
    - ifan04.h
  on_boot:
    priority: 225
    # turn off the light as early as possible
    then:
      - light.turn_off: ${device_name}_light

remote_receiver:
  pin: GPIO3

binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO0
    on_press:
      then:
        - light.toggle: ${device_name}_light

  - platform: remote_receiver
    name: "Buzzer"
    id: remote_buzzer
    internal: true
    raw:
      code: [-207, 104, -103, 104, -104, 103, -104, 207, -104, 103, -104, 103, -104, 104, -103, 104, -103, 104, -104, 107, -721, 105, -206, 207, -518, 105, -931, 104, -104, 103, -725, 104, -104, 103, -725, 104, -104, 103, -207, 104, -414]
    on_release:
      then:
        - switch.toggle: buzzer_dummy

  - platform: remote_receiver
    name: "Fan 0"
    id: remote_0
    raw:
      code: [-207, 104, -103, 104, -104, 103, -104, 207, -104, 103, -104, 104, -103, 104, -104, 103, -104, 105, -102, 104, -725, 104, -311, 103, -518, 104, -933, 103, -104, 104, -725, 104, -932, 104, -207, 207, -519]
    on_release:
      then:
        - fan.turn_off: ${device_name}_fan
    internal: true

  - platform: remote_receiver
    id: remote_fan1
    raw:
      code: [-207, 104, -104, 103, -104, 104, -103, 207, -104, 104, -103, 104, -104, 103, -104, 104, -103, 104, -104, 103, -726, 103, -312, 103, -518, 104, -933, 103, -104, 104, -725, 104, -103, 104, -726, 103, -104, 311, -518]
    on_release:
      then:
        - fan.turn_on:
              id: ${device_name}_fan
              speed: 1
        - if:
            condition:
              and:
                - switch.is_on: buzzer_dummy
            then:
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
    internal: true
  - platform: remote_receiver
    id: remote_fan2
    raw:
      code: [-208, 103, -104, 104, -103, 104, -103, 208, -103, 104, -104, 103, -104, 104, -103, 104, -104, 103, -104, 103, -726, 104, -310, 104, -518, 104, -933, 103, -104, 104, -725, 104, -207, 104, -622, 103, -416, 102, -415]
    on_release:
      then:
        - fan.turn_on:
              id: ${device_name}_fan
              speed: 2
        - if:
            condition:
              and:
                - switch.is_on: buzzer_dummy
            then:
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
              - delay: 50ms
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
    internal: true

  - platform: remote_receiver
    id: remote_fan3
    raw:
      code: [-207, 104, -104, 103, -104, 104, -103, 208, -103, 104, -104, 103, -104, 104, -103, 104, -104, 103, -104, 103, -726, 104, -311, 104, -518, 103, -934, 103, -103, 104, -726, 103, -104, 207, -622, 104, -103, 104, -207, 104, -415]
    on_release:
      then:
        - fan.turn_on:
              id: ${device_name}_fan
              speed: 3
        - if:
            condition:
              and:
                - switch.is_on: buzzer_dummy
            then:
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
              - delay: 50ms
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
              - delay: 50ms
              - output.turn_on: buzzer
              - delay: 50ms
              - output.turn_off: buzzer
    internal: true

  - platform: remote_receiver
    id: remote_light
    raw:
      code: [-207, 104, -103, 104, -104, 103, -104, 207, -104, 103, -104, 104, -103, 104, -103, 104, -104, 103, -104, 104, -725, 104, -311, 103, -518, 104, -933, 103, -104, 103, -726, 103, -311, 104, -518, 104, -207, 104, -103, 104, -414]
    on_release:
      then:
        - light.toggle: ${device_name}_light


output:
  - platform: custom
    type: float
    outputs:
      id: fanoutput
    lambda: |-
      auto ${device_name}_fan = new IFan04Output();
      App.register_component(${device_name}_fan);
      return {${device_name}_fan};

  - platform: gpio
    pin: GPIO9
    inverted: True
    id: light_output

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

light:
  - platform: binary
    name: "${friendly_name} Light"
    output: light_output
    id: ${device_name}_light

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

  - platform: template
    id: update_fan_speed
    optimistic: True
    turn_on_action:
      then:
        - delay: 200ms
        - if:
            condition:
              and:
                - switch.is_off: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_off: ${device_name}_fan
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ${device_name}_fan
                  speed: 1
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ${device_name}_fan
                  speed: 2
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_on: fan_relay3
            then:
              - fan.turn_on:
                  id: ${device_name}_fan
                  speed: 3
        - switch.turn_off: update_fan_speed

  - platform: gpio
    pin: GPIO14
    id: fan_relay1

  - platform: gpio
    pin: GPIO12
    id: fan_relay2

  - platform: gpio
    pin: GPIO15
    id: fan_relay3

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

ifan04.h

#include "esphome.h"
using namespace esphome;

class IFan03Output : public Component, public FloatOutput {
  public:
    void write_state(float state) override {
      if (state < 0.3) {
        // OFF
        digitalWrite(14, LOW);
        digitalWrite(12, LOW);
        digitalWrite(15, LOW);
      } else if (state < 0.6) {
        // low speed
        digitalWrite(14, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(15, LOW);
      } else if (state < 0.9) {
        // medium speed
        digitalWrite(14, HIGH);
        digitalWrite(12, HIGH);
        digitalWrite(15, LOW);
      } else {
        // high speed
        digitalWrite(14, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(15, HIGH);
      }
    }
};

Can you share a link to wear you purchased this item and if it works well without exchanging the capacitors out for US-based fans?

Really? The page I pointed to has 4 sources to buy from and specifically says

Largely identical to iFan03 with capacitors designed for 110V fans and an improved RF chip with better range.

Really happy with mine now, have a nice lovelace card for the fan and my original wall switches work the lights & fan on/off at low speed using a D1 mini. I think medium speed is a tiny bit faster but low is spot on which is where our fans run 90% of the time.

I bought direct from itead, took around 3 weeks delivery to east coast. I got 2 units.

https://itead.cc/product/sonoff-ifan03-wi-fi-ceiling-fan-and-light-controller/