Search code examples
moduleesp8266relay

esphome how write config for module esp01 with integrated 4 relay


I got 4 relay module with esp01 from aliexpress but it switch relays not through pins.

How I can control this relay module with esphome?


Solution

  • # Enable logging
    logger:
      baud_rate: 0 #need this to free up UART pins
    uart:
      baud_rate: 9600 # speed to STC15L101EW
      tx_pin: 1
      rx_pin: 3
    
    globals:
       - id: on_flag_1
         type: int
         restore_value: no
         initial_value: '0'
       - id: on_flag_2
         type: int
         restore_value: no
         initial_value: '0'
       - id: on_flag_3
         type: int
         restore_value: no
         initial_value: '0'
       - id: on_flag_4
         type: int
         restore_value: no
         initial_value: '0'
    
    switch:
      - platform: template 
        id: relay1
        name: "Relay #1"
        lambda: 
          return id(on_flag_1);
        turn_on_action:
          - uart.write: [0xA0, 0x01, 0x01, 0xA2]
          - globals.set:
              id: on_flag_1
              value: '1'
        turn_off_action:
          - uart.write: [0xA0, 0x01, 0x00, 0xA1]
          - globals.set:
              id: on_flag_1
              value: '0'
      - platform: template 
        id: relay2
        name: "Relay #2"
        lambda: 
          return id(on_flag_2);
        turn_on_action:
          - uart.write: [0xA0, 0x02, 0x01, 0xA3]
          - globals.set:
              id: on_flag_2
              value: '1'
        turn_off_action:
          - uart.write: [0xA0, 0x02, 0x00, 0xA2]
          - globals.set:
              id: on_flag_2
              value: '0'
      - platform: template 
        id: relay3
        name: "Relay #3"
        lambda: 
          return id(on_flag_3);
        turn_on_action:
          - uart.write: [0xA0, 0x03, 0x01, 0xA4]
          - globals.set:
              id: on_flag_3
              value: '1'
        turn_off_action:
          - uart.write: [0xA0, 0x03, 0x00, 0xA3]
          - globals.set:
              id: on_flag_3
              value: '0'
      - platform: template 
        id: relay4
        name: "Relay #4"
        lambda: 
          return id(on_flag_4);
        turn_on_action:
          - uart.write: [0xA0, 0x04, 0x01, 0xA5]
          - globals.set:
              id: on_flag_4
              value: '1'
        turn_off_action:
          - uart.write: [0xA0, 0x04, 0x00, 0xA4]
          - globals.set:
              id: on_flag_4
              value: '0'
    
    

    I have a single but extended it based on what worked on my single this will produce 4 switches that can be toggled on/off.