Use variables within mushroom-template-card

Hi there,

is there a chance to define the variables in a way so that I can use them in other sections of the card (e.g. icon, card_mod,…)
In my exaple below, I would like to use ‘total’ and ‘ein’ also in card_mod without calculating the values again, but it looks like that the calculated values are not present in othe “sections” of the card and have to be calculated again.

type: custom:mushroom-template-card
primary: Garten
multiline_secondary: true
secondary: >-
  {% set total = 0 %} {% set ein = 0 %} {% set
  total=((label_entities("Wasserspiel")if
  states('sensor.sommerzeit')=='True')|list|count) +
     (label_entities("Xmas_out")if states('binary_sensor.weihnachtszeit')=='on')|list|count +
     (label_entities("Gartenbeleuchtung"))|list|count%}

  {% set ein=((label_entities("Wasserspiel")if
  states('sensor.sommerzeit')=='True')| select('is_state', 'on')|list|count) +
     (label_entities("Xmas_out")if states('binary_sensor.weihnachtszeit')=='on')| select('is_state', 'on')|list|count +
     (label_entities("Gartenbeleuchtung"))| select('is_state', 'on')|list|count%}
  {% if ein ==0 %}
   {{"alles aus"}}
  {%elif ein >=1 %}
   {{ein}}/{{total}} Geräte
  {% endif %}
   
icon_color: >-

I was only able to do this because I did something in the first day or two of poking aound with mushroom cards and HA automatically defined and reused a variable in the YAML. Of course, it wasn’t what I wanted to do, and I immediately overwrote the YAML to do what I really wanted. I was lucky that I saved that really weird snippet of code just in case.

My “accidental” discovery of variables happened the same way that my code shows the “|2-” text in a couple of places. For the life of me, I haven’t figured out why it’s there and what it means differently than the other multiline indicators in yaml. But Homeassistant keeps putting it there if I use the graphical editor.

Mainly to see if/how I could do it, I have created a page in my dashboard to help me navigate through all the custom ESP devices I’ve created in my setup. Of course, it’s nice to be able to reset a specific device remotely, but it isn’t useful to have that button on every single dashboard. Every once in a while, I want to be able to access some of the more esoteric entities presented by a device, but don’t want to try and organize all of those very rarely accessed entities in a dashboard, or parse through the default dashboard, which has just become a ridiculously tedious sea of entities. Part of my goal is to eventually have HA spawn the web page hosted by an individual device if I click on the IP address or hostname chip. Unfortunately, so far I haven’t been able to pass the proper URLs via sensor or variable to the tap_action >> URL action path - it is a missing capability of the chip card. The only way the tap_action works is if I fully hardcode the URL, http://<ip address> or http://<hostname>.local - Of course, that defeats the whole idea of using variables and entities…

This is the diagnostic section I made for a Sonoff outlet. I am in the process of doing this for all the devices in my HA installation. Since the code I uploaded to the outlet maxxed out the storage space on the outlet, not everything can be managed by the device itself. The outlet itself doesn’t have the code storage available to store the IP address, SSID it’s connected to, the hostname or the device name as set in HA. Other devices are able to hold that information in text entities or text_sensors, but that only gets me 80% of the way.

You have to define the variable as part of an “entities” card “at the same level” that it will be used. This type of card (so far as I’ve used them) doesn’t show any content, except that they do occupy a little space, creating a bit of a gap at the left of the row, but allows you to create a variable that can be used later. I haven’t found any way to make them not take up that space - the visibility parameter might be a way, but it hasn’t bothered me enough to tackle it yet. Since I use the values within a grid card, I define the variables first and then the individual chips that use those variables.

Here’s the code for this diagnostic bit:

  - square: false
    type: grid
    columns: 1
    title: ''
    grid_options:
      columns: full
    cards:
      - type: custom:mushroom-template-card
        primary: Garage Spider Restart
        secondary: |2-
                {% if is_state ('switch.spider_one_restart', 'unavailable') %}Restarting...
                {% else %}Double Click
                {% endif %}
        icon: mdi:radioactive
        icon_color: |2-
                {% if is_state ('switch.spider_one_restart', 'unavailable') %}pink
                {% else %}lime
                {% endif %}
        tap_action:
          action: none
        hold_action:
          action: none
        double_tap_action:
          action: perform-action
          perform_action: switch.turn_on
          target:
            entity_id: switch.spider_one_restart
  - square: false
    type: grid
    columns: 1
    grid_options:
      columns: full
    cards:
      - type: custom:mushroom-chips-card
        chips:
          - type: entities
            entities:
              # These are dynamic on most of my custom ESP32 devices, but not on the Sonoff
              # outlet due to code space constraints
              - entity: sensor.spider_one_wattage
                name: &ip1 "192.168.0.145"
                # If the device has an IP Address sensor, it can be used to dynamically provide 
                # the value, such as
                #name: &ip1 {{ states('sensor.spider_one_ip_addr') }}
              - entity: sensor.spider_one_wattage
                name: &ipurl1 "http://192.168.0.145"                      
                # If the device has an IP Address sensor, it can be used to dynamically provide 
                # the value, such as
                #name: &ipurl1 "http://{{ states('sensor.spider_one_ip_addr') }}"
              - entity: sensor.spider_one_wattage
                name: &host1 "spider1"                      
              - entity: sensor.spider_one_wattage
                name: &hosturl1 "http://spider1.local"                      
          - type: entity
            entity: sensor.spider_one_wattage
            icon_color: amber
            icon: mdi:gauge
          - type: template
            content: *ip1
            tap_action:
              action: url
              url_path: *ipurl1
            icon_color: teal
            icon: mdi:ip-network
          - type: template
            content: Chameleon.M
            icon_color: teal
            icon: mdi:wifi-settings
          - type: entity
            entity: sensor.spider_one_wifi_signal
            icon_color: teal
            icon: mdi:wifi-check
          - type: template
            content: *host1
            entity: text.garage_sensors_hostname
            icon_color: purple
            icon: mdi:sign-real-estate
            hold_action:
              action: url
              url_path: *hosturl1
          - type: template
            content: Garage Spider
            entity: text.garage_sensors_devname
            icon_color: purple
            icon: mdi:sign-real-estate

When you define the variables, you HAVE TO use an entity that exists. The entity that you choose doesn’t have to be related to the variable at all, but it has to exist within the HA environment. You can use the same entity over and over again, and use a different name (create a new variable) each time. I randomly chose one that is an entity of the device I’m referring to, so that if a different device goes down, this diagnostic isn’t impacted.

I’m planning on trying out the decluttering card to make this way more efficient, but haven’t had the time yet. The decluttering card might solve a couple of problems I’ve run into, most specifically that neither of the URL actions work reliably, since they don’t properly parse the URLs if they’re pulled from entities and don’t seem to parse out any jinja.

You should be able to define &total and &ein just before the first line of the code you showed in the question. Create an entities type card, and then define an entity for each. Since it’s already part of what you’re presenting, just use sensor.sommerzeit as the entity and then define the value of each as needed. I’m sorry - I’m a bit too tired to parse out exactly how to translate exactly how to assign the proper value to your variables…I hope this pushes you in the right direction, though.