Yet another YAML anchor trouble

I’m trying to setup some similar REST sensors as follows:

- resource: https://serverless-offer.maxmilhas.com.br/offer/market-analysis
  sensor:
    - &sensor
      icon: mdi:plane
      device_class: monetary
      json_attributes:
        - price3DaysAgo #and other ones... this is actually line 14
      name: Milheiro LATAM # this is line 15
      value_template: "(value_json|selectattr('airline', 'eq', 'Latam')|first).price24HoursAgo"
      json_attributes_path: $[?(@.airline=='Latam' && @.international == 0)]
    - <<: *sensor
      name: Milheiro Gol # this is actually line 19
      value_template: "(value_json|selectattr('airline', 'eq', 'Gol')|first).price24HoursAgo"
      json_attributes_path: $[?(@.airline=='Gol' && @.international == 0)]
    - <<: *sensor # this is line 22
      name: Milheiro Azul # this is line 23
      # [...]

This works wonders if you paste it to https://www.yamllint.com.

However:

Logger: homeassistant.util.yaml.loader
Source: util/yaml/loader.py:437

YAML file /config/_inc/rest.yaml contains duplicate key "value_template". Check lines 15 and 19
YAML file /config/_inc/rest.yaml contains duplicate key "json_attributes_path". Check lines 16 and 20
YAML file /config/_inc/rest.yaml contains duplicate key "name". Check lines 14 and 22
YAML file /config/_inc/rest.yaml contains duplicate key "value_template". Check lines 15 and 23
YAML file /config/_inc/rest.yaml contains duplicate key "json_attributes_path". Check lines 16 and 24

If I remove the anchor expansion and copy-pasta the extra keys, that error is gone from logs.

To my understanding, HA is able to correctly interpret YAML anchors… or only in some scenarios? Any guess on what’s going on with this setup?

Thanks in advance for your patience :joy:

Edit:
Completely ignore below as I totally misunderstood what was going on (sorry).


Original Post:
you have some funky things going on in your yaml.
You only need to have sensor: once and not sure about - <<: * or - &

Try this:

- resource: https://serverless-offer.maxmilhas.com.br/offer/market-analysis
  sensor:
    - name: Milheiro LATAM # this is line 15
      icon: mdi:plane
      device_class: monetary
      json_attributes:
        - price3DaysAgo #and other ones... this is actually line 14
      value_template: "(value_json|selectattr('airline', 'eq', 'Latam')|first).price24HoursAgo"
      json_attributes_path: $[?(@.airline=='Latam' && @.international == 0)]
    - name: Milheiro Gol # this is actually line 19
      value_template: "(value_json|selectattr('airline', 'eq', 'Gol')|first).price24HoursAgo"
      json_attributes_path: $[?(@.airline=='Gol' && @.international == 0)]
    - name: Milheiro Azul # this is line 23
      # [...]

Note: I haven’t checked anything else apart from the sensor part.

You need to declare the anchor dictionary you’re adding also using the <<: if you want to declare it in-line. Anchors need a field to declare the anchor on. <<: says “add these keys to this object”, it also doubles as a field so that you can declare an anchor.

Secondly, your value template weren’t correct. They were missing jinja indicators {{ }}

Also, inline comments mess up yaml, don’t do that. Put the comments before or after.

- resource: https://serverless-offer.maxmilhas.com.br/offer/market-analysis
  sensor:
    - <<: &sensor
        icon: mdi:plane
        device_class: monetary
        json_attributes:
          - price3DaysAgo
      name: Milheiro LATAM 
      value_template: >
        {{ (value_json|selectattr('airline', 'eq', 'Latam')|first).price24HoursAgo }}
      json_attributes_path: $[?(@.airline=='Latam' && @.international == 0)]

    - <<: *sensor
      name: Milheiro Gol 
      value_template: >
        {{ (value_json|selectattr('airline', 'eq', 'Gol')|first).price24HoursAgo }}
      json_attributes_path: $[?(@.airline=='Gol' && @.international == 0)]

    - <<: *sensor 
      name: Milheiro Azul 
      # [...]
1 Like

Those are yaml anchors, what his post is specifically about.

1 Like

Agh, thanks. I guess I have some anchor learning to do!

1 Like

WOW. Ok! It works! It took me a bit to understand, but it’s my turn to explain now haha

Usually, YAML allows you to override anchor keys. But for some reason, the interpreter used by HA doesn’t do it (hence, complaints about dup keys). Your example works because your extra <<: is splitting the anchor keys from the keys of the actual entry. That’s very subtle to understand because of the indentation, I only realized it once I gave up and copy-pasted your example into my full file.

Again, notice that my original code works on YamlLint (as yours do), and I’ve used this kind of syntax before on Docker files (no <<: on declaration and overridable keys); that’s where I learned it from.

I wonder if it’s worth publishing this lacking behavior as a bug report?


God, right, the missing {{ }} on the template gave me many minutes of headache :joy: I figured it out by myself once I gave up on the anchors last night… Felt like forgetting the semi-colon on my early dev days.

And I never had issues so far with inline comments, I love those. But I’ll definitely keep that in mind, thanks for the note!


Thanks as usual, Mr. Forum Savior :clap: