Use a variable as an entity in a card

Hi all,
I’m using the custom graph card and in each of the cards I often have the same variable a number of times, and this is a pain to update.

I’d like to set a variable at the top of the card ‘this_sensor’, and have here the entity once, then use this_sensor elsewhere in the card so I only change the entity once - i hope that makes sense!

Here is my code (note this works, if I just put the entity int he three places, but this is what I want to avoid)

type: custom:mini-graph-card
name: Kitchen
variables: 
  this_sensor: climate.kitchen
icon: mdi:thermometer
aggregate_func: max
hours_to_show: 24
decimals: 2
entities:
  - entity: this_sensor
    attribute: current_temperature
    unit: °C
  - entity: this_sensor
    attribute: temperature
    show_state: true
    show_graph: false
    title: bob
    unit: °C
  - entity: this_sensor
    attribute: temperature
    show_state: false
    show_graph: true
    show_line: false
    color: grey
    show_legend: false
    y_axis: secondary
    smoothing: true
state_map:
  - value: heat
    label: Heat
  - value: 'off'
    label: 'Off'
color_thresholds:
  - value: 21
    color: red
  - value: 19
    color: '#ff7f00'
  - value: 16
    color: '#54abf7'

Thanks

1 Like

Gentle nudge, any ideas on how to solve?
I’ve looked at Anchors but haven’t managed to get them working

thx

Yes I second this. I have, for example, temperature and battery card entities that change color and/or icon depending on state. Updating and modifying is very time consuming. A variable would make life so much easier.
Cheers

1 Like

I have a solution. Not sure if this is exactly what you want but nonetheless here it is.
The code I show here is for the first sensor shown in the image at the bottom. The others are the same except for the entity so I don’t bother to include all the redundant code.

I use

{% set sentity = "sensor.aqara_01_battery" %} 

in the style section to define the entity variable that can be used for all the formatting.

Hope this helps.
Cheers

type: entities
title: Battery - Aqara Environmental Sensors
theme: blackback
entities:
  - entity: sensor.aqara_01_battery
    name: 01 Basement
    state_header: null
    style: |
      :host {
        {% set sentity = "sensor.aqara_01_battery" %}
        {% if is_state(sentity, 'Off') %}
          --paper-item-icon-color: red;
          --card-mod-icon: mdi:battery-off;
        {% else %}
          {% if states(sentity)| float(-999) == 100 %}
            --paper-item-icon-color: lime;
            color: lime;
            --card-mod-icon: mdi:battery;
          {% elif states(sentity)| float(-999) >= 90 %}
            --paper-item-icon-color: lawngreen;
            color: lawngreen;
            --card-mod-icon: mdi:battery-90;
          {% elif states(sentity)| float(-999) >= 80 %}
            --paper-item-icon-color: rgb(50,255,0);
            color: rgb(50,255,0);
            --card-mod-icon: mdi:battery-80;
          {% elif states(sentity)| float(-999) >= 70 %}
            --paper-item-icon-color: greenyellow;
            color: greenyellow;
            --card-mod-icon: mdi:battery-70;
          {% elif states(sentity)| float(-999) >= 60 %}
            --paper-item-icon-color: yellow;
            color: yellow;
            --card-mod-icon: mdi:battery-60;
            color: yellow;
          {% elif states(sentity)| float(-999) >= 50 %}
            --paper-item-icon-color: orange;
            color: orange;
            --card-mod-icon: mdi:battery-50;
          {% elif states(sentity)| float(-999) >= 40 %}
            --paper-item-icon-color: darkorange;
            color: darkorange;
            --card-mod-icon: mdi:battery-40;
          {% elif states(sentity)| float(-999) >= 30 %}
            --paper-item-icon-color: orangered;
            color: orangered;
            --card-mod-icon: mdi:battery-30;
          {% elif states(sentity)| float(-999) >= 20 %}
            --paper-item-icon-color: crimson;
            color: crimson;
            --card-mod-icon: mdi:battery-20;
          {% elif states(sentity)| float(-999) < 10 %}
            --paper-item-icon-color: red;
            color: red;
            --card-mod-icon: mdi:battery-10;
          {% else %}
            --paper-item-icon-color: white;
            --card-mod-icon: mdi:battery-alert;
          {% endif %}
        {% endif %};
        ;
      }

2 Likes