Usage of jinja macro within a lovelace card

origin card:

type: custom:bar-card
name: " "
height: 42px
min: "0"
max: "500"
decimal: "4"
severity:
  - from: "0"
    to: "50"
    color: green
  - from: "50.0001"
    to: "100"
    color: yellow
  - from: "100.0001"
    to: "150"
    color: orange
  - from: "150.0001"
    to: "200"
    color: red
  - from: "200.0001"
    to: "300"
    color: purple
  - from: "300.0001"
    to: "500"
    color: "#7e0023"
entity_row: true
positions:
  icon: "off"
  indicator: "off"
  value: "off"
entities:
  - entity: sensor.air_quality_index_borken
color: grey
card_mod:
  style: |
    ha-card {
      padding: 12px;
      margin-left: 12px;
      --bar-card-border-radius: 12px;
    }
    bar-card-backgroundbar {
      opacity: 0.2;
      filter: brightness(1);
    }

Trying to outsouce the severity part:

      - type: custom:bar-card
        name: " "
        height: 42px
        min: "0"
        max: "500"
        decimal: "4"
        severity: >-
          {% from 'aqi.jinja' import aqi_bar_card_severity %}
          {{ aqi_bar_card_severity() }}
        entity_row: true
        positions:
          icon: "off"
          indicator: "off"
          value: "off"
        entities:
          - entity: sensor.air_quality_index_borken
        color: grey
        card_mod:
          style: |
            ha-card {
              padding: 12px;
              margin-left: 12px;
              --bar-card-border-radius: 12px;
            }
            bar-card-backgroundbar {
              opacity: 0.2;
              filter: brightness(1);
            }

Macro file: aqi.jinja

{% macro aqi_primary(entity_id) %}
{% set state = states(entity_id) | float %}
{% if state <= 50 %} AQI - Good
{% elif state <= 100 %} AQI - Moderate
{% elif state <= 150 %} AQI - Unhealthy for Sensitive Groups
{% elif state <= 200 %} AQI - Unhealthy
{% elif state <= 300 %} AQI - Very Unhealthy
{% elif state > 300 %} AQI - Hazardous
{% endif %}
{% endmacro %}

{% macro aqi_icon_color(entity_id) %}
{% set state = states(entity_id) | float %}
{% if state <= 50 %} green
{% elif state <= 100 %} yellow
{% elif state <= 150 %} orange
{% elif state <= 200 %} red
{% elif state <= 300 %} purple
{% elif state > 300 %} '#7e0023'
{% else %} grey
{% endif %}
{% endmacro %}

{% macro aqi_bar_card_severity() %}
- from: "0"
  to: "50"
  color: green
- from: "50.0001"
  to: "100"
  color: yellow
- from: "100.0001"
  to: "150"
  color: orange
- from: "150.0001"
  to: "200"
  color: red
- from: "200.0001"
  to: "300"
  color: purple
- from: "300.0001"
  to: "500"
  color: "#7e0023"
{% endmacro %}

The first two macros are working, but the last one NOT.

Any ideas in which way to achieve?

The first two Jinja2 macros contain valid Jinja2 code. The third one doesn’t. It appears to contain YAML. You can’t use Jinja2 to create YAML code (because Home Assistant processes YAML and Jinja2 in two separate steps, all YAML first, then Jinja2.

I’ve found the following workarround:

severity: !include /config/custom_templates/aqi-bar-severity.yaml

Include file: /config/custom_templates/aqi-bar-severity.yaml

- from: "0"
  to: "50"
  color: green
- from: "50.0001"
  to: "100"
  color: yellow
- from: "100.0001"
  to: "150"
  color: orange
- from: "150.0001"
  to: "200"
  color: red
- from: "200.0001"
  to: "300"
  color: purple
- from: "300.0001"
  to: "500"
  color: "#7e0023"