Sonoff Ifan04 - ESPHome working code

This is the first ESPHome device (iFan04-H) so coding isn’t that obvious to me at the moment.

The good news is, I got your code working just fine once I got the basics of getting code to a device. Even managed to add a few diagnostics and an uptime sensor.

However what I’m looking for is a switch to enable/disable the buzzer when required without having edit the code. A liitle help there would be appreciated.

This is already in the sources above but in case you missed it:

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

Will give you a button in HA for you to toggle

I was using cpyarger’s code. I changed to that one because it had the buzzer working hard coded.
The yaml code you referred too is from one that wouldn’t enable the buzzer regardless of the switch state.

My current code is a merger of the two, mainly to get the other stats like uptime, wifi ssid etc.

I know its a little picky, but I like the little details :slight_smile:

Be sure to share :slight_smile:

1 Like

This adapter posted above doesn’t even show up in aliexpress anymore.
https://www.aliexpress.com/item/32761423124.html
Are there any suggested adapters from amazon that are known to work??

Thanks!

Just type CH340 in aliexpress and you will find the module pretty quickly by referencing the picture above.

https://www.aliexpress.com/item/32831329095.html

The only thing that matters is that it has an onboard regulator circled in the image above.

I just found this person’s code and it works exceptionally well with the remote so far. Very responsive.

Everyone may want to give it a test and if so report back.

I commented out the controller flash writing stuff. As I never want it to boot up in the last state (power outage or turn on randomly in the night etc.) Always want it off on boot. Probably need to do more than that.

Looks like they also use my I2C addon discovery work.

You can conveniently reuse the buzzer/wifisync buttons on the remote like this for other lights or stuff in the room.

          - if: # Buzzer on/off
              condition:
                lambda: return x == "0106000101";
              then:
                #- switch.toggle: buzzer_enabled
                #- output.turn_on: buzzer
                #- delay: 10ms
                #- output.turn_off: buzzer
                - homeassistant.service:
                    service: light.toggle
                    data:
                      entity_id: light.sengled_e11_n1ea_light
          - if: # Press one of the two bottom buttons on the remote Wifi/RF unsync (Do not hold down)
              condition:
                lambda: return x == "0101000102";
              then:
                - homeassistant.service:
                    service: light.toggle
                    data:
                      area_id: living_room

Make sure to turn this on to control other devices in HA from the ESPHome device

1 Like

Thanks for posting this. I just started using esphome and switched this device from tasmota. I actually use smartbulbs in my fan since the sonoff doesn’t dim (the light wire goes right to hot and not through the sonoff). Adding the light.toggle call service for the bulbs right in the config lets me still use the remote to turn the bulbs on and off with no extra automations.

Overall the code works well, but i did have a situation where quickly turning the fan off after first setting it to low caused the fan to come back on high after a few seconds. It was one of the first times after flashing, I’ll have to see if it happens again.

1 Like

Came here after finding that exact github project. Have you been working with it? I’ve been using your code from earlier this year with no issues. Hate switching code out unless this guy’s git is a better method.

1 Like

I’ve had my ifan04 for a while, but I just decided to convert it over the esphome. I’m using https://github.com/gagebenne/esphome/blob/8835ca6abbcc93e787d27f86f404c2ced0bdefb0/packages/ifan04.yaml for my yaml, but it seems there is an issue with pin 3. I get this error.

INFO ESPHome 2023.12.5
INFO Reading configuration /config/ifan04.yaml...
WARNING ESP8266: Pin 9 (9-10) might already be used by the flash interface in QUAD IO flash mode.
WARNING ESP8266: Pin 10 (9-10) might already be used by the flash interface in QUAD IO flash mode.
Failed config

uart: [source <unicode string>:15]
  
  Pin 3 is used in multiple places.
  - tx_pin: 
      number: 1
      mode: 
        output: True
        input: False
        open_drain: False
        pullup: False
        pulldown: False
        analog: False
      inverted: False
    rx_pin: 
      number: 3
      mode: 
        input: True
        output: False
        open_drain: False
        pullup: False
        pulldown: False
        analog: False
      inverted: False
    baud_rate: 9600
    rx_buffer_size: 256
    stop_bits: 1
    data_bits: 8
    parity: NONE
remote_receiver: [source <unicode string>:20]
  
  Pin 3 is used in multiple places.
  - pin: 
      number: 3
      mode: 
        input: True
        output: False
        open_drain: False
        pullup: False
        pulldown: False
        analog: False
      inverted: False
    dump: []
    tolerance: 25
    buffer_size: 1000
    filter: 50us
    idle: 10ms
    memory_blocks: 3

I did a little searching and it sounds like there might be some brand new checking to prevent accidental config errors. I experimented with adding allow_other_uses: True but I couldn’t figure out the syntax.

Things like that are why it was added. You’re using the same pin for uart RX and the remote receiver which really doesn’t make sense and you’re lucky it works.

This compiled but sounds like it might not be the right thing to do in the end.

uart:
  tx_pin: 1
  rx_pin:
    number: 3
    allow_other_uses: true
  baud_rate: 9600

remote_receiver:
  pin:
    number: 3
    allow_other_uses: true

It’s not. What are you trying to do with that?

I just did this with this GitHub - rh1rich/esphome-ifan04: Sonoff IFAN04 with ESPHome

Works perfectly.

Someone more qualified can answer why pin 3 is reused. I just found out how to disable the check.

@deschmit - The link that @Wheemer mentioned above does not reuse pin 3.

You know what they say if it’s not broken don’t fix it.

As for me, it was driving the people in my house nuts that the remote wouldn’t properly respond all the time. Nobody else in this thread seems to confirm this behavior or say they don’t use the remote enough for it to matter. To me that is broken. @ssieb told me that the old code defines the UART twice which may be the problem there. The code that rh1rich did does this as well but the remote works 99% of the time with 1 press which is better than barely ever. I made an issue on rh1rich’s repo for it. Also, the ability shown above to reuse two buttons on the remote pretty easily, lets the little people in my house scared of the dark, turn the lights on downstairs from their room instead of waking others up to do that.

The only downfall to this code I see is the dev made this code and then disappeared. They Don’t seem to look at their GitHub issues or may be away on a long vacation UART conflict · Issue #1 · rh1rich/esphome-ifan04 · GitHub So if you find anything wrong you are probably still on your own just like the other methods above to find a way to fix it.

It’s been running pretty much fine all this time so far. I wouldn’t use anything else personally, specifically for the remote responsiveness. I had a few glitches where the remote randomly decided to pick one of the other speeds instead of the one you pressed. Not sure if this is a flaw in the code or the RF remote sending or not receiving the right bit to translate to the right action.

Hope this helps in your decision.

Nothing stops you from forking the project and place your version here and proclaim victory :slight_smile:
That is the beauty of opensource after all !

I personally do not use this code so my remote works!
I use:

external_components:
  - source: github://ssieb/custom_components
    components: [ ifan04 ]

Sure there is a version of that code here already !

The remote in my scenario is used as the primary switch for the room light so it’s noticeable if it doesn’t work 100% of the time. The controller is directly powered with the hotline no hardwired switch in between. The switch on the wall for the room won’t turn the power to the controller off like most setups, so wondering if maybe people turning off the power to the unit all the time helps with also not seeing remote problems.

What does your full ESPHome YAML look like?

@NonaSuomy Would you mind helping guide me on getting a IFAN04-L flashed with esphome and adopted into my local Home Assistant installation? I’m trying to replace a failed Insteon Fanlinc at our second home, which I am only physically at for the next few days. I don’t really care about remotes and/or particular flavors of esphome projects to use, as I just need to be able to automate fan speeds to match the other fans in the house. I soldered pins onto the board and have it physically connected to my Mac which has esphome on it. I am just a n00b at how to proceed from here and cannot find any examples of working yaml that I can use to get it going.