Hi
I have built a button panel with the following features:
- 8 push buttons
- each button can do single click, double click and hold
- each button has a led to signal mode
- a buzzer (rtttl)
- everything controlled by an ESP32
Having this many components and functions gives me a very long yaml file.
I looking for ways to reduce the repeated parts - actually looking for a discussion for the best approach and to learn something…
So I would love to get some tips and inputs to the below.
All the basic stuff is hidden away in an injected file called .base.yaml - I have included in at the end for completeness.
substitutions:
device_name: kitchen-buttons
friendly_name: Bedlamp
ip_node: '222'
packages:
base: !include .base.yaml
esphome:
name: ${device_name}
esp32:
board: node32s
framework:
type: arduino
switch:
################################################
#
# Switch 1
#
#################################################
- platform: template
name: "Toggle 1 single"
id: switch_1_single
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_1
effect: "single"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_1
- platform: template
name: "Toggle 1 double"
id: switch_1_double
optimistic: true
icon: mdi:numeric-1-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_1
effect: "double"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_1
- platform: template
name: "Toggle 1 long"
id: switch_1_long
internal: true
optimistic: true
icon: mdi:chat-sleep
on_turn_on:
- light.turn_on:
id: button_led_1
effect: "hold"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_1
################################################
#
# Switch 2
#
#################################################
- platform: template
name: "Toggle 2 single"
id: switch_2_single
optimistic: true
icon: mdi:numeric-2-box
on_turn_on:
- light.turn_on:
id: button_led_2
effect: "single"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_2
- platform: template
name: "Toggle 2 double"
id: switch_2_double
optimistic: true
icon: mdi:numeric-2-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_2
effect: "double"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_2
- platform: template
name: "Toggle 2 long"
id: switch_2_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_2
effect: "hold"
brightness: 50%
on_turn_off:
- light.turn_off: button_led_2
################################################
#
# Switch 3
#
#################################################
- platform: template
name: "Toggle 3 single"
id: switch_3_single
optimistic: true
icon: mdi:numeric-3-box
on_turn_on:
- light.turn_on:
id: button_led_3
effect: "single"
on_turn_off:
- light.turn_off: button_led_3
- platform: template
name: "Toggle 3 double"
id: switch_3_double
optimistic: true
icon: mdi:numeric-3-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_3
effect: "double"
on_turn_off:
- light.turn_off: button_led_3
- platform: template
name: "Toggle 3 long"
id: switch_3_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_3
effect: "hold"
on_turn_off:
- light.turn_off: button_led_3
################################################
#
# Switch 4
#
#################################################
- platform: template
name: "Toggle 4 single"
id: switch_4_single
optimistic: true
icon: mdi:numeric-4-box
on_turn_on:
- light.turn_on:
id: button_led_4
effect: "single"
on_turn_off:
- light.turn_off: button_led_4
- platform: template
name: "Toggle 4 double"
id: switch_4_double
optimistic: true
icon: mdi:numeric-4-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_4
effect: "double"
on_turn_off:
- light.turn_off: button_led_4
- platform: template
name: "Toggle 4 long"
id: switch_4_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_4
effect: "hold"
on_turn_off:
- light.turn_off: button_led_4
################################################
#
# Switch 5
#
#################################################
- platform: template
name: "Toggle 5 single"
id: switch_5_single
optimistic: true
icon: mdi:numeric-5-box
on_turn_on:
- light.turn_on:
id: button_led_5
effect: "single"
on_turn_off:
- light.turn_off: button_led_5
- platform: template
name: "Toggle 5 double"
id: switch_5_double
optimistic: true
icon: mdi:numeric-5-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_5
effect: "double"
on_turn_off:
- light.turn_off: button_led_5
- platform: template
name: "Toggle 5 long"
id: switch_5_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_5
effect: "hold"
on_turn_off:
- light.turn_off: button_led_5
################################################
#
# Switch 6
#
#################################################
- platform: template
name: "Toggle 6 single"
id: switch_6_single
optimistic: true
icon: mdi:numeric-6-box
on_turn_on:
- light.turn_on:
id: button_led_6
effect: "single"
on_turn_off:
- light.turn_off: button_led_6
- platform: template
name: "Toggle 6 double"
id: switch_6_double
optimistic: true
icon: mdi:numeric-6-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_6
effect: "double"
on_turn_off:
- light.turn_off: button_led_6
- platform: template
name: "Toggle 6 long"
id: switch_6_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_6
effect: "hold"
on_turn_off:
- light.turn_off: button_led_6
################################################
#
# Switch 7
#
#################################################
- platform: template
name: "Toggle 7 single"
id: switch_7_single
optimistic: true
icon: mdi:numeric-7-box
on_turn_on:
- light.turn_on:
id: button_led_7
effect: "single"
on_turn_off:
- light.turn_off: button_led_7
- platform: template
name: "Toggle 7 double"
id: switch_7_double
optimistic: true
icon: mdi:numeric-7-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_7
effect: "double"
on_turn_off:
- light.turn_off: button_led_7
- platform: template
name: "Toggle 7 long"
id: switch_7_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_7
effect: "hold"
on_turn_off:
- light.turn_off: button_led_7
################################################
#
# Switch 8
#
#################################################
- platform: template
name: "Toggle 8 single"
id: switch_8_single
optimistic: true
icon: mdi:numeric-8-box
on_turn_on:
- light.turn_on:
id: button_led_8
effect: "single"
on_turn_off:
- light.turn_off: button_led_8
- platform: template
name: "Toggle 8 double"
id: switch_8_double
optimistic: true
icon: mdi:numeric-8-box-multiple
on_turn_on:
- light.turn_on:
id: button_led_8
effect: "double"
on_turn_off:
- light.turn_off: button_led_8
- platform: template
name: "Toggle 8 long"
id: switch_8_long
internal: true
optimistic: true
icon: mdi:numeric-1-box
on_turn_on:
- light.turn_on:
id: button_led_8
effect: "hold"
on_turn_off:
- light.turn_off: button_led_8
binary_sensor:
################################################
#
# Button 1
#
#################################################
- name: "Button 1"
id: button_1
internal: true
platform: gpio
pin:
number: 23
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_1_double).toggle();
- logger.log: "Button 1 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_1_single).toggle();
- logger.log: "Button 1 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_1_single).turn_off();
- lambda: !lambda id(switch_1_double).turn_off();
- logger.log: "Button 1 Long"
################################################
#
# Button 2
#
#################################################
- name: "Button 2"
id: button_2
internal: true
platform: gpio
pin:
number: 22
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_2_double).toggle();
- logger.log: "Button 2 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_2_single).toggle();
- logger.log: "Button 2 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_2_single).turn_off();
- lambda: !lambda id(switch_2_double).turn_off();
- logger.log: "Button 2 Long"
################################################
#
# Button 3
#
#################################################
- name: "Button 3"
id: button_3
internal: true
platform: gpio
pin:
number: 4
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_3_double).toggle();
- logger.log: "Button 3 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_3_single).toggle();
- logger.log: "Button 3 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_3_single).turn_off();
- lambda: !lambda id(switch_3_double).turn_off();
- logger.log: "Button 3 Long"
################################################
#
# Button 4
#
#################################################
- name: "Button 4"
id: button_4
internal: true
platform: gpio
pin:
number: 3
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_4_double).toggle();
- logger.log: "Button 4 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_4_single).toggle();
- logger.log: "Button 4 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_4_single).turn_off();
- lambda: !lambda id(switch_4_double).turn_off();
- logger.log: "Button 4 Long"
################################################
#
# Button 5
#
#################################################
- name: "Button 5"
id: button_5
internal: true
platform: gpio
pin:
number: 19
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_5_double).toggle();
- logger.log: "Button 5 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_5_single).toggle();
- logger.log: "Button 5 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_5_single).turn_off();
- lambda: !lambda id(switch_5_double).turn_off();
- logger.log: "Button 5 long"
################################################
#
# Button 6
#
#################################################
- name: "Button 6"
id: button_6
internal: true
platform: gpio
pin:
number: 18
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_6_double).toggle();
- logger.log: "Button 6 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_6_single).toggle();
- logger.log: "Button 6 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_6_single).turn_off();
- lambda: !lambda id(switch_6_double).turn_off();
- logger.log: "Button 6 long"
################################################
#
# Button 7
#
#################################################
- name: "Button 7"
id: button_7
internal: true
platform: gpio
pin:
number: 5
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_7_double).toggle();
- logger.log: "Button 7 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_7_single).toggle();
- logger.log: "Button 7 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_7_single).turn_off();
- lambda: !lambda id(switch_7_double).turn_off();
- logger.log: "Button 7 long"
################################################
#
# Button 8
#
#################################################
- name: "Button 8"
id: button_8
internal: true
platform: gpio
pin:
number: 17
mode:
input: true
pullup: true
inverted: true
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 0.3s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: !lambda id(switch_8_double).toggle();
- logger.log: "Button 8 Double"
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_8_single).toggle();
- logger.log: "Button 8 Single"
- timing:
- ON for 1s to 2s
- OFF for at least 0.5s
then:
- lambda: !lambda id(switch_8_single).turn_off();
- lambda: !lambda id(switch_8_double).turn_off();
- logger.log: "Button 8 long"
output:
- platform: ledc
pin: 16
id: pin_led_1
- platform: ledc
pin: 27
id: pin_led_2
- platform: ledc
pin: 14 #25
id: pin_led_3
- platform: ledc
pin: 26 #33
id: pin_led_4
- platform: ledc
pin: 25 #32
id: pin_led_5
- platform: ledc
pin: 13
id: pin_led_6
- platform: ledc
pin: 33 #14
id: pin_led_7
- platform: ledc
pin: 32 #27
id: pin_led_8
- platform: ledc
pin: 2
id: rtttl_out
light:
################################################
#
# Light 1
#
#################################################
- platform: monochromatic
output: pin_led_1
name: "Button 1 LED"
id: button_led_1
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 2
#
#################################################
- platform: monochromatic
output: pin_led_2
name: "Button 2 LED"
id: button_led_2
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 3
#
#################################################
- platform: monochromatic
output: pin_led_3
name: "Button 3 LED"
id: button_led_3
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 4
#
#################################################
- platform: monochromatic
output: pin_led_4
name: "Button 4 LED"
id: button_led_4
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 5
#
#################################################
- platform: monochromatic
output: pin_led_5
name: "Button 5 LED"
id: button_led_5
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 6
#
#################################################
- platform: monochromatic
output: pin_led_6
name: "Button 6 LED"
id: button_led_6
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 7
#
#################################################
- platform: monochromatic
output: pin_led_7
name: "Button 7 LED"
id: button_led_7
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
################################################
#
# Light 8
#
#################################################
- platform: monochromatic
output: pin_led_8
name: "Button 8 LED"
id: button_led_8
internal: true
effects:
- pulse:
name: "single"
transition_length: 2s
update_interval: 4s
- pulse:
name: "double"
transition_length: 1s
update_interval: 2s
- pulse:
name: "hold"
transition_length: 0.5s
update_interval: 1s
rtttl:
output: rtttl_out
on_finished_playback:
- logger.log: 'Song ended!'
api:
services:
- service: play_rtttl
variables:
song_str: string
then:
- rtttl.play:
rtttl: !lambda 'return song_str;'
base.yaml:
logger:
api:
ota:
password: !secret esphome_api_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
use_address: 192.168.0.${ip_node}
manual_ip:
static_ip: 192.168.0.${ip_node}
gateway: 192.168.0.100
subnet: 255.255.255.0
dns1: 192.168.0.100
ap:
ssid: ${device_name} conf AP
password: !secret fallback_password
captive_portal:
web_server:
port: 80
time:
- platform: homeassistant
id: homeassistant_time
sensor:
- platform: wifi_signal
name: ${device_name} WiFi Sensor
update_interval: 60s
- platform: uptime
name: ${device_name} Uptime Sensor
filters:
- lambda: return x / 3600;
unit_of_measurement: "hours"
accuracy_decimals: 2
binary_sensor:
- platform: status
name: "${device_name} Status"