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
# [...]
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.
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 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!