Use variables in card configuration?

I am trying to have some general variables easily accessible on the top lines of my card yaml.

Is this somehow possible?

variables:
  custom_switch: switch.docker_portainer
  web_url: https://portainer.***.duckdns.org

type: custom:stack-in-card
cards:
  - type: custom:button-card
    entity: ???[custom_switch]???
    tap_action:
      action: url
      url_path: ???[web_url]???
 [...]

Can yaml-anchors help you to achieve your goal?

anchor_custom_switch: &ref_custom_switch switch.docker_portainer
anchor_web_url: &ref_web_url https://portainer.***.duckdns.org

type: custom:stack-in-card
cards:
  - type: custom:button-card
    entity: *ref_custom_switch 
    tap_action:
      action: url
      url_path: *ref_web_url

… once I save the card the anchors are converted to plain text:

anchor_custom_switch: &ref_custom_switch switch.docker_portainer
(...)
entity: *ref_custom_switch 

becomes (when I tap “edit card” again):

anchor_custom_switch: switch.docker_portainer
(...)
entity: switch.docker_portainer

So I cannot go back and edit the two variables on the top anymore if I hit “edit card” again in the future.

That defeats my purpose. :thinking:

That would be a solution if I manage and edit my code not in the UI itself but offline (in VSCode or so). But that makes things much more complicated to manage in general for me …

yaml-anchors can be only used in yaml, not in UI-driven dashboards.

I mean they do work … they are just converted after one time use. ^^

Yaml-anchors are mainly to re-use a code: define in 1 place, use in many places - and edit in 1 place and see other places affected.
Most of code re-use features are only available in yaml dashboards.

… so any chance the “variables” thing somehow works?

Depends on what you want to achieve.
Explain your scenario.

I have a section with a lot of docker containers tiles (15+):

Screenshot 2024-07-24 at 09.30.28

The code for every single tile looks like this:

type: custom:stack-in-card
fill_container: false
layout_options:
  grid_columns: 1
  grid_rows: 1.55
cards:
  - type: custom:button-card
    entity: switch.docker_portainer
    show_entity_picture: true
    name: Portainer
    entity_picture: /local/icons/portainer.png
    tap_action:
      action: url
      url_path: https://portainer.***.duckdns.org
    state:
      - value: 'off'
        styles:
          card:
            - filter: opacity(30%)
          icon:
            - filter: grayscale(100%)
    card_mod:
      style: |
        ha-card {
          border: 0px !important;
          #background: #262626 !important;
        }
  - type: custom:button-card
    entity: switch.docker_portainer
    show_icon: false
    show_name: false
    show_state: true
    state:
      - value: 'on'
        styles:
          card:
            - filter: opacity(100%)
            - color: '#00AD3A'
            - font-weight: 500
            - font-size: 15px
            - text-transform: uppercase;
      - value: 'off'
        styles:
          card:
            - filter: opacity(30%)
            - font-weight: 500
            - font-size: 15px
            - text-transform: uppercase;
    card_mod:
      style: |
        ha-card {
          border: 0px !important;
          background: #262626 !important;
          #background: #1C5B31 !important;
        }
card_mod:
  style: |
    ha-card {
      #border: 0px !important;
      #background: #262626 !important;
      border-radius: 5px !important;
    }

Now when I edit a tile (in the GUI!) I would like to have two variables at the very top of that code. The entity (for example: switch.docker_portainer) and the URL (for example: https://portainer.***.duckdns.org).

Screenshot 2024-07-24 at 09.34.45

Google about a custom decluttering-card - it allows you to create a template for similar cards. But I have no idea about a possibility to use custom cards in a section view.

1 Like

Exactly what I was looking for! Thanks a lot.

Now I narrowed down the code to just this for each tile:

type: custom:decluttering-card
template: docker_template
layout_options:
  grid_columns: 1
variables:
  - name: Portainer
  - ico_url: /local/icons/portainer.png
  - web_url: https://portainer.***.duckdns.org
  - entity_1: switch.docker_portainer

Hi!! I’m facing a similar problem but I don’t understand your solution (I’m a newbie).
I’m using a picture-elements and I want to use some variables, but how I have to write it?
This is the code:

views:
  - title: Home
    panel: true
    cards:
      - type: picture-elements
        variables:
          - dimensione_font: 20%
          - scala: 5
        elements:
          - type: state-label
            entity: sensor.camera_temperature
            style:
              top: 35%
              left: 48%
              color: black
              font-size: 100%
              scale: ???[scala]???

Thank you,
Diego