Dynamic icon based on battery level (iOS)

Hi,
I added this template to my battery sensor for my iPhone and iPad, and it works really well :slight_smile:

icon_template: '{%- if is_state("sensor.battery_iphone", "unknown") %}mdi:battery-unknown{%- elif is_state_attr("device_tracker.iphone", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.iphone.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.iphone.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.iphone.attributes.battery|float / 10)|round*10}}{%- endif %}'
    

It sets the icon for the sensor based on the battery level precentage.

Sensor config:

- platform: template
  sensors:
    battery_iphone:
      unit_of_measurement: '%'
      value_template: >
          {%- if states.device_tracker.iphone.attributes.battery %}
              {{ states.device_tracker.iphone.attributes.battery|round }}
          {% else %}
              {{ states.sensor.battery_iphone.state }}
          {%- endif %}
      icon_template: '{%- if is_state("sensor.battery_iphone", "unknown") %}mdi:battery-unknown{%- elif is_state_attr("device_tracker.iphone", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.iphone.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.iphone.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.iphone.attributes.battery|float / 10)|round*10}}{%- endif %}'
    

Sensor config is based on the example here: https://home-assistant.io/cookbook/track_battery_level/

Uppdated 21.03.17:

  • Added support for charging icon
  • fixed an issue where the icon was blank if battery level was exactly 5 or 95 %
22 Likes

Nice work!

Is templating icons only possible in the sensor section?
I’m having trouble with this in the customize section:
https://community.home-assistant.io/t/templating-icons-in-customize-section/13993?u=bob_nl

If anyone could make this work for android and the “mqtt” way of getting battery level, that would be great.
I have tried for 2 day untill i today realised that your code was for ios… :slight_smile:

I’m not sure, I think I have only seen it been used in an sensor template :confused:
You can try to add it as an FR :slight_smile:

1 Like

Thanks, will do that.

How does the battery level show up in that way?

If you have it in the dev-states part of the front end this should be possible :slight_smile:

What platform are you using to track iphone?
I am using owntracks via cloudmqtt and as far as I can tell it will not support charging status.
Are you using iCloud or HA iOS beta app?

Man I wish owntracks was better… it pushes updates so infrequently that it’s almost useless.
Its odd though because I think the iOS significant location change API works well - IFTTT warns me of geofence very responsively.
/rant :slight_smile:

1 Like

I’m using the icloud platform now, tried Owntracks over MQTT earlier but it was too slow :frowning:

But I also have applied this to my netatmo outdoor module, so I think this can be used with everythin as long as the battery show up in ‘dev-states’.
https://github.com/ludeeus/hass-config/blob/master/Sensor/template.yaml

1 Like

Finaly i got it working.
Here is my code:

mctasker_batt:
  friendly_name: "McTasker Batt"
  unit_of_measurement: "%"
  value_template: "{{states.device_tracker.matte_location.attributes.battery}}"
  icon_template: "{%- if  states.device_tracker.matte_location.attributes.battery >= '95' %}mdi:battery{%- elif states.device_tracker.matte_location.attributes.battery <= '15' %}mdi:battery-outline{% else %}mdi:battery-{{(states.device_tracker.matte_location.attributes.battery|float / 10)|round*10}}{%- endif %}"

EDIT:
Found out now that when battery is 100% i get “mdi:battery-outline” ?

1 Like

Nice :slight_smile:
Is this MQTT?

Found out now that when battery is 100% i get “mdi:battery-outline” ?
I will look in to that later today :slight_smile:

Tested this now, I got ‘mdi:battery’ when at 100…
I will set up Owntracks over MQTT later this evening, to see the difference, maybe come up with something that work on both :slight_smile:

Its working with owntracks/MQTT for me.
I’m at work right now but will share the config later this evening.
But as I mentioned there is no charging indicator from owntracks

So now I have set up owntracks on my iPhone and applied this template to it:
Link to config here.

And the icon is identical to the sensor for icloud…
Have I misunderstood the issues you are having?

I see that Owntracks is a bit slower to update values, but when it does, the icon do change, at least for me.

All works except the change you made for charging battery.
I guess that owntracks sends only battery level only - I dont get the indicator for charging.

Aha, for charging, will check on my end :slight_smile:

Edit: It looks like this can’t be done for now, owntracks does not send that data, and I can’t find a way to turn it on.
Found an FR on their github to add this support, but that was discarded last year.

I am using the owntrack service in my HASS setup, but i dont use the owntrack app on my phones.
I use tasker to send a MQTT message regulary instead.
This way i can now even include charging status from the phone.
http://i.imgur.com/2CrAUGO.png
No power
http://i.imgur.com/oWHxzVu.png
Power.

My template setup:

- platform: template
  sensors:
    mctasker_batt:
      friendly_name: "Battery Level"
      unit_of_measurement: "%"
      value_template: "{{states.device_tracker.matte_location.attributes.battery}}"
      icon_template: "{%- if  states.device_tracker.matte_location.attributes.battery >= '95' %}mdi:battery{%- elif states.device_tracker.matte_location.attributes.battery <= '15' %}mdi:battery-outline{% else %}mdi:battery-{{(states.device_tracker.matte_location.attributes.battery|float / 10)|round*10}}{%- endif %}"

    mctasker_power_state:
      friendly_name: "Charging Status"
      value_template: "{%- if  states.sensor.mctasker_power.state == 'ac' %}Power connected{%- elif states.sensor.mctasker_power.state == 'usb' %}Power connected{% else %}Power disconnected{%- endif %}"
      icon_template: "{%- if  states.sensor.mctasker_power.state == 'ac' %}mdi:battery-charging{%- elif states.sensor.mctasker_power.state == 'usb' %}mdi:battery-charging{% else %}mdi:battery-unknown{%- endif %}"

My tasker send mqtt string:

{"_type": "location","acc":%LOCACC,"batt":"%BATT","lat":"%MatteLoc1","lon":"%MatteLoc2","tid":"ml","tst":%TIMES,"s6power":"%myPOWER"}

Everything works fine exept the battery_outline at 100%.

2 Likes

beautiful :smiley:

For the outline part:
I testet your template in my env, and the icon got blank…
When I changed ‘15’ to 15 it worked :slight_smile:

If i remove the ’ around the values my template does not work at all!
But if i add this string to my template it works:

{%- elif states.device_tracker.matte_location.attributes.battery == '100' %}mdi:battery

so the total template would look like this:

mctasker_batt:
  friendly_name: "Battery Level"
  unit_of_measurement: "%"
  value_template: "{{states.device_tracker.matte_location.attributes.battery}}"
  icon_template: "{%- if  states.device_tracker.matte_location.attributes.battery >= '95' %}mdi:battery{%- elif states.device_tracker.matte_location.attributes.battery == '100' %}mdi:battery{%- elif states.device_tracker.matte_location.attributes.battery <= '15' %}mdi:battery-outline{% else %}mdi:battery-{{(states.device_tracker.matte_location.attributes.battery|float / 10)|round*10}}{%- endif %}"
1 Like

When I add this icon template I noticed that round function works oddly. For that template

{{ 4.5 | round }} {{ 5.5 | round }} {{ 6.5 | round }} {{ 7.5 | round }}
result should be 5 6 7 8. In HA 0.41.0 is 4 6 6 8.
Anyone can confirm?

Can confirm this for 0.41.0