Lambda inside !include issue?

None of the buttons on the MJ-SD01 seem to work when I use an an !include.
Maybe it has something to do with the lambdas?

If I use !include .base.light.mjdimmer.yaml, it validates and uploads no problem, but none of the buttons respond to input.

# Basic Config
substitutions:
#   # https://esphome.io/guides/configuration-types.html#substitutions
  device_name: mjd-officelight   # hostname & entity_id
  friendly_name: Office   # Displayed in HA frontend
  pwm_min_power: 15% # keep dimming functional at lowest levels
  no_delay: 0s # transition when changing dimmer_lvl & relay delay
  transition_length: .5s # transition when turning on/off
  long_press_min: .4s # minimum time to activate long-press action
  long_press_max: 2s # maximum time to activate long-press action
  long_press_up: 100% # long press brightness
  long_press_down: 33% # long press brightness
  long_press_main: 50% # long press brightness
  # Number of incremental steps between 0 and 100% intensity with the up/down
  # buttons.
  steps: "8"
  gamma_correct: "2.0" # Default gamma of 2.8 is generally too high.

esphome:
  # https://esphome.io/components/esphome
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m
  esp8266_restore_from_flash: true

<<: !include .base8266.yaml
<<: !include .base.light.mjdimmer.yaml

If i replace <<: !include .base.light.mjdimmer.yaml with the actual yaml the buttons respond.

binary_sensor:
  - platform: gpio
    # https://esphome.io/components/binary_sensor/gpio.html
    #name: "${friendly_name} Up Button"
    id: up_button
    pin:
      number: GPIO0
      inverted: True
      mode: INPUT_PULLUP
    on_press:
      # https://esphome.io/components/binary_sensor/index.html#on-press
      - if:
          condition:
            light.is_on: dimmer
          then:
            - lambda: !lambda |-
                // Similar to light.dim_relative but add the flashing.
                auto val = id(dimmer).remote_values.get_brightness();
                if (val >= (${steps}.0f-1.0f)/${steps}.0f) {
                  val = 1.0f;
                } else {
                  val += 1.0f/${steps}.0f;
                }
                auto call = id(dimmer).turn_on();
                call.set_brightness(val);
                call.perform();
                if (val == 1.f) {
                  id(flash_lights).execute();
                }
          else:
            - light.turn_on:
                id: dimmer
                brightness: "${long_press_up}"
    on_click:
      # https://esphome.io/components/binary_sensor/index.html#on-click
      min_length: ${long_press_min}
      max_length: ${long_press_max}
      then:
        - light.turn_on:
            id: dimmer
            brightness: "${long_press_up}"
  - platform: gpio
    # https://esphome.io/components/binary_sensor/gpio.html
    #name: "${friendly_name} Down Button"
    id: down_button
    pin:
      number: GPIO1
      inverted: True
      mode: INPUT_PULLUP
    on_press:
      # https://esphome.io/components/binary_sensor/index.html#on-press
      - if:
          condition:
            light.is_on: dimmer
          then:
            - lambda: !lambda |-
                // Similar to light.dim_relative but add the flashing.
                auto val = id(dimmer).remote_values.get_brightness();
                if (val <= 1.0f/${steps}.0f) {
                  val = .01f;
                } else {
                  val -= 1.0f/${steps}.0f;
                }
                auto call = id(dimmer).turn_on();
                call.set_brightness(val);
                call.perform();
                if (val == 0.01f) {
                  id(flash_lights).execute();
                }
          else:
            - light.turn_on:
                id: dimmer
                brightness: "${long_press_down}"
    on_click:
      # https://esphome.io/components/binary_sensor/index.html#on-click
      min_length: ${long_press_min}
      max_length: ${long_press_max}
      then:
        - light.turn_on:
            id: dimmer
            brightness: "${long_press_down}"
  - platform: gpio
    # https://esphome.io/components/binary_sensor/gpio.html
    #name: ${friendly_name} Main Button
    id: main_button
    pin:
      number: GPIO15
      mode: INPUT_PULLUP
    on_press:
      # TODO: Use "light.toggle: dimmer" instead of the code below if you want
      # to keep the previous brightness by default.
      # https://esphome.io/components/binary_sensor/index.html#on-press
      - if:
          condition:
            light.is_on: dimmer
          then:
            - light.turn_off: dimmer
          else:
            - light.turn_on:
                id: dimmer
                brightness: "${long_press_main}"
    on_click:
      # https://esphome.io/components/binary_sensor/index.html#on-click
      min_length: ${long_press_min}
      max_length: ${long_press_max}
      then:
        - light.turn_on:
            id: dimmer
            brightness: "${long_press_main}"

light:
  - platform: status_led
    id: ledred
    pin:
      number: GPIO4
      inverted: True
  - platform: monochromatic
    # https://esphome.io/components/light/monochromatic.html
    name: "${friendly_name}"
    id: dimmer
    output: pwm
    default_transition_length: ${no_delay}
    gamma_correct: "${gamma_correct}"
    effects:
      - flicker:
          name: "Flicker"
          alpha: 90%
          intensity: 25%
      - strobe:
          name: "Fast Pulse"
          colors:
            - state: true
              brightness: 100%
              duration: 500ms
            - brightness: 1%
              duration: 750ms
    on_state:
      - script.execute: set_lights

script:
  - id: set_lights
    then:
      - lambda: |-
          if (id(dimmer).remote_values.get_state() == 0.f) {
            id(led2).turn_off();
            id(led3).turn_off();
            id(led4).turn_off();
            id(led5).turn_off();
            // Comment the following line out if you don't want the red LED when
            // the dimmer is off.
            id(ledred).turn_on().perform();
            return;
          }
          id(ledred).turn_off().perform();
          auto val = id(dimmer).remote_values.get_brightness();
          if (val >= .1f) {
            id(led2).turn_on();
          } else {
            id(led2).turn_off();
          }
          if (val >= .36f) {
            id(led3).turn_on();
          } else {
            id(led3).turn_off();
          }
          if (val >= .73f) {
            id(led4).turn_on();
          } else {
            id(led4).turn_off();
          }
          if (val >= .9f) {
            id(led5).turn_on();
          } else {
            id(led5).turn_off();
          }
  - id: flash_lights
    then:
      - output.turn_on: led2
      - output.turn_on: led3
      - output.turn_on: led4
      - output.turn_on: led5
      - delay: 150ms
      - output.turn_off: led2
      - output.turn_off: led3
      - output.turn_off: led4
      - output.turn_off: led5
      - delay: 150ms
      - output.turn_on: led2
      - output.turn_on: led3
      - output.turn_on: led4
      - output.turn_on: led5
      - delay: 150ms
      - output.turn_off: led2
      - output.turn_off: led3
      - output.turn_off: led4
      - output.turn_off: led5
      - delay: 150ms
      - script.execute: set_lights

output:
  - platform: esp8266_pwm
    # https://esphome.io/components/output/esp8266_pwm.html
    power_supply: relay
    pin: GPIO13
    id: pwm
    # Even lower frequency can be used. 120 Hz works fine in 60 Hz countries.
    frequency: 300 Hz
    min_power: ${pwm_min_power}
  - platform: gpio
    # https://esphome.io/components/output/gpio.html
    id: led2
    pin: GPIO14
    inverted: true
  - platform: gpio
    id: led3
    pin: GPIO12
    inverted: true
  - platform: gpio
    id: led4
    pin: GPIO5
    inverted: true
  - platform: gpio
    id: led5
    pin: GPIO3
    inverted: true

power_supply:
  - id: relay
    # https://esphome.io/components/power_supply.html
    pin:
      number: GPIO16
      inverted: True
    enable_time: ${no_delay}
    keep_on_time: ${no_delay}

!include .base8266.yaml


wifi:
  # https://esphome.io/components/wifi
  domain: !secret domain
  power_save_mode: none
  networks:
  - ssid: !secret iot_wifi_ssid
    password: !secret iot_wifi_pass
#    hidden: true
  ap:
    ssid: ${friendly_name}_AP
    password: !secret wifi_pass
#    channel: 1
    manual_ip:
      static_ip: 192.168.1.1
      gateway: 192.168.1.1
      subnet: 255.255.255.0

# Enable Home Assistant API
api:

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

logger:
  baud_rate: 0 #disable UART logging since we aren't connected to GPIO1 TX
  # https://esphome.io/components/logger

ota:
  password: !secret default_password
  # https://esphome.io/components/ota

time:
  - platform: sntp
    id: sntp_time
    timezone: US/Central
    servers: 
      - !secret sntp_server1
      - !secret sntp_server2

binary_sensor:
  # Reports if this device is Connected or not
  - platform: status
    name: ${friendly_name} Status
      
sensor:
  - platform: wifi_signal
    name: ${friendly_name} Signal
    update_interval: 60s
  # Reports how long the device has been powered (in minutes)
  - platform: uptime
    name: ${friendly_name} Uptime
    filters:
      - lambda: return x / 60.0;
    unit_of_measurement: minutes

text_sensor:
  # Reports the ESPHome Version with compile date
  - platform: version
    name: ${friendly_name} ESPHome Version
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP Address
    ssid:
      name: ${friendly_name} Connected SSID
    bssid:
      name: ${friendly_name} Connected BSSID
    mac_address:
      name: ${friendly_name} Mac Wifi Address
    
switch:
- platform: restart
  name: ${friendly_name} Reboot

Both of your included files have binary_sensor: blocks so I believe the this is what is creating the conflict. To merge both blocks together, they need to be included as packages:

packages:
  base: !include .base8266.yaml
  dimmer: !include .base.light.mjdimmer.yaml
1 Like

This works for me:

substitutions:
  name_of_board: test_board
  ip_address: <redacted>
  ota_password: <redacted>
  baud_logger: "0"
############################################
#YOU SHOULD NOT NEED TO EDIT BELOW THIS LINE
############################################
esphome:
  name: ${name_of_board}
  platform: ESP8266
  board: d1_mini

# Adding packages
packages:
  wi_fi: !include common/core_components/wifi.yaml # Configures WiFi, AP and Manual IP
  portal_ota: !include common/core_components/ota_portal.yaml # Configures Captive portal and OTA
  logger: !include common/core_components/logger.yaml # Configures logger, level & baud_rate
  basic_sensors: !include common/basic_sensors.yaml

1 Like

Thank you everyone! I was not aware of packages.