Skip to content

Instantly share code, notes, and snippets.

@sarabveer
Last active May 18, 2022 03:34
Show Gist options
  • Save sarabveer/b39996235928e582bf148930c095478a to your computer and use it in GitHub Desktop.
Save sarabveer/b39996235928e582bf148930c095478a to your computer and use it in GitHub Desktop.
ESPHome YAML Config for TreatLife SS01 3-Way Switch

upstairs-hallway-light-1.yaml

YAML for device.

# https://esphome.io/guides/configuration-types.html#substitutions
substitutions:
  device_name: upstairs-hallway-light-1
  friendly_name: "Upstairs Hallway Light 1"
  icon: "mdi:lightbulb"

# https://esphome.io/components/wifi.html
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${friendly_name}
    password: !secret wifi_password

<<: !include templates/treatlife_ss01_3way.yaml

templates/treatlife_ss01_3way.yaml

Base Template

# ##################################
# TreatLife SS01 3-Way Switch
# ##################################
# D2 GPIO04: White LED (Inverted)
# D1 GPIO05: Red LED (Inverted)
# D6 GPIO12: Relay
# D7 GPIO13: Button (Inverted)
# D5 GPIO14: Power/Usage Sensor (Inverted)
# ##################################

# https://esphome.io/components/esphome
esphome:
  name: ${device_name}

# https://esphome.io/components/esp8266.html
esp8266:
  board: esp01_1m

# https://esphome.io/components/captive_portal.html
captive_portal:

# https://esphome.io/components/web_server.html
web_server:
  port: 80

# Enable logging
# https://esphome.io/components/logger
logger:

# Enable Home Assistant API
# https://esphome.io/components/api
api:

# https://esphome.io/components/ota
ota:

# Red LED
# https://esphome.io/components/status_led
status_led:
  pin:
    number: GPIO5
    inverted: True

# https://esphome.io/components/binary_sensor/gpio.html
binary_sensor:
  # Other 3-Way Switch Sensor
  - platform: gpio
    pin:
      number: GPIO14
      inverted: True
    id: sensor
    name: ${friendly_name} Sensor
    filters:
      - delayed_off: 50ms
    on_press:
      - switch.turn_off: white_led
    on_release:
      - switch.turn_on: white_led
    internal: True

  # TreatLife Button
  - platform: gpio
    pin:
      number: GPIO13
      inverted: True
    id: button
    name: ${friendly_name} Button
    on_press:
      - switch.toggle: relay
    internal: True


switch:
  # https://esphome.io/components/switch/restart.html
  - platform: restart
    name: ${friendly_name} Restart

  # White LED
  # https://esphome.io/components/switch/gpio
  - platform: gpio
    id: white_led
    name: ${friendly_name} White LED
    pin:
      number: GPIO4
      inverted: True
    restore_mode: RESTORE_DEFAULT_ON
    internal: True

  # Relay
  # https://esphome.io/components/switch/gpio
  - platform: gpio
    pin: GPIO12
    id: relay
    name: ${friendly_name} Relay
    internal: True
    
  # present the state of the switch in HA based on the sensor
  # https://esphome.io/components/switch/template.html
  - platform: template
    name: ${friendly_name}
    icon: ${icon}
    lambda: |-
      if (id(sensor).state) {
        return true;
      } else {
        return false;
      }
    # https://esphome.io/guides/automations.html#if-action
    turn_on_action:
      - if:
          condition:
            binary_sensor.is_off: sensor
          then:
            - switch.toggle: relay
    turn_off_action:
      - if:
          condition:
            binary_sensor.is_on: sensor
          then:
            - switch.toggle: relay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment