Verwendung von Templates/ wiederkehrende Programmteile

Ist wahrscheinlich eine Anfängerfrage oder Verständnisproblem meinerseits …

Ich habe in einer Mushroom Card im Dashboard einen Ablauf der wiederkehrend ist, allerdings für untrschiedliche Entities. In meinem Fall z.B. Speedtest Internet und auch Abrrage von Sensoren.
Kann man hier den Code in einem Template/script/automation speichern und einfach nur als variable (Text) an die Card übergeben?
Ich hoffe es ist verständlich ausgedrückt? Im Prinzip geht es um den Teil, der unter “Primary” bzw. “Secundary” steht…

Danke schon mal an alle, die bis hierher gelesen haben :slight_smile:

type: custom:mushroom-template-card
entity: sensor.speedtest_download
primary: |

  {% if states [entity] is defined %}
  {# it's safe to access the last_changed attribute #}

    {% set TIME_aktual = as_timestamp( now(),
      default=0)|timestamp_custom('%H:%M:%S',
      default=0)%}
    
    {% set TIME_entity
      =as_timestamp(states[entity].last_changed,
      default=0)|timestamp_custom('%H:%M:%S',
      default=0)%} 
    
    {% set TIME_Diff = as_timestamp (now()) | int(0) -
      as_timestamp(states[entity].last_changed)|int(0) %}
    
    {% set MESSAGE = 'Last Update: *** ' + TIME_Diff |
      timestamp_custom ('%H:%M:%S', false) + ' ***' %}
   

  {% else %}
    {% set MESSAGE = '*** ( NOK ) ***'%}

  {% endif %}

  {{ MESSAGE }}
   
secondary: |-

  {% if states [entity] is defined %}
  {# it's safe to access the last_changed attribute #}
    {% set TIME_aktual = as_timestamp( now(),
      default=0)|timestamp_custom('%H:%M:%S',
      default=0)%}
    {% set TIME_entity
      =as_timestamp(states[entity].last_changed,
      default=0)|timestamp_custom('%H:%M:%S',
      default=0)%} 
    {% set TIME_Diff = as_timestamp (now()) | int(0) -
      as_timestamp(states[entity].last_changed)|int(0) %}
    {% set MESSAGE = '( Last: ' + TIME_entity +
      ' / Now: ' + TIME_aktual + ')' %}

  {% else %}
    {% set MESSAGE = '( NOK )'%}

  {% endif %}

  {{ MESSAGE }} 
icon: mdi:timeline-clock-outline
badge_icon: ""
icon_color: yellow
layout: horizontal
multiline_secondary: false
tap_action:
  action: more-info
hold_action:
  action: toggle
double_tap_action:
  action: perform-action
  perform_action: automation.trigger
  target:
    entity_id: automation.run_speedtest
  data:
    skip_condition: true
grid_options:
  columns: 9
  rows: 1
card_mod:
  style: |
    ha-card {

    {% if states.automation.run_speedtest.attributes.current == 1 %}
       background-color: purple;
       border: 2px solid yellow;
       --card-primary-color: yellow;
       --card-secondary-color: orange;    
    {% else %}    
       background-color: black;
       border: 2px solid yellow;
       --card-primary-color: yellow;
       --card-secondary-color: orange;    
    {% endif %}
    }

Und bringt mir z.B.
Beispiel

Ja, das Stichwort heißt Makro. Hier ein Beispiel auch mit der Mushroom Template Karte.

EDIT
Marco → Makro :slight_smile:
Automatische Wortanpassung wenn man übers Handy schreibt, rr

1 „Gefällt mir“

Ich hatte mich erst gefragt, welcher Marco gemeint ist. Aber du meintest Makros, wie man sie auch von Excel oder anderen Anwendung her kennt.

Danke - hat mir sehr geholfen!