-
|
Checklist
Describe the bug Version of the card To Reproduce Expected behavior Additional context I tried an if loop where the icon is set, but this also just throws an error: Hope someone can get me out of this. Best regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Two options. Both of these require sensor to have device class of battery which then manages the battery icon. If this is not possible with yoru existing sensor then you will need to use a template sensor with device class of battery.
type: custom:button-card
name: Battery
styles:
grid:
- grid-template-areas: "\"n extra extra1\""
- grid-template-columns: 1fr 1fr 1fr
custom_fields:
extra: |
[[[
// Wait for updateComplete
this.updateComplete.then(() => {
if (!this.myExtra) {
this.myExtra = true;
let battery = states['sensor.door_battery'].state + '%'
let stateIcon = document.createElement('ha-state-icon')
stateIcon.hass = hass;
stateIcon.stateObj = states['sensor.door_battery']
stateIcon.stateValue = states['sensor.door_battery'].state
stateIcon.style.setProperty('--mdc-icon-size', '50px')
stateIcon.style.setProperty('--icon-primary-color', 'var(--state-sensor-battery-medium-color)')
this.shadowRoot.querySelector("#extra").append(stateIcon);
let text = document.createElement("div")
text.innerText = battery
this.shadowRoot.querySelector("#extra").append(text);
}
});
// Return a space to generate element
return " ";
]]]
extra1:
card:
type: custom:button-card
entity: sensor.door_battery
show_name: false
show_state: true
size: 50px |
Beta Was this translation helpful? Give feedback.


Two options. Both of these require sensor to have device class of battery which then manages the battery icon. If this is not possible with yoru existing sensor then you will need to use a template sensor with device class of battery.