Show Phone Battery status with Macro Example

Home Assistant Template
{% macro phonestatus(phone) %}
{% set battery = states('sensor.' ~ phone ~ '_battery_level') %}
{% set charging = states('sensor.' ~ phone ~ '_battery_state') %}
{% set title = state_attr('sensor.' ~ phone ~ '_battery_state','friendly_name') | replace('Battery State','') %}

{% set level = battery | float | multiply(0.1) | round(0,"floor") | multiply(10)| round(0) %}
{% if battery |float < 50 %}
{% set color = "orange" %}
{% elif battery | float < 20 %}
{% set color = "red" %}
{% else %} {% set color = "green" %}
{% endif %}
{% if charging == 'charging' or charging == 'full' %}
{% set s = "charging-" %}
{% endif %}
{% set icon = '<span style="color: ' ~ color ~ ';">' ~ '</span>' %}
{{icon}} {{battery}}% {{title}}
{% endmacro %}

{{ phonestatus('moto_g_6') }}

The phone “name” is the sensor main name so in the example above sensor.moto_g_6_battery_level is the battery level for the phone, based on the sensors created by the Home Assistant app.
This is used on a Markdown card to show coloured icons for the battery level for all our devices. The mdi icon varies based on battery level and if it is plugged in.

Also see

Creating a template entity with dynamic icon