Merging Lists in Lovelace YAML mode

I’ve got YAML mode enabled and working. Now I’m trying to dynamically construct a list of “chips” in a Mushroom card, alongside some hard-coded items. Basically this is what I’m trying to do:

cards:
  - type: custom:mushroom-chips-card
    chips:
      - !include ./resources/some_chips.yaml
      - type: alarm-control-panel
        entity: alarm_control_panel.alarmo
      - !include ./resources/more_chips.yaml
    alignment: center

some_chips.yaml/more_chips.yaml look more or less like this:

- type: conditional
  conditions:
    - entity: vacuum.kirby
      state_not: docked
  chip:
    type: template
    entity: vacuum.kirby
    icon: mdi:robot-vacuum
    tap_action:
      action: more-info
    icon_color: |-
      {% if not is_state("vacuum.kirby", "cleaning") %}
        red
      {% endif %}
    content: '{{ states("vacuum.kirby") }}'
...

Where the chips.yaml cards are just lists of chips. The !included items don’t show up. I can do this:

cards:
  - type: custom:mushroom-chips-card
    chips: !include ./resources/some_chips.yaml
    alignment: center
  - type: custom:mushroom-chips-card
    chips:
      - type: alarm-control-panel
        entity: alarm_control_panel.alarmo
    alignment: center
  - type: custom:mushroom-chips-card
    chips: !include ./resources/more_chips.yaml
    alignment: center

But I can’t seem to get one card with all the chips in it. I’ve flailed around a bit trying to use anchors, but… Nothing seems to work. I think I must be missing something here, some way to get all the lists to merge/append.

Anybody have any hints for me?

Ask here…you may get some headway.

Thanks for the reply, after a good night’s sleep and some additional Googling, it appears the YAML spec doesn’t support merging sequences, which is the problem I am running into.

Basically, no matter which syntax we use, including merging anchors:

cards:
  - <<: *some_anchor
  - !include some_file.yaml
  - More
  - Stuff

Will be interpreted as:

cards:
  - ["anchor", "contents"]
  - ["yaml", "file", "contents"]
  - More
  - stuff

Or exploded:

cards:
  - - anchor
    - contents
  - - yaml
    - file
    - contents
  - More
  - stuff

In other words, we get a nested list. YAML can’t (and/or doesn’t) flatten lists, as there’s currently not an unambiguous way to append to sequences. Some parsers have played with it, but it’s definitely not standardized.

I suppose I’ll have to come up with another way to do this. Probably a bunch of anchors and some declutter cards.

2 Likes

Same problem here, thank god almost pulling all my hair out. But would like to ask have you found a solution?

Short answer is no, it’s a hard limitation, combining YAML lists won’t work without changes to the YAML parsing in Home Assistant that (might) reduce backwards compatibility. So don’t hold your breath, I would not be surprised if this never works.

That said: My long answer is, sorta/kinda. I am able to do everything I wanted without this functionality, but I had to get a little creative and in short I’m using parameterized Decluttering Cards and anchors. The anchors are then included in the decluttering card definitions.

The result looks something like this:

###
# Anchors for Summary Chips
kirby_chip: &kirby_chip
  type: conditional
  conditions:
    - entity: vacuum.kirby
      state_not: docked
  chip:
    type: template
    entity: vacuum.kirby
    icon: mdi:robot-vacuum
    tap_action:
      action: more-info
    icon_color: |-
      {% if not is_state(entity, "cleaning") %}
        red
      {% endif %}
    content: "{{ states(entity) }}"
marvin_chip: &marvin_chip
  type: conditional
  conditions:
    - entity: vacuum.marvin
      state_not: docked
  chip:
    type: template
    entity: vacuum.marvin
    icon: mdi:robot-vacuum
    tap_action:
      action: more-info
    icon_color: |-
      {% if not is_state(entity, "cleaning") %}
        red
      {% endif %}
    content: "{{ states(entity) }}"
octoprint_chip: &octoprint_chip
  type: conditional
  conditions:
    - entity: binary_sensor.octoprint_printing
      state_not: unavailable
  chip:
    type: entity
    entity: sensor.octoprint_estimated_finish_time
    content_info: state
    tap_action:
      action: fire-dom-event
      browser_mod:
        service: browser_mod.popup
        data:
          dismissable: True
          title: Ender3Pro
          timeout: 300000
          size: wide
          content: !include /config/packages/lovelace/resources/cards/ender3pro.yaml
lr4_chip: &lr4_chip
  type: conditional
  conditions:
    - condition: numeric_state
      entity: sensor.litter_robot_4_waste_drawer
      above: 85
  chip:
    type: entity
    entity: vacuum.litter_robot_4_litter_box
    content_info: state
    tap_action:
      action: fire-dom-event
      browser_mod:
        service: browser_mod.popup
        data:
          dismissable: True
          title: Litter Robot 4
          timeout: 300000
          size: normal
          content: !include /config/packages/lovelace/resources/cards/lr4.yaml
front_hose_chip: &front_hose_chip
  type: conditional
  conditions:
    - entity: switch.front_yard_sprinkler
      state: "on"
  chip:
    type: entity
    entity: switch.front_yard_sprinkler
    content_info: name
    tap_action:
      action: toggle
    icon_color: blue
holiday_lights_chip: &holiday_lights_chip
  type: conditional
  conditions:
    - entity: light.holiday_lights
      state: "on"
  chip:
    type: entity
    entity: light.holiday_lights
    content_info: name
    tap_action:
      action: toggle
# etc...

###
# Decluttering Cards:
mobile_summary_chips:
  card:
    type: custom:mushroom-chips-card
    chips:
      - *broadcast_chip
      - *kirby_chip
      - *marvin_chip
      - *dog_fur_eater_chip
      - *octoprint_chip
      - *front_hose_chip
      - *holiday_lights_chip
      - *back_hose_chip
      - *printer_chip
      - *obihai_chip
      - *chicken_water_chip
      - *water_fountain_chip
      - *chickens_incubating_chip
      - *laundry_chip
      - *holiday_decor_chip
      - *grill_chip
      - *package_chip
      - type: template
        entity: lock.front_door
        icon: >-
          {{ iif(not is_state(entity, "locked"), "mdi:lock-open",
          "mdi:lock") }}
        tap_action:
          action: toggle
        icon_color: |-
          {% if not is_state(entity, "locked") %}
            red
          {% endif %}
        content: Front Door
      - type: template
        entity: lock.back_patio_door
        icon: >-
          {{ iif(not is_state(entity, "locked"),
          "mdi:lock-open", "mdi:lock") }}
        tap_action:
          action: toggle
        icon_color: |-
          {% if not is_state(entity, "locked") %}
            red
          {% endif %}
        content: Patio Door
      - *sied_chip
      - *grc_chip
      - *btc_chip
    alignment: center
dashboard_summary_chips:
  card:
    type: custom:mushroom-chips-card
    chips:
      - *broadcast_chip
      - *kirby_chip
      - *marvin_chip
      - *dog_fur_eater_chip
      - *octoprint_chip
      - *front_hose_chip
      - *holiday_lights_chip
      - *back_hose_chip
      - *printer_chip
      - *obihai_chip
      - *chicken_water_chip
      - *water_fountain_chip
      - *chickens_incubating_chip
      - *laundry_chip
      - *holiday_decor_chip
      - *grill_chip
      - *package_chip
      - type: alarm-control-panel
        entity: alarm_control_panel.alarmo
      - *sied_chip
      - *grc_chip
      - *btc_chip

    alignment: center

And then the usage might look like:

      - type: custom:decluttering-card
        template: mobile_summary_chips

This particular example isn’t parameterized and therefore I don’t think it has to be a Decluttering Card. That said, there are some other oddities related to parsing order that I remember struggling with and I think Decluttering Card helped solve some of those issues. Anchors are parsed early so you can’t have any variables in them but I honestly can’t remember the specifics.

Thanks for sharing your solution!