How to not repeat YAML for template covers?

I’ve made a bunch of template covers to be able to add some smart functionality to my outdoor vertical awnings. I have an AppDaemon app that listens for events for these covers (e.g. open, close, stop) so that I can override them in different ways, e.g. if it’s been raining and they’ve gotten wet, keep them in their current state so they can dry, keep them rolled down while the TV is on to avoid glare etc.

I have a couple of them and don’t seem to be able to use YAML anchors for them due to the entity in the event being sent. I’ve tried adding an extra field in the template like this:

living_room_bottom_screen: &smart_screen
  entity: cover.living_room_bottom_screen
  friendly_name: "Living Room Bottom Screen"
  unique_id: cover-living_room_bottom_screen
  position_template: "{{ state_attr('cover.hidden_living_room_bottom_screen', 'current_position') | int }}"
  device_class: shade
  open_cover:
    event: SMART_SCREEN_OPEN
    event_data:
      entity: "{{ entity }}"
  close_cover:
    event: SMART_SCREEN_CLOSE
    event_data:
      entity: "{{ entity }}"
  stop_cover:
    event: SMART_SCREEN_STOP
    event_data:
      entity: "{{ entity }}"
  set_cover_position:
    event: SMART_SCREEN_SET_POSITION
    event_data:
      entity: "{{ entity }}"
      position: "{{ position }}"

living_room_west_screen:
  <<: *smart_screen
  entity: cover.living_room_west_screen
  friendly_name: "Living Room West Screen"
  unique_id: cover-living_room_west_screen

but get the following error in HA:

So I’ve basically had to copy-paste everything for each cover and just hope that I haven’t missed replacing one of the fields to the correct entity name:

living_room_bottom_screen:
  friendly_name: "Living Room Bottom Screen"
  unique_id: cover-living_room_bottom_screen
  position_template: "{{ state_attr('cover.hidden_living_room_bottom_screen', 'current_position') | int }}"
  device_class: shade
  open_cover:
    event: SMART_SCREEN_OPEN
    event_data:
      entity: cover.living_room_bottom_screen
  close_cover:
    event: SMART_SCREEN_CLOSE
    event_data:
      entity: cover.living_room_bottom_screen
  stop_cover:
    event: SMART_SCREEN_STOP
    event_data:
      entity: cover.living_room_bottom_screen
  set_cover_position:
    event: SMART_SCREEN_SET_POSITION
    event_data:
      entity: cover.living_room_bottom_screen
      position: "{{ position }}"

living_room_top_south_screen:
  friendly_name: "Living Room Top South Screen"
  unique_id: cover-living_room_top_south_screen
  position_template: "{{ 100 - state_attr('cover.hidden_living_room_top_south_screen', 'current_position') | int }}"
  device_class: shade
  open_cover:
    event: SMART_SCREEN_OPEN
    event_data:
      entity: cover.hidden_living_room_top_south_screen
  close_cover:
    event: SMART_SCREEN_CLOSE
    event_data:
      entity: cover.hidden_living_room_top_south_screen
  stop_cover:
    event: SMART_SCREEN_STOP
    event_data:
      entity: cover.hidden_living_room_top_south_screen
  set_cover_position:
    event: SMART_SCREEN_SET_POSITION
    event_data:
      entity: cover.hidden_living_room_top_south_screen
      position: "{{ 100 - position }}"

living_room_west_screen:
  friendly_name: "Living Room West Screen"
  unique_id: cover-living_room_west_screen
  position_template: "{{ 100 - state_attr('cover.hidden_living_room_west_screen', 'current_position') | int }}"
  device_class: shade
  open_cover:
    event: SMART_SCREEN_OPEN
    event_data:
      entity: cover.living_room_west_screen
  close_cover:
    event: SMART_SCREEN_CLOSE
    event_data:
      entity: cover.living_room_west_screen
  stop_cover:
    event: SMART_SCREEN_STOP
    event_data:
      entity: cover.living_room_west_screen
  set_cover_position:
    event: SMART_SCREEN_SET_POSITION
    event_data:
      entity: cover.living_room_west_screen
      position: "{{ 100 - position }}"

office_screen:
  friendly_name: "Office Screen"
  unique_id: cover-office_screen
  position_template: "{{ 100 - state_attr('cover.hidden_office_screen', 'current_position') | int }}"
  device_class: shade
  open_cover:
    event: SMART_SCREEN_OPEN
    event_data:
      entity: cover.office_screen
  close_cover:
    event: SMART_SCREEN_CLOSE
    event_data:
      entity: cover.office_screen
  stop_cover:
    event: SMART_SCREEN_STOP
    event_data:
      entity: cover.office_screen
  set_cover_position:
    event: SMART_SCREEN_SET_POSITION
    event_data:
      entity: cover.office_screen
      position: "{{ 100 - position }}"

(For those wondering, the position inversion on some covers is intended/correct)

Is there any way to do this cleaner?

You could use this: link.

Hi Adabelle_Leiram,

Yaml anchors do work.

Solved: Reuse code with YAML Anchors and more.

I have an example in one of my blueprints if it helps you.

HA_Blueprints/Automations/Zigbee2MQTT-Aqara-Magic-Cube-T1-Pro-CTP-R01-Xiaomi-Lumi.yaml at d1c15d75a7b5fa3042d60f7eee2d7633ea8a27a3 · SirGoodenough/HA_Blueprints · GitHub.

You can also look into creating a script ot a script blueprint and creating fields, then instead of repeating the code, action a script sending it data and you can isolate the code that way. Sometimes it makes sense to create custom_template macrd and call repeating templates that way.

Lots of possibilities.

Another option is to go all in with YAML only editing and lovelace_gen.
If YAML editing is not scaring you then take a look at lovelace_gen.