Help with Sonoff iFan02: Light turns on at bootup no matter what I do

Hi all,

I have a Sonoff iFan02 working with ESPHome, which I got right using some code from a github user called @quazzie. They don’t appear to have been active for a while.

I got it to compile with only very minor changes - can’t use “light” as an id anymore . However, when the device boots up, the light turns on. Now, I mostly don’t care, because the iFan is deployed in a lounge. But I want to put one in my kids’ bedroom, where it will be an issue.

Why? Because we live in an area where the grid is unstable, and short power dips (2-3 seconds) are a regular occurrence. If the iFan turns the light on every time it boots, then it’ll wake my kids up several times a night. Nope.

Below is the (barely) modified code. I have added an “on boot” section to turn the light off immediately on startup, but this has no effect. I also tried adding restore_mode: ALWAYS_OFF to the lght config. Also doesn’t work.

Can you see anything glaringly obviously wrong in the code? I’m stumped.

esphome:
  name: son025_ifan1
  platform: ESP8266
  board: esp8285
  includes:
    - ifan02.h
  on_boot:
    then:
      - light.turn_off: light1

wifi:
  ssid: "<SSID>"
  password: "<PASSWORD>"
  manual_ip:
    static_ip: 192.168.1.87
    gateway: 192.168.1.1
    subnet: 255.255.255.0
# Enable logging

logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    id: vbutton_light
    pin: 
      number: GPIO0
      inverted: True
    on_press:
      then:
        - light.toggle: light1

  - platform: gpio
    id: vbutton_relay_1
    pin: 
      number: GPIO9
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay1
        - switch.turn_on: update_fan_speed

  - platform: gpio
    id: vbutton_relay_2
    pin: 
      number: GPIO10
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay2
        - switch.turn_on: update_fan_speed
        
  - platform: gpio
    id: vbutton_relay_3
    pin: 
      number: GPIO14
      inverted: True
    on_press:
      then:
        - switch.toggle: fan_relay3
        - switch.turn_on: update_fan_speed

output:
  - platform: custom
    type: float
    outputs:
      id: fanoutput
    lambda: |-
      auto ifan02 = new IFan02Output();
      App.register_component(ifan02);
      return {ifan02};
      
  - platform: gpio
    pin: GPIO12
    id: light_output

light:
  - platform: binary
    name: "Light - Stoep"
    output: light_output
    id: light1
    restore_mode: ALWAYS_OFF

switch:
  - 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: ifan02
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on: 
                  id: ifan02
                  speed: LOW
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on: 
                  id: ifan02
                  speed: MEDIUM
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_on: fan_relay3
            then:
              - fan.turn_on: 
                  id: ifan02
                  speed: HIGH
        - switch.turn_off: update_fan_speed

  - platform: gpio
    pin: GPIO5
    id: fan_relay1
    
  - platform: gpio
    pin: GPIO4
    id: fan_relay2
    
  - platform: gpio
    pin: GPIO15
    id: fan_relay3

fan:
  - platform: speed
    output: fanoutput
    id: ifan02
    name: "Fan - Stoep"

The ifan02.h file referred to is @quazzie’s original, it seems unrelated to the light at all, and seems to only refer to the fanspeed relay. Nevertheless, here it is:

#include "esphome.h"
using namespace esphome;

class IFan02Output : public Component, public output::FloatOutput {
  public:
    void write_state(float state) override {
        if (state < 0.3) {
          digitalWrite(5, LOW);
          digitalWrite(4, LOW);
          digitalWrite(15, LOW);
        }

        if (state >= 0.32 && state <= 0.34) {
          digitalWrite(5, HIGH);
          digitalWrite(4, LOW);
          digitalWrite(15, LOW);
        }
        if (state >= 0.65 && state <= 0.67) {
          digitalWrite(5, HIGH);
          digitalWrite(4, HIGH);
          digitalWrite(15, LOW);
        }
        if (state >= 0.9) {
          digitalWrite(5, HIGH);
          digitalWrite(4, LOW);
          digitalWrite(15, HIGH);
        }
    }
};

Have you checked to see if you have a retained message in mqtt ?

Hi @cooljimy84, thanks for the suggestion. Generally speaking MQTT problems are a good guess with random issues like this, but, one of the things I like about ESPHome is that it integrates directly with HA through the API: no MQTT broker is required (although you can use MQTT if you specifically add it).

So it’s not that.

Hi :slight_smile:
I have updated the code on my gist.

The light powers on because the ifan02 is designed that way.
There is not way to disable that (well maybe with some HW mod).

I have added a “fix” for the issue by turning the light off as soon as possible.
But if power is toggled there will be a brief period of light.

1 Like

Thank you @quazzie.

Yes I see the API bit in the code now. :man_facepalming: