############################################# ############################################# # UNDERHOUSE LIGHTS # Controlled by a Sonoff 4Ch R2 # https://github.com/jvyoralek/homeassistant-config/blob/master/esphome/sonoff-4ch.yaml # # V1.0 2025-06-18 Initial Version # ########################################################################################## ########################################################################################## ########################################################################################## # SPECIFIC DEVICE VARIABLE SUBSTITUTIONS # If NOT using a secrets file, just replace these with the passwords etc (in quotes) ########################################################################################## substitutions: # Device Naming device_name: "esp-underhouselights" friendly_name: "Underhouse Lights" description_comment: "Underhouse Lights Control :: Sonoff 4Ch R2" device_area: "Underhouse" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant. # Project Naming project_name: "Sonoff Technologies.Sonoff 4Ch R2" # Project Details project_version: "v1.0" # Project V denotes release of yaml file, allowing checking of deployed vs latest version # Passwords api_key: !secret esp-api_key # unfortunately you can't use substitutions inside secrets names ota_pass: !secret esp-ota_pass # unfortunately you can't use substitutions inside secrets names static_ip_address: !secret esp-underhouselights_ip mqtt_main_command_topic: !secret mqtt_main_command_topic # Device Settings log_level: "ERROR" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE update_interval: "60s" # update time for for general sensors etc # MQTT Controls mqtt_device_name: "underhouse-lights-control" mqtt_main_topic: "${mqtt_main_command_topic}/${mqtt_device_name}" # Topic we will use to command stuff from external without HA # Switch Naming switch_1_name: "Underhouse Entrance Lights" button_1_name: "Button 1" relay_1_icon: "mdi:lightbulb" switch_2_name: "Underhouse Storage Lights" button_2_name: "Button 2" relay_2_icon: "mdi:lightbulb" switch_3_name: "Spare 3" button_3_name: "Button 3" relay_3_icon: "mdi:lightbulb" switch_4_name: "Spare 4" button_4_name: "Button 4" relay_4_icon: "mdi:lightbulb" ########################################################################################## # PACKAGES: Included Common Packages # https://esphome.io/components/esphome.html ########################################################################################## packages: common_wifi: !include file: common/network_common.yaml vars: local_static_ip_address: "${static_ip_address}" local_ota_pass: "${ota_pass}" common_api: !include file: common/api_common.yaml vars: local_api_key: "${api_key}" #common_webportal: !include # file: common/webportal_common.yaml common_mqtt: !include file: common/mqtt_common.yaml common_sntp: !include file: common/sntp_common.yaml common_general_sensors: !include file: common/sensors_common.yaml vars: local_friendly_name: "${friendly_name}" local_update_interval: "${update_interval}" ########################################################################################## # ESPHome # https://esphome.io/components/esphome.html ########################################################################################## esphome: name: "${device_name}" friendly_name: "${friendly_name}" comment: "${description_comment}" # Appears on the esphome page in HA area: "${device_area}" # platformio_options: # build_flags: # - "-Os" # optimize for size # - "-Wl,--gc-sections" # drop unused code/data # - "-fno-exceptions" # strip C++ exceptions # - "-fno-rtti" # strip C++ RTTI ########################################################################################## # ESP Platform and Framework # https://esphome.io/components/esp32.html ########################################################################################## esp8266: board: esp01_1m # The original sonoff basic restore_from_flash: True # restore some values on reboot preferences: flash_write_interval: 5min mdns: disabled: False ########################################################################################## # ESPHome Logging Enable # https://esphome.io/components/logger.html ########################################################################################## logger: level: "${log_level}" #INFO Level suggested, or DEBUG for testing baud_rate: 0 # set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM) #esp8266_store_log_strings_in_flash: false #tx_buffer_size: 64 ########################################################################################## # SWITCH COMPONENT # https://esphome.io/components/switch/ ########################################################################################## # Sonoff 4Ch R2 Relays are GPIO12,05,04,15 ############################################# switch: - platform: gpio id: relay1 name: "${switch_1_name}" pin: GPIO12 icon: "${relay_1_icon}" - platform: gpio id: relay2 name: "${switch_2_name}" pin: GPIO5 icon: "${relay_2_icon}" - platform: gpio id: relay3 name: "${switch_3_name}" pin: GPIO4 icon: "${relay_3_icon}" - platform: gpio id: relay4 name: "${switch_4_name}" pin: GPIO15 icon: "${relay_4_icon}" ########################################################################################## # BINARY SENSORS # https://esphome.io/components/binary_sensor/ ########################################################################################## # Sonoff 4Ch R2 Buttons are GPIO00,09,10,14 ############################################# binary_sensor: - platform: gpio id: button1 name: "${button_1_name}" pin: number: GPIO0 mode: INPUT_PULLUP inverted: True on_press: - switch.toggle: relay1 - platform: gpio id: button2 name: "${button_2_name}" pin: number: GPIO9 mode: INPUT_PULLUP inverted: True on_press: - switch.toggle: relay2 - platform: gpio id: button3 name: "${button_3_name}" pin: number: GPIO10 mode: INPUT_PULLUP inverted: True on_press: - switch.toggle: relay3 - platform: gpio id: button4 name: "${button_4_name}" pin: number: GPIO14 mode: INPUT_PULLUP inverted: True on_press: - switch.toggle: relay4 - platform: gpio pin: number: GPIO03 mode: INPUT_PULLUP inverted: true name: "${switch_1_name} Remote Switch" id: light_button_1 filters: - delayed_on: 20ms on_click: - min_length: 20ms max_length: 500ms then: - lambda: |- if (id(relay1).state) { // Relay is ON: turn it OFF id(relay1).turn_off(); } else { // Relay is OFF: turn it ON id(relay1).turn_on(); } - platform: gpio pin: number: GPIO02 mode: INPUT_PULLUP inverted: true name: "${switch_2_name} Remote Switch" id: light_button_2 filters: - delayed_on: 20ms on_click: - min_length: 20ms max_length: 500ms then: - lambda: |- if (id(relay2).state) { // Relay is ON: turn it OFF id(relay2).turn_off(); } else { // Relay is OFF: turn it ON id(relay2).turn_on(); } ########################################################################################## # STATUS LED # https://esphome.io/components/status_led.html ########################################################################################## # Sonoff Basic R1 LED is GPIO13 ############################################# status_led: pin: number: GPIO13 inverted: yes ########################################################################################## # MQTT COMMANDS # This adds device-specific MQTT command triggers to the common MQTT configuration. ########################################################################################## mqtt: on_message: # Relay 1 control - topic: "${mqtt_main_topic}/relay1/set" payload: "ON" then: - switch.turn_on: relay1 - topic: "${mqtt_main_topic}/relay1/set" payload: "OFF" then: - switch.turn_off: relay1