This template contains Jinja macros that can be used to convert the state of a battery in Home Assistant into either a matching Material Design Icon or a color that matches the current battery level. Both will be returned as strings and can be used in many ways.
One example of a use case would be a Mushroom Template Badge that shows the current battery state of a wall-mounted tablet.
Without HACS
- Download the contents from battery_icons.jinja from the master-branch.
- Add the file to your
<config>/custom_templates-folder. - Reload your configuration from within the developer tools in Home Assistant.
- The template should now be ready for use.
With HACS
- Search for "Battery Icons" in HACS.
- Download the repository via HACS.
- The template should now be ready for use.
You can also use this button to add the repository to your Home Assistant instance via HACS:
Takes the given battery level and the current charging state and returns an Material Design Icon based on them as a string.
The battery level can either be given as a number directly or as a string that will be converted.
battery_icon(battery_level_raw, is_charging=false)
| Param | Type | Explanation |
|---|---|---|
battery_level_raw |
string or number |
Will be converted into a float and used for determining the corresponding icon. |
is_charging |
boolean |
Can be used to change the icon depending on the current charging state of the battery. If omitted, the macro will never return a charging-icon. (Default: false) |
Input
{%- from 'battery_icons.jinja' import battery_icon -%}
{{ battery_icon(states('sensor.wall_tablet_battery_level'), (states('binary_sensor.wall_tablet_is_charging') == 'on')) }}Output
Depends on the current battery states. Here's one example:
mdi:battery-charging-70
Takes the given battery level and returns a corresponding color as a string based on it.
The battery level can either be given as a number directly or as a string that will be converted.
battery_color(battery_level_raw)
| Param | Type | Explanation |
|---|---|---|
battery_level_raw |
string or number |
Will be converted into a float and used for determining the corresponding color. |
Input
{%- from 'battery_icons.jinja' import battery_color -%}
{{ battery_color(states('sensor.wall_tablet_battery_level')) }}Output
Depends on the current battery states. Here's one example:
orange