This is my thermostat card:
It’s using the custom:button card template you’ll find here: GitHub - custom-cards/button-card: Lovelace button-card for home assistant which I can’t say enough good about.
The full code for the thermostat is as follows:
- entity: climate.thermostat_2
type: 'custom:button-card'
icon: 'mdi:thermostat'
aspect_ratio: 3/2
name: Thermostat
variables:
mode_icon: |
[[[
if (states["climate.thermostat_2"].state == "heat") {
return "hass:fire"; }
if (states["climate.thermostat_2"].state == "cool") {
return "hass:snowflake"; }
else return "hass:thermostat";
]]]
styles:
card:
- background-color: |
[[[
if (states['climate.thermostat_2'].state == 'heat') return 'darkRed';
if (states['climate.thermostat_2'].state == 'cool') return 'darkBlue';
if (states['climate.thermostat_2'].state == 'off') return 'darkSlateGrey';
else return 'yellow';
]]]
- border-radius: 0%
- padding: 1%
- color: ivory
- font-size: 12px
- text-transform: capitalize
grid:
- grid-template-areas: >-
"n n n n" "i temp temp temp" "stat stat stat stat" "fan
fan fan fan"
- grid-template-columns: 1fr 1fr 1fr 1fr
- grid-template-rows: min-content 1fr min-content min-content
name:
- font-weight: bold
- font-size: 13px
- color: white
- align-self: middle
- justify-self: start
- padding-bottom: 0px
img_cell:
- justify-content: start
- align-items: start
- margin: 0%
icon:
- color: yellow
- width: 100%
- margin-top: 0%
custom_fields:
temp:
- font-size: 13px
- align-self: middle
- justify-self: start
stat:
- padding-bottom: 2px
- align-self: middle
- justify-self: start
- '--icon-color-sensor': |
[[[
if (states["climate.thermostat_2"].state == "heat") {
if (states['climate.thermostat_2'].attributes['hvac_action'] == "heating") return "orange";
if (states['climate.thermostat_2'].attributes['hvac_action'] == "fan") return "yellow";
return "white"; }
if (states["climate.thermostat_2"].state == "cool") {
if (states['climate.thermostat_2'].attributes['hvac_action'] == "cooling") return "lightSkyBlue";
if (states['climate.thermostat_2'].attributes['hvac_action'] == "fan") return "lightGreen";
return "white"; }
else return "lightGrey";
]]]
fan:
- align-self: middle
- justify-self: start
- '--icon-color-sensor': |
[[[
if (states["climate.thermostat_2"].attributes["fan_mode"] == "on") {
if (states['climate.thermostat_2'].attributes['fan_action'] == "running") return "lightSkyBlue";
return "white"; }
if (states["climate.thermostat_2"].attributes["fan_mode"] == "auto") {
if (states['climate.thermostat_2'].attributes['fan_action'] == "running") return "lightSkyBlue";
return "white"; }
else return "lightGrey";
]]]
custom_fields:
temp: |
[[[
return `<span style="font-size: 24px;">${states['climate.thermostat_2'].attributes['current_temperature']}°F </span>
<span>Set:${states['climate.thermostat_2'].attributes['temperature']}°F</span>`
]]]
stat: |
[[[
return `<ha-icon icon="${variables.mode_icon}"
style="width: 14px; height: 14px; color: var(--icon-color-sensor);">
</ha-icon><span> mode: <span>${states['climate.thermostat_2'].state} | </span>
<span>${states['climate.thermostat_2'].attributes['hvac_action']}</span>`
]]]
fan: |
[[[
return `<ha-icon icon="mdi:fan"
style="width: 14px; height: 14px; color: var(--icon-color-sensor);">
</ha-icon><span> fan: <span>${states['climate.thermostat_2'].attributes['fan_mode']} | </span>
<span>${states['climate.thermostat_2'].attributes['fan_action']}</span>`
]]]
My question is simple: Is it more efficient to use the CSS variables like I do in the “stat” field:
stat:
- padding-bottom: 2px
- align-self: middle
- justify-self: start
- '--icon-color-sensor': |
[[[
if (states["climate.thermostat_2"].state == "heat") {
if (states['climate.thermostat_2'].attributes['hvac_action'] == "heating") return "orange";
if (states['climate.thermostat_2'].attributes['hvac_action'] == "fan") return "yellow";
return "white"; }
if (states["climate.thermostat_2"].state == "cool") {
if (states['climate.thermostat_2'].attributes['hvac_action'] == "cooling") return "lightSkyBlue";
if (states['climate.thermostat_2'].attributes['hvac_action'] == "fan") return "lightGreen";
return "white"; }
else return "lightGrey";
]]]
stat: |
[[[
return `<ha-icon icon="${variables.mode_icon}"
style="width: 14px; height: 14px; color: var(--icon-color-sensor);">
</ha-icon><span> mode: <span>${states['climate.thermostat_2'].state} | </span>
<span>${states['climate.thermostat_2'].attributes['hvac_action']}</span>`
]]]
or would it be more efficient to use template variables as I do here:
variables:
mode_icon: |
[[[
if (states["climate.thermostat_2"].state == "heat") {
return "hass:fire"; }
if (states["climate.thermostat_2"].state == "cool") {
return "hass:snowflake"; }
else return "hass:thermostat";
]]]
stat: |
[[[
return `<ha-icon icon="${variables.mode_icon}"
style="width: 14px; height: 14px; color: var(--icon-color-sensor);">
</ha-icon><span> mode: <span>${states['climate.thermostat_2'].state} | </span>
<span>${states['climate.thermostat_2'].attributes['hvac_action']}</span>`
]]]
Both work well with the coding above. It’s really just a matter of consistency to use only one of them. What is the assembled wisdom of you folks that are more familiar with CSS and templates?