Adding lots of cards at once via YAML

I’m working on some tools to automatically add Modbus devices to HA without having to go through a huge amount of manual editing of various YAML files. Specifically, they auto-generate the YAML to go from Modbus device+register → MQTT gateway → HA. The final hurdle is adding the cards for each Modbus device, I can generate the YAML for them, for example for power meters it’s:

type: entities
title: Grid
entities:
  - entity: sensor.grid_voltage
  - entity: sensor.grid_frequency
  - entity: sensor.grid_current
  - entity: sensor.grid_active_power
  - entity: sensor.grid_import_energy
  - entity: sensor.grid_export_energy
  - entity: sensor.grid_total_demand
  - entity: sensor.grid_positive_demand
  - entity: sensor.grid_reverse_demand
  - entity: sensor.grid_current_demand
  - entity: sensor.grid_max_current_demand
  - entity: sensor.grid_total_energy

type: entities
title: Backup
entities:
  [As above]

type: entities
title: Non-backup
entities:
  [As above]

type: entities
title: Generator
entities:
  [As above]

[Several more]

but it’s not clear what the next step is. Note that each Modbus device can have dozens of registers and there can be dozens of Modbus devices, which means you really need to use automated generation unless you want to go mad cutting and pasting endless entries into the UI. And then if a change is necessary across all of those dozens of devices…

There are JSON (not YAML) configs in /config/.storage/lovelace.*, but presumably there’s some way to get the YAML in in a manner that doesn’t involve pasting a card at a time into the UI.

Alternatively, is there a way to have the system automatically recognise and add the cards as it does for many extenions? For example if I add the Foo extension the Overview dashboard automatically adds a pile of cards for all of the Foo devices, presumably there’s some mechanism for doing that but I haven’t found it yet.

Mechanisms only exist for UI integrations.

For yaml, you have to use yaml. If your yaml integration provides a unique_id to the entity_id, those will also show up.

Your best option for autogenerated yaml is to use templates in the template editor to generate your yaml.

e.g.

{% for device in [ list out your esphome devices as strings ] %}
type: entities
title: {{ device }}
entities:
  {%- for entity in [ list out your entitys for this device as strings %}
  - {{ entity }}
  {%- endfor %}
{% endfor %}

then copy/paste each card.

If I’ve understood your comment correctly, the problem isn’t actually generating the YAML, the script I’ve got already does that, it’s the “copy/paste each card” step. Since there can be dozens of cards, and a change somewhere can potentially require re-copy/pasting each one, I’m wondering if there’s some way to bulk update a whole pile of cards in one go from a single lot of YAML that describes each card in turn.

If you have a script that does that, have it create a file and include the file. That can only be done with a dashboard in yaml mode.

That’s where I’m stuck at the moment, you can edit individual cards as YAML but to edit an entire dashboard seems to require JSON, or at least the dashboards /config/.storage/lovelace.* are stored as JSON and not YAML. So at the individual-card level it’s YAML, at the everything-in-one-place level it’s JSON. What I was trying to find out is if there’s a way to do the everything-in-one-place in YAML, or if it has to be done in JSON.

1 Like

Yes, thats why I said

This means the whole dashboard has to be in yaml mode. Not just 1 card. There’s 2 modes for dashboards, storage and yaml. Storage = UI, yaml = all yaml, nothing else. Your dashboard needs to be in yaml mode to include external files.