{"id":13274,"date":"2021-03-18T16:57:18","date_gmt":"2021-03-18T16:57:18","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13274"},"modified":"2021-03-23T10:00:52","modified_gmt":"2021-03-23T10:00:52","slug":"show-phone-battery-status-with-macro-example","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/show-phone-battery-status-with-macro-example\/","title":{"rendered":"Show Phone Battery status with Macro Example"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"md\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{% macro phonestatus(phone) %}\n{% set battery = states('sensor.' ~ phone ~ '_battery_level') %}\n{% set charging = states('sensor.' ~ phone ~ '_battery_state') %}\n{% set title = state_attr('sensor.' ~ phone ~ '_battery_state','friendly_name') | replace('Battery State','') %}\n\n{% set level = battery | float | multiply(0.1) | round(0,\"floor\") | multiply(10)| round(0) %}\n{% if battery |float &amp;lt; 50 %}\n{% set color = \"orange\" %}\n{% elif battery | float &amp;lt; 20 %}\n{% set color = \"red\" %}\n{% else %} {% set color = \"green\" %}\n{% endif %}\n{% if charging == 'charging' or charging == 'full' %}\n{% set s = \"charging-\" %}\n{% endif %}\n{% set icon = '&lt;span style=\"color: ' ~ color ~ ';\">' ~ '&lt;\/span>' %}\n{{icon}} {{battery}}% {{title}}\n{% endmacro %}\n\n{{ phonestatus('moto_g_6') }}<\/pre>\n\n\n\n<p>The phone &#8220;name&#8221; 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.<br>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.<\/p>\n\n\n\n<p>Also see<\/p>\n\n\n\n<p><a href=\"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/creating-a-template-entity-with-dynamic-icon\/\">Creating a template entity with dynamic icon<\/a><\/p>\n","protected":false},"template":"","class_list":["post-13274","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}