# Jinja Magic Scripts

{% hint style="info" %}
The following is a jinja2 script that you need to run in the Developer Tools -> Templates (template editor), then copy and paste the output into your `ui-lovelace.yaml` file. Copying the following jinja code/script directly into the `ui-lovelace.yaml` file will give you severe headaches and possibly long-term Migraines.
{% endhint %}

## Code to auto-generate Lovelace Script

Use the following script to automagically generate the lovelace cards based on what you already have. Go to your Home Assistant, click on the hamburger icon on the top left, under `Developer Tools`, click on `Templates`, and then replace what's out there with the following code. You should see the output right next to it. Copy that output, and paste it in the `ui-lovelace.yaml` file in your config folder and go to https\://<>:8123/lovelace url and hit refresh.

```yaml
{# @Author: Mahasri Kalavala (a.k.a @skalavala #}

{%- macro plural(name) -%}
  {%- if name[(name|length|int -1):] == "y" -%}
  {{- name[:-1] -}}ies
{%- else -%}
  {{- name -}}s
{%- endif -%}
{%- endmacro -%}

title: My Lovely Home Automation
views:
  - title: Home
    cards:
{%- set domains = states | map(attribute='domain') |list | unique | list %}
{%- for item in states['camera'] %}
      - id: {{ item.entity_id.replace('.', '_') }}
        type: picture-entity      
        title: {{ item.name }}
        entity: {{ item.entity_id }}
        camera_image: {{ item.entity_id }}
        show_info: true
        tap_action: dialog
{% endfor %}
{%- for item in states['media_player'] %}
      - type: media-control
        entity: {{ item.entity_id }}
{% endfor %}
{%- for item in domains if item != "camera" and item != "media_player" %}
      - type: {% if item == "device_tracker" -%}
        entity-filter
        {%- elif item == "camera" -%}
        picture-entity
        {%- else -%}
          entities
        {%- endif %}
{%- if states[item]|list |length|default(0)|int > 1 %}
        title: {{ plural(item.replace('_', ' ') | title)  }}
{%- else %}
        title: {{ item.replace('_', ' ') | title }}
{%- endif %}
        show_header_toggle: true
        entities:
{%- for e in states[item] %}
          - {{ e.entity_id }}
{%- endfor %}
{%- if item == "device_tracker" %}
        state_filter:
          - 'home'
        card:
          type: glance
          title: My Device Trackers
{% endif %}
{% endfor -%}
```

If you need additional support for other custom cards, please feel free to do a PR or contact @skalavala.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sharethelove.io/tools/jinja-magic-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
