Why include file vars become lambdas?

Hi there! I’m trying to modularize my configuration for a shelly 1 to be able to make it work in detached mode, add multi click capabilities and send mqtt messages (with fallback to simple toggle when connection is down) based on the type of click.

The main config file has these lines:

substitutions:
  device_name: interruttore-camera
  friendly_name: Interruttore camera
  relay_id: shelly_relay
  button_id: button1

# ...

binary_sensor:
  - platform: gpio
    name: ${friendly_name} Input
    pin: GPIO5
    id: ${button_id}
    internal: true
    filters:
      delayed_on_off: 50ms
    on_multi_click: !include on-multi-click.yaml

on-multi-click.yaml contains the following

- timing:
    - ON for at most 0.5s
    - OFF for at most 0.5s
    - ON for at most 0.5s
    - OFF for at least 0.2s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: double

- timing:
    - OFF for at most 0.5s
    - ON for at most 0.5s
    - OFF for at most 0.5s
    - ON for at least 0.2s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: double

- timing:
    - ON for at most 0.5s
    - OFF for at least 0.5s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: single

- timing:
    - OFF for at most 0.5s
    - ON for at least 0.5s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: single

- timing:
    - ON for at least 1.5s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: long

- timing:
    - OFF for at least 1.5s
  then: !include
    file: publish-mqtt-or-toggle.yaml
    vars:
      payload: long

And then the publish-to-mqtt-or-toggle.yaml looks like this:

- if:
    condition:
      and:
        - wifi.connected:
        - mqtt.connected:
        - switch.is_on: ${relay_id}
    then:
      - mqtt.publish_json:
          topic: homie/${device_name}/${button_id}/status
          payload: ${payload}
    else:
      - switch.toggle: ${relay_id}

If I try to compile the configuration I got weird messages such as

publish-mqtt-or-toggle.yaml: In lambda function:
publish-mqtt-or-toggle.yaml:13:3: error: expected unqualified-id before '}' token
publish-mqtt-or-toggle.yaml: In lambda function:
publish-mqtt-or-toggle.yaml:12:7: error: 'single' was not declared in this scope; did you mean 'sinl'?

using esphome config I realized that the payload variable is turned into a lambda, and the “long”, “double” and “single” values are taken as C types instead of actual values.

substitutions:
  device_name: interruttore-camera
  friendly_name: Interruttore camera
  relay_id: shelly_relay
  button_id: button1
# ...
binary_sensor:
  - platform: gpio
    name: Interruttore camera Input
    pin:
      number: 5
      mode:
        input: true
        output: false
        open_drain: false
        pullup: false
        pulldown: false
        analog: false
      inverted: false
    id: button1
    internal: true
    filters:
      - delayed_on_off: 50ms
    on_multi_click:
      - timing:
          - state: true
            min_length: 0ms
            max_length: 500ms
          - state: false
            min_length: 0ms
            max_length: 500ms
          - state: true
            min_length: 0ms
            max_length: 500ms
          - state: false
            min_length: 200ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      double
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
      - timing:
          - state: false
            min_length: 0ms
            max_length: 500ms
          - state: true
            min_length: 0ms
            max_length: 500ms
          - state: false
            min_length: 0ms
            max_length: 500ms
          - state: true
            min_length: 200ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      double
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
      - timing:
          - state: true
            min_length: 0ms
            max_length: 500ms
          - state: false
            min_length: 500ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      single
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
      - timing:
          - state: false
            min_length: 0ms
            max_length: 500ms
          - state: true
            min_length: 500ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      single
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
      - timing:
          - state: true
            min_length: 1500ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      long
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
      - timing:
          - state: false
            min_length: 1500ms
        then:
          - if:
              condition:
                and:
                  - wifi.connected: {}
                  - mqtt.connected: {}
                  - switch.is_on:
                      id: shelly_relay
              then:
                - mqtt.publish_json:
                    topic: homie/interruttore-camera/button1/status
                    payload: !lambda |-
                      long
                    qos: 0
                    retain: false
              else:
                - switch.toggle:
                    id: shelly_relay
        invalid_cooldown: 1s
    disabled_by_default: false

Am I doing something wrong or hitting an unkown issue? I’ve searched through esphome issues and this forum, but I cannot find anything like this…

Oh boy, I’m stupid.

It has nothing to do with includes, I was using the wrong mqtt action, I just need mqtt.publish and not mqtt.publish_json!

Copy-paste error much? :sweat_smile:

1 Like