poster copy

All In One Home Automation V2 using Home Assistant

In the first version of our All in One Home Automation project, we were able to control 4 appliances and speed of 1 AC fan using Home Assistant Dashboard and manual switches. We already covered a complete video about that on YouTube.

After the great success of the first version, we came up with the second version of this project which is 18% smaller then the previous one but still has more features in it. And this is the article about that project, Enjoy…

Components Required:-

Buy the complete kit which is Ready to Use…

We are selling this complete project, with all the components already soldered on the PCB along with ESP32 which you need to program it by yourself, DHT11 Sensor and Rotary Switch . Just click the link mentioned below to buy it.

Schematic

Connection diagram

Code files

esp32.yaml

esphome:
  name: esp-fan
  includes: ifan02.h

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "tXfMjq3L0HQrXSqi5oWOG5NFVo20arn1m7lGs9GJKL4="

ota:
  password: "989776e5dd4b4024d2346ed0c38de407"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp-Fan Fallback Hotspot"
    password: "oLR184xFSxja"

sensor:
    - platform: dht
      pin: 16
      temperature:
        name: "Living Room Temperature"
      humidity:
        name: "Living Room Humidity"
      update_interval: 5s

captive_portal:

binary_sensor:
  - platform: gpio
    pin: GPIO32
    name: "Light1"
    device_class: light
    on_press:
      then:
        - light.turn_off: light1
    on_release:
      then:
        - light.turn_on: light1
        
  - platform: gpio
    pin: GPIO35
    name: "Light2"
    device_class: light
    on_press:
      then:
        - light.turn_off: light2
    on_release:
      then:
        - light.turn_on: light2

  - platform: gpio
    pin: GPIO34
    name: "Light3"
    device_class: light
    on_press:
      then:
        - light.turn_off: light3
    on_release:
      then:
        - light.turn_on: light3

  - platform: gpio
    pin: GPIO39
    name: "Light4"
    device_class: light
    on_press:
      then:
        - light.turn_off: light4
    on_release:
      then:
        - light.turn_on: light4
        
  - platform: gpio
    pin: GPIO33
    name: "Fan_Switch"
    device_class: light
    on_press:
      then:
              fan.turn_off: ifan02_fan
    on_release:
      then:
        - fan.turn_on:
              id: ifan02_fan
              speed: 1

  - platform: gpio
    pin: GPIO27
    name: "fan speed1"
    device_class: power
    id: fan_speed1
    on_release:
          then:
          - fan.turn_on:
              id: ifan02_fan
              speed: 1
              

  - platform: gpio
    pin: GPIO14
    name: "fan speed2"
    device_class: power
    id: fan_speed2
    on_release:
      then:
        - fan.turn_on: 
            id: ifan02_fan
            speed: 2

  - platform: gpio
    pin: GPIO13
    name: "fan speed4"
    device_class: power
    id: fan_speed4
    on_release:
          then:
            - fan.turn_on: 
                id: ifan02_fan
                speed: 4

light:
  - platform: binary
    name: "Light1"
    output: light_output1
    id: light1

  - platform: binary
    name: "Light2"
    output: light_output2
    id: light2

  - platform: binary
    name: "Light3"
    output: light_output3
    id: light3

  - platform: binary
    name: "Light4"
    output: light_output4
    id: light4

output:
  - id: light_output1
    platform: gpio
    pin: 15
    

  - id: light_output2
    platform: gpio
    pin: 2
    

  - id: light_output3
    platform: gpio
    pin: 4
    

  - id: light_output4
    platform: gpio
    pin: 22

  - platform: custom
    type: float
    outputs:
      id: fanoutput
    lambda: |-
      auto ifan02_fan = new IFan02Output();
      App.register_component(ifan02_fan);
      return {ifan02_fan};

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: 1
        - if:
            condition:
              and:
                - switch.is_off: fan_relay1
                - switch.is_on: fan_relay2
                - switch.is_off: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan02_fan
                  speed: 2
        - 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: 3
        - if:
            condition:
              and:
                - switch.is_off: fan_relay1
                - switch.is_off: fan_relay2
                - switch.is_on: fan_relay3
            then:
              - fan.turn_on:
                  id: ifan02_fan
                  speed: 4
        - switch.turn_off: update_fan_speed
        

  - platform: gpio
    pin: GPIO21
    id: fan_relay1

  - platform: gpio
    pin: GPIO19
    id: fan_relay2

  - platform: gpio
    pin: GPIO18
    id: fan_relay3

fan:
  - platform: speed
    output: fanoutput
    id: ifan02_fan
    speed_count: 4
    name: "iFan02 Fan"


ifan02.h


#include "esphome.h"

using namespace esphome;

class IFan02Output : public Component, public FloatOutput {
  public:
  void write_state(float state) override 
  {
      if(state < 0.2)
      {
        digitalWrite(21, LOW);
        digitalWrite(19, LOW);
        digitalWrite(18, LOW);
      }
      else if (state < 0.4) {
        // OFF
          digitalWrite(21, LOW);
          digitalWrite(19, LOW);
          digitalWrite(18, LOW);
          delay(1000);
          digitalWrite(21, HIGH);
          digitalWrite(19, LOW);
          digitalWrite(18, LOW);
      } else if (state < 0.6) {
        // low speed
          digitalWrite(21, LOW);
          digitalWrite(19, LOW);
          digitalWrite(18, LOW);
          delay(1000);
          digitalWrite(21, LOW);
          digitalWrite(19, HIGH);
          digitalWrite(18, LOW);
      } else if (state < 0.8) {
        // medium speed
          digitalWrite(21, LOW);
          digitalWrite(19, LOW);
          digitalWrite(18, LOW);
          delay(1000);
          digitalWrite(21, HIGH);
          digitalWrite(19, HIGH);
          digitalWrite(18, LOW);
      } else {
        // high speed
          digitalWrite(21, LOW);
          digitalWrite(19, LOW);
          digitalWrite(18, LOW);
          delay(1000);
          digitalWrite(21, LOW);
          digitalWrite(19, LOW);
          digitalWrite(18, HIGH);
      }
  }
};

Full Tutorial video

If you want to understand this project in detail, kindly watch out our complete tutorial video on it

Comments are closed.