########################################################################################## ########################################################################################## # PMB Electronics 6 Button ESP32 Switch # V1.0 2025-08-04 Initial Version ########################################################################################## # PMB Details Page https://rcbeacon.com/blog/?p=5488 # NOTES # 1. To Flash via ESPHome, you likely have to connect to the computer, start the flash # process then hold down the I00 button. I think GPIO12 is preventing the flash process # 2. OTA flash to an Existing Tasmota device is likely a bit hard with and ESP32 as they # expect a signed Tasmota binary... and partition layout needs to be fixed anyway # 3. EspHome warns on compiling: "legacy adc driver is deprecated, please migrate to use # esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode # drivers respectively" [-Wcpp]"" # ########################################################################################## ########################################################################################## ########################################################################################## # SPECIFIC DEVICE VARIABLE SUBSTITUTIONS # If NOT using a secrets file, just replace "!secret my_key" with the password etc (in quotes) ########################################################################################## substitutions: # Device Naming device_name: "esp-6buttontest-pmb" #the filename should be the device_name.yaml friendly_name: "PMB Light Switch Test" description_comment: "Multi Button Wallswitch in standard AU/NZ flush size. 5 Buttons with LEDs and one Knob" device_area: "Lounge" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant. # 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-6buttontest-pmb_ip # Device Settings log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE update_interval: "60s" # update time for for general sensors etc pwm_freq: "1000Hz" ledc_bits: "12" # Switch Naming switch_1_name: "Red" switch_2_name: "White" switch_3_name: "Green" switch_4_name: "Orange" switch_5_name: "Blue" switch_5b_name: "Blue (Double)" switch_5c_name: "Blue (Hold)" # switch_6_name: "Spare" variable_1_name: "Dimmer" variable_1_full_scale: "3.14" # ADC reading when at 100% (adjust after calibration) variable_1_min_scale: "0.14" # volts at ADC for 0% (adjust after calibration) ########################################################################################## # PACKAGES: Included Common Packages # https://esphome.io/components/packages.html ########################################################################################## packages: common_wifi: !include file: common/network_common.yaml vars: local_device_name: "${device_name}" 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 vars: local_device_name: "${device_name}" 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} ########################################################################################## # ESP Platform and Framework # https://esphome.io/components/esp32.html ########################################################################################## # Suggest using ESP-IDF Framework. Changes might need need to be cable flashed here to # reset the partitioning ########################################################################################## esp32: board: esp32dev framework: type: esp-idf # "esp-idf" OR "arduino". version: recommended # recommended, latest or dev preferences: flash_write_interval: 5min # not too important but anything written for reboot mem will wear the flash ########################################################################################## # GLOBALS - State storage for vrelays ########################################################################################## globals: - id: vrelay_1_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_2_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_3_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_4_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_5_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_5b_state type: bool restore_value: yes initial_value: 'false' - id: vrelay_5c_state type: bool restore_value: yes initial_value: 'false' ############################################# # ESPHome Logging Enable # https://esphome.io/components/logger.html ############################################# logger: level: ${log_level} #INFO Level suggested, or DEBUG for testing ########################################################################################## # STATUS LED # https://esphome.io/components/status_led.html ########################################################################################## # This is usually the blue LED on the ESP32 Dev board # Status_LED can show ESPHome errors and warnings ########################################################################################## status_led: pin: number: GPIO2 inverted: yes ########################################################################################## # BINARY SENSORS # https://esphome.io/components/binary_sensor/ ########################################################################################## # These are the input buttons on GPIO. # No "mode: INPUT_PULLUP" needed as they have external pullups # Switches pull to GND so "inverted:true" for espHome to report ON when pressed. # They will toggle the virtual relays when pressed (the vrelays will toggle the LEDs) ########################################################################################## binary_sensor: - platform: gpio pin: number: GPIO19 mode: INPUT inverted: true name: "Button 1: ${switch_1_name}" filters: - delayed_on: 50ms - delayed_off: 50ms on_press: - switch.toggle: vrelay_1 - platform: gpio pin: number: GPIO35 mode: INPUT inverted: true name: "Button 2: ${switch_2_name}" filters: - delayed_on: 50ms - delayed_off: 50ms on_press: - switch.toggle: vrelay_2 - platform: gpio pin: number: GPIO05 mode: INPUT inverted: true name: "Button 3: ${switch_3_name}" filters: - delayed_on: 50ms - delayed_off: 50ms on_press: - switch.toggle: vrelay_3 - platform: gpio pin: number: GPIO33 mode: INPUT inverted: true name: "Button 4: ${switch_4_name}" filters: - delayed_on: 50ms - delayed_off: 50ms on_press: - switch.toggle: vrelay_4 - platform: gpio pin: number: GPIO16 mode: INPUT inverted: true name: "Button 5: ${switch_5_name}" on_multi_click: # Double click - timing: - ON for 50ms to 500ms - OFF for 50ms to 500ms - ON for 50ms to 500ms then: - switch.toggle: vrelay_5b # Hold - timing: - ON for at least 2s then: - switch.toggle: vrelay_5c # Single click - timing: - ON for 50ms to 500ms - OFF for at least 300ms then: - switch.toggle: vrelay_5 #- platform: gpio # pin: # number: GPIO12 # mode: INPUT # inverted: true # name: "Button 6: ${switch_6_name}" # filters: # - delayed_on: 50ms # - delayed_off: 50ms # on_press: # - switch.toggle: vrelay_6 ########################################################################################## # OUTPUT COMPONENT # https://esphome.io/components/light/index.html ########################################################################################## # An OUTPUT can be binary (0,1) or float, which is any value between 0 and 1. # PWM Outputs such as "ledc" are float. https://esphome.io/components/output/ledc.html # We could change frequency: "100Hz" and/or resolution: "12", but these are the default. ########################################################################################## output: - platform: ledc id: led1_output pin: GPIO21 frequency: ${pwm_freq} - platform: ledc id: led2_output pin: GPIO25 frequency: ${pwm_freq} - platform: ledc id: led3_output pin: GPIO18 frequency: ${pwm_freq} - platform: ledc id: led4_output pin: GPIO26 frequency: ${pwm_freq} - platform: ledc id: led5_output pin: GPIO17 frequency: ${pwm_freq} ########################################################################################## # LIGHT COMPONENT # https://esphome.io/components/light/index.html ########################################################################################## # We are just going to use simple monochromatic component as only one colour to PWM control # This doesn't actually do anything by itself, you need to tied it to an OUTPUT component. ########################################################################################## light: - platform: monochromatic id: led1 output: led1_output name: "LED 1: ${switch_1_name}" - platform: monochromatic id: led2 output: led2_output name: "LED 2: ${switch_2_name}" - platform: monochromatic id: led3 output: led3_output name: "LED 3: ${switch_3_name}" - platform: monochromatic id: led4 output: led4_output name: "LED 4: ${switch_4_name}" - platform: monochromatic id: led5 output: led5_output name: "LED 5: ${switch_5_name}" effects: - strobe: name: "Pulse 0.5s" colors: - state: ON brightness: 100% duration: 500ms - state: OFF duration: 500ms ########################################################################################## # SWITCH COMPONENT # https://esphome.io/components/switch/ ########################################################################################## # Normally SWITCH would be for things like a relay output and turning that on and off. # We are using it for virtual relays, that show up in Home Assistant etc, and toggling # them along with the LEDs when the buttons are pressed. # Separating them from the LEDs as outputs means we can do thinks like light blinking etc # but still leave the switch outputs toggled on or off. ########################################################################################## switch: - platform: template name: "Output 1: ${switch_1_name}" id: vrelay_1 lambda: |- return id(vrelay_1_state); turn_on_action: - light.turn_on: led1 - lambda: |- id(vrelay_1_state) = true; turn_off_action: - light.turn_off: led1 - lambda: |- id(vrelay_1_state) = false; - platform: template name: "Output 2: ${switch_2_name}" id: vrelay_2 lambda: |- return id(vrelay_2_state); turn_on_action: - light.turn_on: led2 - lambda: |- id(vrelay_2_state) = true; turn_off_action: - light.turn_off: led2 - lambda: |- id(vrelay_2_state) = false; - platform: template name: "Output 3: ${switch_3_name}" id: vrelay_3 lambda: |- return id(vrelay_3_state); turn_on_action: - light.turn_on: led3 - lambda: |- id(vrelay_3_state) = true; turn_off_action: - light.turn_off: led3 - lambda: |- id(vrelay_3_state) = false; - platform: template name: "Output 4: ${switch_4_name}" id: vrelay_4 lambda: |- return id(vrelay_4_state); turn_on_action: - light.turn_on: led4 - lambda: |- id(vrelay_4_state) = true; turn_off_action: - light.turn_off: led4 - lambda: |- id(vrelay_4_state) = false; - platform: template name: "Output 5: ${switch_5_name}" id: vrelay_5 lambda: |- return id(vrelay_5_state); turn_on_action: - light.turn_on: led5 - lambda: |- id(vrelay_5_state) = true; turn_off_action: - light.turn_off: led5 - lambda: |- id(vrelay_5_state) = false; #- switch.turn_off: vrelay_5c - platform: template name: "Output 5B: ${switch_5b_name}" id: vrelay_5b lambda: |- return id(vrelay_5b_state); turn_on_action: - logger.log: "vrelay_5b ON" - lambda: |- id(vrelay_5b_state) = true; turn_off_action: - logger.log: "vrelay_5b OFF" - lambda: |- id(vrelay_5b_state) = false; - platform: template name: "Output 5C: ${switch_5c_name}" id: vrelay_5c lambda: |- return id(vrelay_5c_state); turn_on_action: - light.turn_on: id: led5 effect: "Pulse 0.5s" - lambda: |- id(vrelay_5c_state) = true; turn_off_action: - light.turn_off: led5 - lambda: |- id(vrelay_5c_state) = false; #- switch.turn_off: vrelay_5 #- platform: template # name: "Output 6: ${switch_6_name}" # id: vrelay_6 # restore_mode: RESTORE_DEFAULT_OFF # optimistic: false # turn_on_action: # - light.turn_on: led6 # turn_off_action: # - light.turn_off: led6 sensor: # Raw ADC reading from GPIO36 - platform: adc pin: GPIO36 id: dimmer_raw name: "Dimmer Raw" attenuation: 11db update_interval: 0.5s on_value: then: - lambda: |- // Convert raw ADC (x) to volts float volts = x; // Apply min/max calibration float min_v = ${variable_1_min_scale}; float max_v = ${variable_1_full_scale}; // Calculate percentage between min and max float pct = ((volts - min_v) / (max_v - min_v)) * 100.0; // Clamp result between 0 and 100 if (pct > 100.0) pct = 100.0; if (pct < 0.0) pct = 0.0; // Publish to percentage sensor id(dimmer_percent).publish_state(pct); # Percentage sensor derived from raw ADC - platform: template id: dimmer_percent name: "${variable_1_name}" unit_of_measurement: "%" accuracy_decimals: 0 # External supply voltage from GPIO39 - platform: adc pin: GPIO39 name: "External Supply Voltage" id: supply_voltage attenuation: 11db # Up to ~3.9V at ADC pin update_interval: 2s filters: - calibrate_linear: # Format: raw_value -> real_voltage_at_pin - 00.66 -> 10.00 # Example: ADC reads 0.80 when pin is actually 3.20V - 00.99 -> 15.00 # Example: ADC reads 1.00 when pin is actually 4.00V - 01.30 -> 20.00 # Example: ADC reads 0.90 when pin is actually 3.60V #- multiply: 4.00 # Voltage divider ratio unit_of_measurement: "V"