How do I label and use anchors?

This is a follow-up to my thread about what I can use instead of copy.

I’m using anchors and aliases so I can use one component as a parent class for other components that are similar. Here’s the part of the YAML file I’m having problems with:

binary_sensor:
  AnchorSwitchMain: &AnchorSwitch
  - platform: gpio
    id: "SwitchMain"
    name: "SwitchMain"
    web_server: &AnchorSwitchWebServer
      sorting_group_id: sorting_group_switchmain
      sorting_weight: 10
    pin: &AnchorSwitchPin
      number: ${SwitchMainPin}
      mode:
        input: true
        pulldown: true
    filters:
      delayed_on: 10ms
    device_class: power
    on_press:
      - logger.log: "DEBUG PRESS 1:     ==> Switch pressed"
      - logger.log:
          format: "DEBUG PRESS 2: Delay value: %.0f"
          args: 'id(TestDelay).state'
    on_release:
      - logger.log:
          format: "DEBUG RELEASE 1: Delay value: %.0f"
          args: 'id(TestDelay).state'
      - logger.log: "DEBUG RELEASE 2:    <== Switch released"

  Switch01:
    <<: *AnchorSwitch
    id: "Switch01"
    name: "Switch 1"
    web_server:
      << : *AnchorSwitchWebServer
      sorting_group_id: sorting_group_switch1
    pin:
      << : *AnchorSwitchPin
      number: ${Switch1Pin}

The problem is the 2nd line:
AnchorSwitchMain: &AnchorSwitch

I’m not sure if I’m doing this correctly. The section I’m attempting to reference si the entire section from the 3rd line to the last line (with logging debug statements) before the blank line and Switch01. I’m not worried about TestDelay at this point - that’s defined elsewhere.

What is the correct way to specify the component AnchorSwitchMain so I can use it as an alias later (like for Switch01)?

(Also note that I’m using 2 more anchors within AnchorSwitchMain, both AnchorSwitchWebserver and AnchorSwitchPin. I’ve been working with this and haven’t had a problem using those two anchors within the other anchor, but I am having trouble and suspect I’m not doing something right at the top of AnchorSwitchMain. It’s easy to add the anchor to the other two, since the anchor goes after teh name of the upcoming dictionary or list, but I’m not clear what the reasoning is and where the anchor for AnchorSwitchMain should be.


Edited to add:

Here’s a second example:

number:
  SwitchMainDelay: &AnchorDelay
    - platform: template
    id: "SwitchMainDelay"
    name: "Main Switch Delay"
    optimistic: true
    min_value: 0
    max_value: 600
    step: 1
    initial_value: 0
    web_server: &AnchorDelayWebServer
      sorting_group_id: sorting_group_switchmain
      sorting_weight: 30

  Switch01Delay:
    <<: *AnchorDelay
    id: "Switch01Delay"
    name: "Switch 01 Delay"
    web_server:
      << : *AnchorDelayWebServer
      sorting_group_id: sorting_group_switch1

  Switch02Delay:
    <<: *AnchorDelay
    id: "Switch02Delay"
    name: "Switch 2 Delay"
    web_server:
      << : *AnchorDelayWebServer
      sorting_group_id: sorting_group_switch2

In this formatting, I get an error that number: needs a platform. I tried swapping the order and putting SwitchMainDelay: &AnchorDelay" after –platform: template` and it didn’t help.

I’m not at all clear how I can label that section and specify the platform.

Probably this is what You looking for:

.number: &AnchorDelay
    optimistic: true
    min_value: 0
    max_value: 600
    step: 1
    initial_value: 0
    web_server: &AnchorDelayWebServer
      sorting_group_id: sorting_group_switchmain
      sorting_weight: 30

number:
  - platform: template
    <<: *AnchorDelay
    id: "SwitchMainDelay"
    name: "Main Switch Delay"

  - platform: template
    <<: *AnchorDelay
    id: "Switch01Delay"
    name: "Switch 01 Delay"
    web_server:
      << : *AnchorDelayWebServer
      sorting_group_id: sorting_group_switch1

  - platform: template
    <<: *AnchorDelay
    id: "Switch02Delay"
    name: "Switch 2 Delay"
    web_server:
      << : *AnchorDelayWebServer
      sorting_group_id: sorting_group_switch2

Seems to work - still testing it. What is this called? I tried to find “dot at start of line YAML” and other searches (including replacing dot with “period”) but couldn’t find anything on this and didn’t see that notation on any page I found about anchors.

AFAIK anchor usage not described on ESPHome site, but there is some examples can be found in Internet. Occasionally found that article: YAML anchors undefined in included yaml · Issue #1011 · esphome/issues · GitHub

I’ve found some examples of YAML anchors when I searched, but so many were for one item. There were a lot of very limited examples and I was able to put together bits and pieces to get a better explanation, but could not find a clear explanation anywhere.

I’m beginning to find that part of my issue with learning ESPHome is with YAML being poorly documented. Yes, there is one “official” site I found, but it’s the kind of specs that you’re either an expert and can read 'em easily or it takes hours to figure out what they’re saying. Very little out there for a lot of aspects of YAML. (For instance, it took me over a week to clarify a lot of how variables or substitutions are used. I’m still not at all clear how to reference or use a global, much less how to change a global’s value.)

I’ve spent time on Google and even asked about this on Stack Exchange.

Apparently the dot/period at the start of the line is not a YAML thing, but I can’t find documentation about it in ESPHome. Is using something like:

.number: &AnchorDelay
    optimistic: true
    min_value: 0
    max_value: 600
    step: 1
    initial_value: 0
    web_server: &AnchorDelayWebServer
      sorting_group_id: sorting_group_switchmain
      sorting_weight: 30

with the .number on a line by itself, the line starting with a dot, an ESPHome thing? If so, where is the documentation on it? I don’t know the term for it, so I can’t effectively search for it.

Thanks, I was looking for the same solution. My best guess is that the leading . somehow hides the key from the schema like gitlab-ci does (CI/CD YAML syntax reference | GitLab). I’m not sure if it’s part of the YAML spec, but it seems to be a convention. Maybe they both use the same schema parser?

It’s not part of the YAML spec and I can’t find any documentation on it in ESPHome’s docs. I suppose, if I knew the name for what they call it, I might find information.

1 Like