iFan03 on ESPHome, working ! well...sort of

I was able to get it to work in HA 2021.6.5.

my yaml

substitutions:
  device_name: ifan03_1
  friendly_name: iFan03-1
  userpass: password
  wifi_pass: wifi-password
  ssid: ssid

  
wifi:
  ssid: "${ssid}"
  password: "${wifi_pass}"

  ap:
    ssid: "${friendly_name}"
    password: "${userpass}"

captive_portal:

logger:

api:
  password: "${userpass}"

ota:
  password: "${userpass}"

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


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

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

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

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: ${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

with the ifan03.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);
      }
    }
};

I have a sonoff 433 bridge that deals with the remotes, I have 4 of the fans and the remotes, when paired, change the other fans. when not paired the remotes all broadcast different commands.

Does the code above work with the latest HA fan speed (percentage) requirements and the remote works as well?

Hi @finity , I no longer use this integration, sorry.

Hi @finity, the code still works with minor adjustments. The ifan03.h file remains the same but in your actual code for the devices you need to make the following changes to the "speed: " lines.

  - 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: ifan03_fan

        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan03_fan
                  speed: 33
   
        - if:
            condition:
              and:
                - switch.is_off: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan03_fan
                  speed: 66
        - if:
            condition:
              and:
                - switch.is_on: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan03_fan
                  speed: 100
        - switch.turn_off: update_fan_speed

2 Likes

The code with the percentages is working fine, but my RF remote has stopped working. I’ve used @sliwma 's codeand changed it the speeds to %, but the RF remote has completely stopped working now, have tried re-paring, but I don’t hear the beep sound from the iFAN when attempting the paring steps, any suggestions?

Works like charm!! thanks

But I would like the light to turn on instantly when the ifan gets power, how do I do that?

I have tried this

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

And it works but only after 5-6 seconds, is there a way to tell ifan3 to turn the light on when the power is on? Thanks again…

ps: 5-6 seconds is quite a lot taking into consideration that my wife is going to use that light… in other words, I don´t want to sleep on the sofa tonight :stuck_out_tongue:

Shouldn’t the light component restore it’s state after boot?

225 isn’t the highest priority though:

so…

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

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m
  includes:
    - ifan03.h
  on_boot:
    priority: 600
    # turn on the light as early as possible
    then:
      - light.turn_on: ${device_name}_light

or just with the “restore_mode: ALWAYS_ON” would be enough?? thank you so much!!!

hi guys,
just wondering did somebody get the ifan03 integrated completely with HA? and could you share your configurations?

+1

Because I’m not a coder I get really confused by all the variants we often see here and am really happy when I find a definitive solution, at least as a starting point. I appreciate different people prefer different things but also believe ‘most people’ would probably like something that sorta works like ‘most people’ might like. :wink:

Like (in no order and I appreciate we probable have all / most at this point on this thread).

A toggle to turn the buzzer on or off.
That it presents itself in HA as ‘Buzzer On/Off’, ‘Light On/Off’ and ‘Fan speeds 1, 2 and 3’ (and they have interlocks, only allowing one relay on at a time).
That the remote still works.
The indicator LEDS still ‘indicate’ the original activity (?) if possible and if not, at least that the unit is powered up.

So, I have added a header to my iFan04, initially flashed it with the basic ESPHome code (to make sure I could and it would go online, it did) and then used the code from the top of the page, not knowing if it represents ‘the best of’ or any of the fixes / updates etc? It seems to work (not applied 240V to it yet but I can hear the relays clicking) and I have the controls on my HA dashboard. The Light is as one would expect but the fan control is a bit clunky when using the std dashboard (you have to open the fan function then randomly slide the slider etc)?

This isn’t a complaint, I very grateful to be able to stand on the shoulders of giants and maybe it’s assumed we can all follow / understand the code, especially when it’s not the whole yaml etc.

As a long term electronics field support tech I was able to add the header and got it to flash first time, it’s just that in spite of trying to learn how to code over many (of my 66 years), it’s always evaded me, especially when you aren’t doing it every day. ;-(

So doesn’t anyone know if there is such a base code offering for the iFan03/4 please? :wink:

Cheers.

Good day,

Please follow this link to my Github Repo for the files and setup required for the integration of the Sonoff iFan03 into Home Assistant via ESPHome.

Thanks very much for your input, I’ve downloaded the files and will check them out later.

Whilst I appreciate some people put a lot of effort into their dashboards, I’m a bit of a utilitarian when it comes to that and so stick to the KISS process.
Now I think I understand that these fans use capacitors to manage the two lower speeds (not PWM as I first assumed) and presumably they are switched in order singly, I’m not sure why people don’t simply present (in ESPHome) the 4 relays as 4 individual relays, one for the light and 3 to the fan being interlocked and then you would automatically just see the 4 controls you wanted in HA?

That’s what I have done with my electric curtain controller where I have three relays, Open, Stop and Close pulsing for 1 second (to trigger the original Swish controller) and are interlocked so only one can be triggered at a time and all 3 come up as that in HA?

switch:

  • platform: gpio
    pin: GPIO16
    id: relay_1
    restore_mode: ALWAYS_OFF
    interlock: &interlock_group [relay_1, relay_2, relay_3, relay_4]

  • platform: gpio
    pin: GPIO17
    id: relay_2
    restore_mode: ALWAYS_OFF
    interlock: *interlock_group

  • platform: gpio
    pin: GPIO18
    id: relay_3
    restore_mode: ALWAYS_OFF
    interlock: *interlock_group

  • platform: gpio
    pin: GPIO19
    id: relay_4
    restore_mode: ALWAYS_OFF
    interlock: *interlock_group

and etc

button:

  • platform: template
    name: “Curtains open”
    icon: “mdi:unfold-more-vertical”
    on_press:
    then:
    • switch.turn_on: relay_1
    • delay: 1s
    • switch.turn_off: relay_1

I don’t know it the above code is right but it seems to work. :wink:

In all honesty I have been using this code for a while now, and it works great. I have 3 fans running the firmware, all of them with remote functionality.

I don’t think that the Interlock code is wrong and it can work for some setups, however since we are controlling a fan, we want it to be displayed as a single entity or device. It also makes control from a Voice Assistant easier.

Thanks again for the reply. :wink:

I plan to give your firmware a go later to see how it compares with what I have now and I get that there may be situations where it would suite a particular approach, like the voice control you mentioned.

On that though … we seem to go out of our way to keep stuff local then use things like Google Home or Amazon Alexa that I believe are both cloud based and really are ‘big brother’? Or are we only using any ‘built in’ Voice Assistants (if there is such a thing)?

So, given I don’t use any VA for any HA / automation stuff (my focus is making my HA system do stuff for me without being asked (like PIR triggered lights or curtains on dusk / dawn etc or allowing me to do things I couldn’t before, like monitor whole house power usage etc) so the odd time I want to have control a remote controller / PC / phone is generally good enough. :wink:

So maybe I’ll see if I can cobble some Fan controlling code together based on a mix of what I find and my curtain controller.

OOI, do we know how the speeds are switched on these fans? eg, Is it, one cap, another (bigger) cap then full power or one cap then PLUS another etc? I’m guessing it wouldn’t do any harm mixing and matching as I predict the caps would just end up in parallel and so could offer 3 speeds between them?

Speed 1, smallest cap > motor
Speed 2, largest cap > motor
Speed 3, small+large caps > motor
Speed 4, direct connection, caps shorted?

Hi,

I’ve just uploaded your straight iFan03 code (not seen / used the little web portal before and used that to upload the code) and it all seems to work (well, I can hear the relays clicking etc). :wink:

What would I need to be able to do to have individual available buttons for the different fan speeds please?

Oh, also I get when I open the log:

“[21:16:21][W][remote_receiver.esp8266:063]: Remote Receiver Signal starts with a HIGH value. Usually this means you have to invert the signal using ‘inverted: True’ in the pin schema!” ?

Do you see that on your implementations?

shameless plug… :slightly_smiling_face:

The post below is good for fan control, in my files if you have a look a the lovelace dashboard file you will see that in order to set the fans to a certain speed, i use the following service calls:

          action: call-service
          service: fan.turn_on
          service_data:
            percentage: 33

As the fan is one entity the same as a Light Bulb, you only have on entity listed for the fan which can be accessed for and the speed adjusted on a slider. To overcome this issue and for WAF i decided to use the service calls instead on a four button layout.

iFan03_DashBoard

As for the error that you are experiencing, this is normal, with the Sonoff Device like ifan03.

Thanks guys.

I’m slightly confused by the whole need for a percentage thing? I mean, the fan has 3 pre-defined speeds and they are provided by 3 relays and two capacitors.

The original remote came with 3 speed buttons and Off, as does the one that came with the iFan04.

Therefore for the most simple control in HA you just need to be able to tell it to turn On/Off any of the 4 relays to ba able to make the fan or light do anything you want?

I understand if you want to use a Voice Assistant (I don’t) or if you have an Up/Down type remote you might want to be able to control the fan speeds with a slider, as would I if the fan actually had a linear speed control, but it doesn’t. ;-(

So last night I spent a good few hours using mostly trial and error and started to trim your .yaml down to something nearer to my needs (without the remote for now) and ended up with this:

esphome:
name: viper-fan
platform: ESP8266
board: esp8285

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.0.224
gateway: 192.168.0.100
subnet: 255.255.255.0

ap:
ssid: “Viper hotspot”
password: !secret ap_wifi_password

captive_portal:

web_server:
port: 80
auth:
username: admin
password: !secret api_password

api:
password: !secret api_password

logger:
level: info
baud_rate: 0

ota:

#Wifi Info Text Sensor
text_sensor:
platform: wifi_info
ip_address:
name: Viper IP Address
ssid:
name: Viper SSID

sensor:

  • platform: wifi_signal
    name: Viper WiFi Signal
    update_interval: 60s

switch:

Buzzer Setup

  • platform: gpio
    name: “Viper buzzer”
    id: buzzer
    pin:
    number: 10
    inverted: true
    internal: true
    restore_mode: ALWAYS_OFF

Fan and light setup

  • platform: gpio
    name: “Fan speed 1”
    icon: “mdi:fan-speed-1”
    id: fan_relay1
    pin: 14
    interlock: &interlock_group [fan_relay1, fan_relay2, fan_relay3]
    restore_mode: ALWAYS_OFF

  • platform: gpio
    name: “Fan speed 2”
    icon: “mdi:fan-speed-2”
    id: fan_relay2
    pin: 12
    interlock: *interlock_group
    restore_mode: ALWAYS_OFF

  • platform: gpio
    name: “Fan speed 3”
    icon: “mdi:fan-speed-3”
    id: fan_relay3
    pin: 15
    interlock: *interlock_group
    restore_mode: ALWAYS_OFF

  • platform: gpio
    id: fan_light
    name: “Fan Light”
    icon: “mdi:lightbulb”
    pin: 09
    restore_mode: ALWAYS_OFF

  • platform: restart
    name: “Restart!”

I’m not sure the light bit is right and haven’t tried to put the buzzer function back in or even if I want it?

Again, I’ve not got it connected to the fan as it’s stripped down but I can hear all 4 relays work when I toggle them on the HA desktop and all the entities appear as expected when you add that ESP integration.(I’m not sure about the purity of the light part though)?

Because the remote supplied with the iFan04 feels a bit crude in use (and I can’t read the icons), I may well just find a ‘better’ Zigbee remote or make one using an ESP32.

My Mrs, although now struggling with the early signs of Dementia can sill use her HA desktop on her smartphone to open / close the curtains and turn the main light on / off so should also be ok with the fan as the 1/2/3 Slow/Med/Fast controls are more intuitive to her because of the existing ‘cyclic’ pull string and radio push-button type fans we have here.

I really wish this coding lark didn’t put my head in a spin (it’s like trying to make sense of an alien language to me) as then I could do more of what I want without having to bother anyone. ;-(

Viper fan and light

Fans in Home Assistant used to have 3 speed settings:
Low
Medium
High.

However with some fans and controllers having more than three speed settings, they had to move to a percentage system, to accommodate fans with more speed settings, so considering a 3 speed fan the values would be as follows:
Off = 0%
Low = 1 - 33 %
Med = 34 - 66%
High = 67- 100%

As for the buzzer it is intended only to work with the remote controller input, i have not been able to even activate it via a service call.

Another thing, with the fan controller you will be required to have Relay 1 open on all speed settings to control the current being supplied to the fan.