Lovelace Search

Home Assistant Script

In my system I often download dashboard components from HACS and then can’t remember if I have used them.
So I built a simple dashboard page to search the lovelace files in .storage for any plain text string and output the results as a markdown card.

Lovelace Search Setup Instructions

Complete setup guide for searching Lovelace configuration files in Home Assistant using MQTT.

Prerequisites

  • Home Assistant with MQTT broker configured (Mosquitto add-on recommended)
  • File Editor add-on (for editing configuration files)

Step 1: Add Input Text Helper

Add this to your configuration.yaml:

input_text:
  lovelace_search_term:
    name: Search Term
    initial: "type:"
    max: 255

Step 2: Add Shell Command

Add this to your configuration.yaml:

shell_command:
  search_lovelace: /bin/bash -c "grep -HnF '{{ search_string }}' /config/.storage/lovelace* 2>&1 || echo 'No matches found'"

Note: The -F flag treats the search as a literal string (not regex), so searches like button-card will work correctly.

Step 3: Configure MQTT Sensor

Add this to your configuration.yaml:

mqtt:
  sensor:
    - name: "Lovelace Search Results"
      state_topic: "temp/lovelace_search/state"
      unique_id: lovelace_search_results
      json_attributes_topic: "temp/lovelace_search/results"  

Step 4: Create Search Script

Add this to your configuration.yaml (or create in UI under Settings > Automations & Scenes > Scripts):

sequence:
  - data:
      search_string: "{{search_text}}"
    action: shell_command.search_lovelace
    response_variable: result_text
  - variables:
      lines: |
        {{ result_text.stdout.split('\n') }}
      parsed_data: >
        {% set ns = namespace(files={}) %} {% for line in lines %}
          {% if line and line.strip() != '' and ':' in line %}
            {% set parts = line.split(':', 2) %}
            {% if parts | length >= 3 %}
              {% set file = parts[0].split('/')[-1] | string %}
              {% set line_num = parts[1] | string %}
              {% set content = parts[2].strip() | string %}
              {% if file in ns.files %}
                {% set ns.files = dict(ns.files, **{file: ns.files[file] + [{'line': line_num, 'content': content}]}) %}
              {% else %}
                {% set ns.files = dict(ns.files, **{file: [{'line': line_num, 'content': content}]}) %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endfor %} {% set total = namespace(count=0) %} {% for file, matches
        in ns.files.items() %}
          {% set total.count = total.count + (matches | length) %}
        {% endfor %} {
          "files": {{ ns.files | tojson }},
          "search_term": "{{ search_text }}",
          "total_matches": {{ total.count }},
          "file_count": {{ ns.files | length }}
        }
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: false
      topic: temp/lovelace_search/results
      payload: |
        {{parsed_data}}
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: false
      topic: temp/lovelace_search/state
      payload: |
        {{search_text}}
alias: Search Lovelace
description: ""
fields:
  search_text:
    selector:
      text: null
    name: Search Text
    description: Search Lovelace GUI Dashboards for this value
    required: true

Step 5: Restart Home Assistant

After adding all the configuration:

  1. Go to Developer Tools > YAML
  2. Click Check Configuration
  3. If valid, click Restart

Step 6: Create Dashboard Card

Add this card to any dashboard:

type: vertical-stack
cards:
  - type: entities
    title: Lovelace File Search
    entities:
      - entity: input_text.lovelace_search_term
        name: Search Term
  
  - type: button
    name: Search
    icon: mdi:magnify
    tap_action:
      action: call-service
      service: script.search_lovelace_files
  
  - type: markdown
    title: Search Results
    content: |
      {% set data = state_attr('sensor.lovelace_search_results', 'files') %}
      {% set total = state_attr('sensor.lovelace_search_results', 'total_matches') %}
      {% set term = states('sensor.lovelace_search_results') %}
      
      # ? Lovelace Search Results
      
      {% if data and total > 0 %}
      **Search term:** `{{ term }}`  
      **Found {{ total }} match(es) in {{ data | length }} file(s)**
      
      {% for file, matches in data.items() %}
      ## ? {{ file }}
      *{{ matches | length }} match(es)*
      
      {% for match in matches %}
      * **{{ match.line }}:** {{ match.content[:100] }}{{ '...' if match.content | length > 100 }}
      {% endfor %}
      
      ---
      {% endfor %}
      {% else %}
      *No matches found or search not yet run*
      {% endif %}

How to Use

  1. Enter a search term in the input field (e.g., button-card, type: entities, custom:mushroom)
  2. Click the Search button
  3. View formatted results showing:
    • Which files contain matches
    • Line numbers where matches occur
    • Content of matching lines

Example Searches

  • button-card – Find all button card instances
  • entities – Find all entities cards
  • custom: – Find all custom cards
  • entity_id – Find entity references
  • sensor. – Find all sensor entities

Troubleshooting

No results showing:

  • Check MQTT broker is running
  • Verify sensor exists: Developer Tools > States > sensor.lovelace_search_results
  • Check script ran successfully in Logbook

Search not finding expected results:

  • Remember searches are case-sensitive
  • Try broader search terms (e.g., button instead of button-card)

MQTT sensor unavailable:

  • Verify MQTT integration is configured
  • Check Mosquitto broker add-on is running
  • Restart Home Assistant

File Structure

Your Lovelace configuration files are stored in:

/config/.storage/lovelace*

These files include:

  • lovelace – Main dashboard
  • lovelace.dashboard_* – Named dashboards
  • lovelace_dashboards – Dashboard registry
  • lovelace_resources – Custom resources

Technical Details

How It Works

  1. Shell Command: Uses grep to search through all Lovelace storage files
  2. Script Processing: Parses grep output and converts to structured JSON
  3. MQTT Publishing: Stores results in MQTT for persistence
  4. Markdown Display: Formats results into a readable table

JSON Structure

The search results are stored in this format:

{
  "files": {
    "lovelace.dashboard_name": [
      {
        "line": "42",
        "content": "type: custom:button-card"
      }
    ]
  },
  "search_term": "button-card",
  "total_matches": 15,
  "file_count": 3
}

Customization Options

Change content length display: Modify [:100] in the markdown card to show more or fewer characters:

* **{{ match.line }}:** {{ match.content[:200] }}

Add case-insensitive search: Modify shell command to add -i flag:

grep -HnFi '{{ search_string }}'

Search specific dashboards only: Modify the file pattern in shell command:

/config/.storage/lovelace.dashboard_*

Credits

This solution uses Home Assistant’s native capabilities combined with standard Linux tools to provide a powerful search interface for Lovelace configuration files.