Config Template Card

repository·master·Indexed 20 days ago

https://github.com/iantrich/config-template-card

A Lovelace card for Home Assistant (version 1.3.1) that enables the use of JavaScript templates within card configurations. It allows for dynamic UI elements that react to Home Assistant states, variables, and custom functions by wrapping cards, rows, or elements. It provides access to the hass, states, and user objects, and supports both local and dashboard-wide variables via the config_template_card_vars key.

Tokens
2.5K
Snippets
8
Records
10
Agent score
69%

What's inside config-template-card

  1. Apply styles to elements and cards

    master

    When using config-template-card inside a picture-elements card or as a wrapper:

    1. The style object inside the element configuration is applied to the element itself.
    2. The style object inside the config-template-card configuration is applied to the surrounding card.

    Note: For picture-elements elements, you must configure top and left attributes on the config-template-card to position it correctly.

    type: picture-elements
    image: http://example.com/image.jpg
    elements:
      - type: 'custom:config-template-card'
        variables:
          - states['light.bed_light'].state
        entities:
          - light.bed_light
        element:
          type: icon
          icon: "${vars[0] === 'on' ? 'mdi:home' : 'mdi:circle'}"
          style:
            '--paper-item-icon-color': '${ states['sensor.light_icon_color'].state }'
        style:
          top: 47%
          left: 75%
  2. Define dashboard-wide variables

    master

    To avoid repeating variable definitions in every card, you can define them once for the entire dashboard using the config_template_card_vars key at the dashboard level.

    Both arrays and objects are supported. If you mix types (e.g., an array in dashboard variables and an object in a card), the dashboard variables are placed first in the vars array. If both are arrays, dashboard variables occupy the first indices of vars in the card.

    title: My dashboard
    
    config_template_card_vars:
      - states['sensor.light'].state
    
    views:
  3. Configure Config Template Card options

    master

    The custom:config-template-card requires specific configuration keys to function. Use entities to tell the card which Home Assistant entities to watch for changes so the templates can re-evaluate.

    NameTypeRequirementDescription
    typestringRequiredcustom:config-template-card
    entitieslistRequiredList of entity strings that should be watched for updates. Templates can be used here
    variableslistOptionalList of variables (can be templates) used in config and indexed via vars or by name
    cardobjectOptionalCard configuration (must be provided)
    rowobjectOptionalRow configuration (must be provided)
    elementobjectOptionalElement configuration (must be provided)
    styleobjectOptionalStyle configuration
  4. Use templates and variables in config-template-card

    master

    The card allows you to use JavaScript-style templates within strings using the ${...} syntax. These templates are evaluated against the Home Assistant state object and user information.

    Available Context

    Inside a template ${...}, you have access to:

    • states: The Home Assistant states object (e.g., states['light.kitchen'].state).
    • user: The current Home Assistant user object.

    Defining Variables

    You can define variables to simplify your templates using the variables key in your configuration. Variables can be defined in two ways:

    1. Local Variables: Defined directly within the config-template-card configuration under the variables key.
    2. Dashboard-wide Variables: Defined in your Lovelace dashboard configuration under config_template_card_vars. These are accessible to all config-template-card instances on that dashboard.

    Variables can be an object (for named variables) or an array (for expressions that evaluate to values).

    config-template-card:
      card:
        type: sensor
        entity: sensor.temperature
        name: "Temp is ${states['sensor.temperature'].state}"
      variables:
        my_temp: "states['sensor.temperature'].state"
      entities:
        - sensor.temperature
  5. Use variables and global functions in templates

    master

    You can define complex logic or reusable functions within the variables block. This allows you to call these functions anywhere within the scope of the card.

    type: 'custom:config-template-card'
      variables:
        setTempMessage: |
          temp => {
            if (temp <= 19) {
                return 'Quick, get a blanket!';
            }
            else if (temp >= 20 && temp <= 22) {
              return 'Cozy!';
            }
            return 'It\'s getting hot in here...';
          }
        currentTemp: states['climate.ecobee'].attributes.current_temperature
      entities:
        - climate.ecobee
      card:
        type: entities
        entities:
          - entity: climate.ecobee
            name: '${ setTempMessage(currentTemp) }'
  6. Apply dynamic styles to elements

    master

    You can apply dynamic CSS styles to the wrapped element or the card itself using the style key. The style object is evaluated for templates just like the card configuration, allowing you to change visual properties based on entity states.

    • If using element mode, styles can be applied to the element itself or the wrapper container.
    • Styles are applied via element.style.setProperty.
    config-template-card:
      element:
        type: state-badge
        entity: light.living_room
      style:
        "--paper-item-icon-color": "${states['light.living_room'].state === 'on' ? 'yellow' : 'grey'}"
      entities:
        - light.living_room
  7. Configure the ConfigTemplateCard

    master

    The ConfigTemplateConfig interface defines the configuration object for the config-template-card. This card allows you to use variables and templates within your Home Assistant dashboard configuration.

    Key configuration properties include:

    • type: Must be set to config-template-card.
    • entities: An array of entity IDs (strings) that the template depends on. The card will re-evaluate templates when these entities change.
    • variables: An optional object or array used to define custom variables for your templates. If using an object, it follows the format { [key: string]: string }.
    • card: An optional object containing the configuration for the actual Lovelace card you want to wrap/template.
    • row: An optional object used when templating an entity row within an entities card.
    • element: An optional object used when templating a specific element within a picture-elements card.
    • style: An optional object for applying CSS styles to the card.
    {
      "type": "config-template-card",
      "entities": [
        "sensor.example_entity"
      ],
      "variables": {
        "my_var": "value"
      },
      "card": {
        "type": "entities",
        "entities": [
          "light.living_room"
        ]
      }
    }
  8. Configure the config-template-card

    master

    The config-template-card is a wrapper used to inject dynamic templates into Home Assistant cards, rows, or elements. To use it, you must define exactly one of the following top-level keys in your configuration:

    • card: Defines a standard Lovelace card.
    • row: Defines a Lovelace row.
    • element: Defines a specific element (e.g., for use within a picture-elements card).

    Requirements:

    • You must provide an entities list. The card uses these entities to monitor state changes and trigger template re-evaluations.
    • If using card, you must specify a type.
    • If using element, you must specify a type.

    Warning for picture-elements users: Do not use config-template-card as the main card type for a picture-elements card. Instead, use config-template-card with the element key to wrap individual elements within a picture-elements card.

    config-template-card:
      card:
        type: entities
        entities:
          - entity: light.living_room
      entities:
        - light.living_room
  9. Available variables for templating

    master

    When writing templates inside ${} blocks, you have access to the following objects:

    • this.hass: The Home Assistant hass object.
    • states: The Home Assistant states object.
    • user: The Home Assistant user object.
    • vars: An object or array containing the variables defined in your variables configuration.
      • If variables is a list, vars is an array (e.g., vars[0]).
      • If variables is an object, vars is a string-indexed map, and you can also access variables directly by their name.