Yaml / jinja2: external file containing anchors?

Hi everybody,

I am currently (once again :wink: ) trying to get a more sensible structure to my configuration yaml files. I’d like to create a file containing yaml anchors that I need in multiple different files in that folder. Can you please help me figure out how to do this?

Example folder structure; the anchors.yaml file is the one I’d like to include in both packages/radiators/bedroom.yaml and packages/radiators/bathroom.yaml.

root_folder/
  |- packages/
    |- lights/
      |- kitchen.yaml
      |- livingroom.yaml
      | (etc.)
    |- radiators/
      |- bedroom.yaml
      |- bathroom.yaml
      |- ==> anchors.yaml

This is an example for the current radiators configuration; it contains all radiators (so it is not -as in the example above- split into one file per room. This is fine, but since I have different things I want to happen in different rooms, I’d like to split it up in one config per room. But when I do, the current solution (below) that I am using will not work.

input_number:
  az_morgens:
    name: "AZ 08:30"
    initial: 16.5
    <<: &input_template
      min: 5
      max: 30
      step: 0.5
      icon: mdi:radiator
      unit_of_measurement: "Β°C"
      
  wz_morgens:
    name: "WZ 07:45"
    initial: 19.5
    <<: *input_template

az being study and wz being livingroom; as per definition above, these two entities, that are currently in the same .yaml file would be in two separate files; so wz_morgens would not be able to access *input_template; my solution would be an anchors.yam file something like this (not sure about indentation here…)

    <<: &input_template
      min: 5
      max: 30
      step: 0.5
      icon: mdi:radiator
      unit_of_measurement: "Β°C"

Then in both wz.yaml and az.yaml I’d somehow have to integrate this file

az.yaml

!include `anchors.yaml`

az_morgens:
  name: "AZ 08:30"
  initial: 16.5
  <<: *input_template

and
wz.yaml

!include `anchors.yaml`

wz_morgens:
  name: "WZ 07:45"
  initial: 19.5
  <<: *input_template

However, if I understand yaml correctly, you can only !include something when it is behind a colon, for example sensor: !include_dir_merge_named sensor/… in my case, I’d somehow have to be able to just have !include anchors.yaml…

Can this be done somehow? Or is there another solution for what I’d like to do?

I am doing this because my packages/ folder alone is getting out of hand! I have too many subdirectories for things that don’t need them on one hand, and some files I was β€œforced” to combine because of this issue that should rather be separate files.

.
β”œβ”€β”€ haus
β”‚  β”œβ”€β”€ bad_oben_test.yaml
β”‚  β”œβ”€β”€ gewitter.yaml
β”‚  β”œβ”€β”€ grandfather_clock.yaml
β”‚  β”œβ”€β”€ grocy.yaml
β”‚  β”œβ”€β”€ neato.yaml
β”‚  β”œβ”€β”€ notify_changes.yaml
β”‚  β”œβ”€β”€ notify_jonna_trinken.yaml
β”‚  β”œβ”€β”€ notify_script.yaml
β”‚  β”œβ”€β”€ podcasts_gpodder.yaml
β”‚  β”œβ”€β”€ r_arbeitszimmer.yaml
β”‚  β”œβ”€β”€ r_draussen.yaml
β”‚  β”œβ”€β”€ r_dusche.yaml
β”‚  β”œβ”€β”€ r_kueche.yaml
β”‚  β”œβ”€β”€ r_schlafzimmer.yaml
β”‚  β”œβ”€β”€ routine_guten_morgen.yaml
β”‚  β”œβ”€β”€ routine_tv.yaml
β”‚  β”œβ”€β”€ spezialtage_sensor.yaml
β”‚  β”œβ”€β”€ tasmota_schalter.yaml
β”‚  β”œβ”€β”€ timer.yaml
β”‚  β”œβ”€β”€ wecker.yaml
β”‚  └── zeitsteuerung.yaml
β”œβ”€β”€ heizungen
β”‚  β”œβ”€β”€ baeder.yaml
β”‚  β”œβ”€β”€ flur.yaml
β”‚  β”œβ”€β”€ fritzbox.yaml
β”‚  β”œβ”€β”€ heizungen_mit_template.yaml
β”‚  β”œβ”€β”€ wohnzimmer.yaml
β”‚  └── zeitsteuerung_heizungen.yaml
β”œβ”€β”€ homeassistant
β”‚  β”œβ”€β”€ anzahlen.yaml
β”‚  β”œβ”€β”€ batterien.yaml
β”‚  β”œβ”€β”€ browser_mod.yaml
β”‚  β”œβ”€β”€ conversation.yaml
β”‚  β”œβ”€β”€ customize.yaml
β”‚  β”œβ”€β”€ customize.yaml.backup
β”‚  β”œβ”€β”€ devices_im_netzwerk.yaml
β”‚  β”œβ”€β”€ discovery.yaml
β”‚  β”œβ”€β”€ downloader.yaml
β”‚  β”œβ”€β”€ feiertage.yaml
β”‚  β”œβ”€β”€ groups.yaml
β”‚  β”œβ”€β”€ hacs.yaml
β”‚  β”œβ”€β”€ influxdb.yaml
β”‚  β”œβ”€β”€ locations_usw.yaml
β”‚  β”œβ”€β”€ logbook.yaml
β”‚  β”œβ”€β”€ logger.yaml
β”‚  β”œβ”€β”€ mqtt.yaml
β”‚  β”œβ”€β”€ neato.yaml
β”‚  β”œβ”€β”€ notify.yaml
β”‚  β”œβ”€β”€ panel_iframe.yaml
β”‚  β”œβ”€β”€ recorder.yaml
β”‚  β”œβ”€β”€ shellies_discovery.yaml
β”‚  β”œβ”€β”€ shopping_list.yaml
β”‚  β”œβ”€β”€ synology.yaml
β”‚  β”œβ”€β”€ tankerkoenig.yaml
β”‚  β”œβ”€β”€ tts.yaml
β”‚  β”œβ”€β”€ unifi.yaml
β”‚  β”œβ”€β”€ wetter.yaml
β”‚  └── zones.yaml
β”œβ”€β”€ licht
β”‚  β”œβ”€β”€ bad_oben.yaml
β”‚  β”œβ”€β”€ festlegen.yaml
β”‚  β”œβ”€β”€ flur.yaml
β”‚  β”œβ”€β”€ flux.yaml
β”‚  β”œβ”€β”€ haustuer.yaml
β”‚  β”œβ”€β”€ kueche.yaml
β”‚  β”œβ”€β”€ licht.yaml
β”‚  β”œβ”€β”€ scenes.yaml
β”‚  β”œβ”€β”€ sonnensensor.yaml
β”‚  β”œβ”€β”€ wohnzimmer.yaml
β”‚  β”œβ”€β”€ xmas.yaml
β”‚  β”œβ”€β”€ zwischenzimmer.yaml
β”‚  └── zwischenzimmer_led.yaml
β”œβ”€β”€ media
β”‚  β”œβ”€β”€ alexa.yaml
β”‚  β”œβ”€β”€ chromecast.yaml
β”‚  β”œβ”€β”€ firetv.yaml
β”‚  β”œβ”€β”€ firetv_tor.yaml
β”‚  β”œβ”€β”€ kodi.yaml
β”‚  β”œβ”€β”€ licht_media.yaml
β”‚  β”œβ”€β”€ media_extractor.yaml
β”‚  β”œβ”€β”€ mpd.yaml
β”‚  β”œβ”€β”€ nvidia_shieldtv.yaml
β”‚  β”œβ”€β”€ plex.yaml
β”‚  β”œβ”€β”€ sonos.yaml
β”‚  └── tv.yaml
β”œβ”€β”€ misc
β”‚  β”œβ”€β”€ caldav.yaml
β”‚  β”œβ”€β”€ calendar.yaml
β”‚  β”œβ”€β”€ day_of_week.yaml
β”‚  β”œβ”€β”€ hundefutter.yaml
β”‚  β”œβ”€β”€ jahreszeit.yaml
β”‚  β”œβ”€β”€ mond.yaml
β”‚  β”œβ”€β”€ sabnzbd.yaml
β”‚  β”œβ”€β”€ sonarr.yaml
β”‚  β”œβ”€β”€ soziale_netzwerke.yaml
β”‚  β”œβ”€β”€ time.yaml
β”‚  └── unifi.yaml
β”œβ”€β”€ remotes
β”‚  β”œβ”€β”€ broadlink.yaml
β”‚  β”œβ”€β”€ button_schlafzimmer.yaml
β”‚  β”œβ”€β”€ espurna.yaml
β”‚  β”œβ”€β”€ zigbee2mqtt.yaml
β”‚  └── zigbee2mqtt_networkmap.yaml
β”œβ”€β”€ security
β”‚  β”œβ”€β”€ alert.test_funktioniert
β”‚  β”œβ”€β”€ cams_alle.yaml
β”‚  β”œβ”€β”€ cams_dafang.yaml
β”‚  β”œβ”€β”€ rauchmelder.yaml
β”‚  β”œβ”€β”€ security_tor.yaml
β”‚  └── security_unterstand.yaml
β”œβ”€β”€ tasmota
β”‚  β”œβ”€β”€ energyreset_total.yaml
β”‚  └── tasmota_startup.yaml
β”œβ”€β”€ telegram
β”‚  β”œβ”€β”€ abfragen.yaml
β”‚  β”œβ”€β”€ callback.yaml
β”‚  β”œβ”€β”€ cameras_telegram.yaml
β”‚  β”œβ”€β”€ jonna_trinken.yaml
β”‚  β”œβ”€β”€ klingel.yaml
β”‚  β”œβ”€β”€ telegram_bot.yaml
β”‚  β”œβ”€β”€ tor.yaml
β”‚  └── wuff.yaml
β”œβ”€β”€ testing
β”‚  β”œβ”€β”€ test_wled.yaml
β”‚  └── zeittest.yaml
└── 00.yaml

Instead of all this, I could instead have one package per room (plus one for things that do not have a location, or work in multiple locations); but that’d require me to import the template somehow, as az.yaml would be in packages/az (containing all things related to the study) and wz.yaml would be in packages/wz with everything related to the livingroom…

THank you for your ideas :slight_smile:

Long question, short reply.

Can’t be done. Anchors only work within the file they’re declared.