Remote_receiver component question:

Hey, guys,
I’m facing a challenge that’s been going on for a week now.
I’m going to replace the board that controls the garage door motor with a board that I have more control over using HA and Node-red. For this I chose this board, as it already has inputs for a 433Mhz RF receiver and transmitter: https://bit.ly/46QLSzg
Using Esphome I made all the logic I need to work.
However, I have come across a problem that is keeping me up at night.
I don’t know how to add the codes for the other 6 controls in the house.
This without me having to repeat the entire button code 6 times. I believe there is a clean way to do this.

For example:



...
       rc_switch_raw:
         code: '110100001001010111100100101' //code control A
         protocol: 6
       rc_switch_raw:
         code: '2101000001001010111100100101' //B code control
         protocol: 6
       rc_switch_raw:
         code: '3101000001001010111100100101' //C code control
         protocol: 6
       rc_switch_raw:
         code: '4101000001001010111100100101' //D code control
         protocol: 6
...



Below is the code in question.


###############################
'''
esphome:
  name: kc868-a4
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

ota:
  password: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

wifi:
  ssid: "STARLINK"
  password: !secret passWordWifi
  manual_ip:
    static_ip: 192.168.88.88
    gateway: 192.168.88.88
    subnet: 255.255.255.0
  ap:
    ssid: "Kc-868-A4-Garagem"
    password: "8888888888888888"

captive_portal:

########### WEB SERVER #######################
web_server:
  port: 80

############ REMOTE_RECEIVER #################
remote_receiver:
#  - id: rc_receiver
  pin:
    number: GPIO19
    inverted: False
  dump: 
    - rc_switch
  tolerance: 35%
  filter: 250us
  idle: 4ms
  buffer_size: 2kb

########## REMOTE_TRANSMITTER ###############
remote_transmitter:
  pin: 21
  # RF uses a 100% carrier signal
  carrier_duty_percent: 100%

########### SWITCHS ###########################
switch:

########### PORTÃO GARAGEM ###########
  - platform: gpio
    pin: GPIO2
    restore_mode: ALWAYS_OFF
    name: "Open Garage"
    id: open_switch
    interlock: [close_switch]
 
  - platform: gpio
    pin: GPIO15
    restore_mode: ALWAYS_OFF
    name: "Close Garage"
    id: close_switch
    interlock: [open_switch]

########### CAPACITOR #################
  - platform: gpio
    pin: GPIO18
    inverted: False
    restore_mode: ALWAYS_OFF
    name: "Capacitor"
    id: in_capacitor
    icon: "mdi:engine"

################ GLOBAL VARIABLES ##############
globals:
  - id: state_gate
    type: std::string
    initial_value: '"off"'


############### SENSOR BINARY  #######################
binary_sensor:
  - platform: template
    name: Gate
    id: gate

############## OPEN BUTTON ##########################
  - platform: remote_receiver
    id: CM1
    on_press:

################## DISABLE CAPACITOR PER 950ms      
     - then:
        - if:
            condition:
              lambda: 'return id(state_gate) == "off";'
            then:
              - switch.turn_off: in_capacitor
              - delay: 2000ms
              - switch.turn_on: in_capacitor
            else:
              - logger.log: ' ATTENTION, THIS IS A TEST'

################## STARTS OPENING
     - then:
        - if:
            condition:
              lambda: 'return id(state_gate) == "off";'
            then:  
              - lambda: 'id(state_gate) = "on";'
              - switch.turn_off: close_switch            
              - switch.turn_on: open_switch
              - delay: 9.5s
              - switch.turn_off: in_capacitor
              - delay: 2000ms
              - switch.turn_off: open_switch
              - binary_sensor.template.publish:
                  id: gate
                  state: on
              
            else:
              - logger.log: 'ATTENTION, THIS IS A TEST'

    rc_switch_raw:
      code: '1101000001001010111100010101'
      protocol: 6

############### CLOSED BUTTON ###################
  - platform: remote_receiver
    id: CM2
    on_press:
################## DISABLE THE CAPACITOR PER 950ms
     - then:
        - if:
            condition:
              lambda: 'return id(state_gate) == "on";'      
            then:
              - switch.turn_off: in_capacitor
              - delay: 2000ms
              - switch.turn_on: in_capacitor
            else:
              - logger.log: 'ATTENTION, THIS IS A TEST'

################## INICIA A ABERTURA
     - then:
        - if:
            condition:
              lambda: 'return id(state_gate) == "on";'
            then:
              - lambda: 'id(state_gate) = "off";'
              - switch.turn_off: open_switch
              - switch.turn_on: close_switch
              - delay: 11s
              - switch.turn_off: in_capacitor
              - delay: 1000ms              
              - switch.turn_off: close_switch
              - binary_sensor.template.publish:
                  id: gate
                  state: off
              
            else:
              - logger.log: 'ATTENTION, THIS IS A TEST'              

    rc_switch_raw:
      code: '1101000001001010111100100101'
      protocol: 6

Guys, after a few days of racking my brain and researching, I found this way to improve the code.
I’m sure there are more elegant ways to get the result I would like, but for now, I’m satisfied.

Thanks

:wink:

substitutions:

code_open_control01: "1101000001001010111100100101"

code_open_control02: "0100101000101101111100100101"

code_open_control03: "1101010101100010111100100101"

code_closed_control01: "1101000001001010111100010101"

code_closed_control02: "0100101000101101111100010101"

code_closed_control03: "1101010101100010111100010101"

esphome:

name: kc868-a4

platform: ESP32

board: esp32dev

# Enable logging

logger:

#kc868-a4

# Enable Home Assistant API

api:

encryption:

key: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"

ota:

password: "XXXXXXXXXXXXXXXXXXXXXXXXX"

wifi:

ssid: "STARLINK"

password: !secret passWordWifi

manual_ip:

static_ip: 192.168.88.88

gateway: 192.168.88.88

subnet: 255.255.255.0

ap:

ssid: "Kc-868-A4-Garage"

password: "XXXXXXXXXX"

captive_portal:

############## WEB SERVER #####################

web_server:

port: 80

############# REMOTE_RECEIVER ###############

remote_receiver:

# - id: rc_receiver

pin:

number: GPIO19

inverted: False

dump:

- rc_switch

tolerance: 35%

filter: 250us

idle: 4ms

buffer_size: 2kb

######## REMOTE_TRANSMITTER ################

remote_transmitter:

pin: 21

# RF uses a 100% carrier signal

carrier_duty_percent: 100%

######## SWITCHS ##############################

switch:

########### GATE GARAGE ###########

- platform: gpio

pin: GPIO2

restore_mode: ALWAYS_OFF

name: "Open Garage"

id: open_switch

interlock: [close_switch]

- platform: gpio

pin: GPIO15

restore_mode: ALWAYS_OFF

name: "Close Garage"

id: close_switch

interlock: [open_switch]

############ SIRENE GARAGE ##########

- platform: gpio

pin: GPIO4

restore_mode: ALWAYS_OFF

name: "Sirene garage"

icon: "mdi:alarm-bell"

########### LIGHT GARAGE ###########

- platform: gpio

pin: GPIO5

restore_mode: ALWAYS_OFF

name: "Light Garage"

icon: "mdi:lightbulb-on-outline"

########### CAPACITOR #################

- platform: gpio

pin: GPIO23

inverted: False

restore_mode: ALWAYS_OFF

name: "Capacitor"

id: in_capacitor

icon: "mdi:engine"

########### IR ########################

- platform: gpio

pin: GPIO18

restore_mode: ALWAYS_OFF

name: "IR"

icon: "mdi:bell"

############ VARIABLES GLOBALS ############

globals:

- id: state_gate

type: std::string

initial_value: '"off"'

############# SENSOR BINARY ###################

binary_sensor:

- platform: template

name: Gate

id: gate

############# BUTTON OPEN ######################

- <<: !include common/sensor_binary_rc_open_control01.yaml

- <<: !include common/sensor_binary_rc_open_control02.yaml

- <<: !include common/sensor_binary_rc_open_control03.yaml

############# BUTTON CLOSE ############################################

- <<: !include common/sensor_binary_rc_close_control01.yaml

- <<: !include common/sensor_binary_rc_close_control02.yaml

- <<: !include common/sensor_binary_rc_close_control03yaml