Passing values to an !include yaml file in packages

This is not related to passing values into a script or anything during runtime, it’s about “substitutions” during setup.

I use packages extensively and I’m trying to save on repeated code. What I’d like to do is to be able to declare some variables at the top of a package yaml and then include a bunch of templates like this:

  room_name: &room_name Living Room
  room_id: &room_id living_room
  climate_entity: &climate_entity climate.living_room
  heater_output: &heater_output switch.living_room_heater
  temperature_input: &temperature_input sensor.living_room_average_temperature

      common: &common
        package: Climate
        room: *room_name

climate: !include ../templates/package_climate/generic_thermostat.yaml
binary_sensor: !include ../templates/package_climate/binary_sensor.yaml
sensor: !include ../templates/package_climate/sensor.yaml

So this would be my climate_living_room.yaml and if I wanted to create the same entities for kitchen, I just duplicate this file and change the anchors. I figure if you’re technically advanced enough to help me here, you know what I’m trying to do so I won’t go into more details.

Following @thomasloven 's guide on GitHub, I have a basic understanding of node anchors and I thought I might be able to get away with the above.

I understand that yaml is not designed to do any of this, however I figure the !secrets, !include, etc are already a work-around and was wondering if anyone knows a way of accomplishing what I’m trying to achieve above.

The error I get with the above code is:

Error loading /config/configuration.yaml: found undefined alias 'room_name'
in "/config/packages/../templates/package_climate/generic_thermostat.yaml", line 2, column 11

Any help would be hugely appreciated,

Unfortunately, the answer is: No. This cannot be done.

Yaml anchors are ONLY valid in the same file, and they are processed before any ! statements.

1 Like

Thanks Thomas, good to have a quick & definitive answer. Nevermind, my copy and paste keys still work fine. Thanks also for your other contributions, my lovelace couldn’t imagine life without your work!