-
-
Save quazzie/09ee3ef2c419ecbcf979a7410062481b to your computer and use it in GitHub Desktop.
#include "esphome.h" | |
using namespace esphome; | |
class IFan02Output : public Component, public FloatOutput { | |
public: | |
void write_state(float state) override { | |
if (state < 0.3) { | |
// OFF | |
digitalWrite(5, LOW); | |
digitalWrite(4, LOW); | |
digitalWrite(15, LOW); | |
} else if (state < 0.6) { | |
// low speed | |
digitalWrite(5, HIGH); | |
digitalWrite(4, LOW); | |
digitalWrite(15, LOW); | |
} else if (state < 0.9) { | |
// medium speed | |
digitalWrite(5, HIGH); | |
digitalWrite(4, HIGH); | |
digitalWrite(15, LOW); | |
} else { | |
// high speed | |
digitalWrite(5, HIGH); | |
digitalWrite(4, LOW); | |
digitalWrite(15, HIGH); | |
} | |
} | |
}; |
esphome: | |
name: ifan02 | |
platform: ESP8266 | |
board: esp8285 | |
includes: | |
- ifan02.h | |
on_boot: | |
priority: 225 | |
# turn off the light as early as possible | |
then: | |
- light.turn_off: ifan02_light | |
wifi: | |
ssid: <YOUR_SSID> | |
password: <YOUR_PASSWORD> | |
api: | |
logger: | |
ota: | |
binary_sensor: | |
- platform: gpio | |
id: vbutton_light | |
pin: | |
number: GPIO0 | |
inverted: True | |
on_press: | |
then: | |
- light.toggle: ifan02_light | |
- 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_fan = new IFan02Output(); | |
App.register_component(ifan02_fan); | |
return {ifan02_fan}; | |
- platform: gpio | |
pin: GPIO12 | |
id: light_output | |
light: | |
- platform: binary | |
name: "iFan02 Light" | |
output: light_output | |
id: ifan02_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: ifan02_fan | |
- if: | |
condition: | |
and: | |
- switch.is_on: fan_relay1 | |
- switch.is_off: fan_relay2 | |
- switch.is_off: fan_relay3 | |
then: | |
- fan.turn_on: | |
id: ifan02_fan | |
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_fan | |
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_fan | |
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_fan | |
name: "iFan02 Fan" |
'esp8266_restore_from_flash' option can help to restore settings after power failure and 'api.connected' condition can be used to prevent the early turning on of light component. Has anyone succeded in turing this code 'power failure resistant'?
Hello!
Does it works also with Sonoff iFan 03?
Hi,
For what it's worth, I can confirm it does work on the iFan 04. I think you can flash the 03 with reasonable confidence, still an ESP chip.
I figure that you just have to edit the yaml to make sure ESPHome is talking to the right GPIOs. You can check the 03's pinouts (and many other esp devices) here; https://templates.blakadder.com/index.html
@javihg1986 I wrote an ESP home yaml for iFan02 which even allows for restoration of the light state. Feel free to give this a go. https://devices.esphome.io/devices/Sonoff-iFan02
Thank you @DeeBeeKay and @rishabmehta7 . I will test it as soon as I can ;)
I do have an iFan04H but no cycles to check the GPIOs, if you figure it out. Let me know :-)
Sometimes when I have a power outage it takes a really long time for the fan to be available in home assistant and sadly I have to bear with the heat until it comes back which annoys the hell out of my girlfriend. How can I make it turn on in high at boot? That way after a power outage at least I have it working until it comes online again. @quazzie
I think I can add it like this :
But I'm unsure whether I need to do anything with
update_fan_speed
or not.EDIT:
Ok I think I figured it out. For people that want to turn the fan on right after boot:
The relevant parts are the relay1 and 3 being turned on at boot. That turns the fan at high speed but doesn't update home assistant.
From here there are two options. Either you add
switch.turn_on: update_fan_speed
at the bottomof the on_boot actions (which in my case it doesn't work because in case of a power outage HA isn't available yet due to the router being resetting), or find another place to make the update. I opted for the latter:For that I added a binary sensor of platform status and made the on_state event check if the API is connected and then push the update to HA.