Jinja Magic Scripts
Jinja2 script to help you create/migrate the existing config/setup to lovelace automagically.
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.

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.
1
{# @Author: Mahasri Kalavala (a.k.a @skalavala #}
2
3
{%- macro plural(name) -%}
4
{%- if name[(name|length|int -1):] == "y" -%}
5
{{- name[:-1] -}}ies
6
{%- else -%}
7
{{- name -}}s
8
{%- endif -%}
9
{%- endmacro -%}
10
11
title: My Lovely Home Automation
12
views:
13
- title: Home
14
cards:
15
{%- set domains = states | map(attribute='domain') |list | unique | list %}
16
{%- for item in states['camera'] %}
17
- id: {{ item.entity_id.replace('.', '_') }}
18
type: picture-entity
19
title: {{ item.name }}
20
entity: {{ item.entity_id }}
21
camera_image: {{ item.entity_id }}
22
show_info: true
23
tap_action: dialog
24
{% endfor %}
25
{%- for item in states['media_player'] %}
26
- type: media-control
27
entity: {{ item.entity_id }}
28
{% endfor %}
29
{%- for item in domains if item != "camera" and item != "media_player" %}
30
- type: {% if item == "device_tracker" -%}
31
entity-filter
32
{%- elif item == "camera" -%}
33
picture-entity
34
{%- else -%}
35
entities
36
{%- endif %}
37
{%- if states[item]|list |length|default(0)|int > 1 %}
38
title: {{ plural(item.replace('_', ' ') | title) }}
39
{%- else %}
40
title: {{ item.replace('_', ' ') | title }}
41
{%- endif %}
42
show_header_toggle: true
43
entities:
44
{%- for e in states[item] %}
45
- {{ e.entity_id }}
46
{%- endfor %}
47
{%- if item == "device_tracker" %}
48
state_filter:
49
- 'home'
50
card:
51
type: glance
52
title: My Device Trackers
53
{% endif %}
54
{% endfor -%}
Copied!
If you need additional support for other custom cards, please feel free to do a PR or contact @skalavala.
Last modified 1yr ago