Dynamic icon based on battery level (iOS)

Rounding a half to even is how I was taught, so seems correct to me.

Just a method selection.

Per Wikipedia:

Round half to even.
This is the default rounding mode used in IEEE 754 computing functions and operators.

Maybe someone here can help an mqtt and owntracks n00b. I’m using Owntracks with Mosquitto and it’s reporting my presence. I’m trying to set this battery-thing up, but I can’t seem to figure out what topic I should subscribe to. How do I know?

Here is what I’m using.

- platform: mqtt
  state_topic: "owntracks/username/device ID"
  name: "Mike Phone Battery"
  qos: 0
  unit_of_measurement: "%"
  value_template: '{{ value_json.batt }}'

Thank you!

How can I apply dynamic icon to my config?

I tried following the post’s, but it didn’t work.

My Sensor Config:

- platform: mqtt
  state_topic: "owntracks/note4/Note4"
  name: "Note 4 Battery"
  unit_of_measurement: "%"
  value_template: '{{ value_json.batt }}'

This works for me
Sensors config

  • platform: mqtt
    state_topic: “owntracks/user/device”
    name: “Mukund Phone Battery”
    unit_of_measurement: “%”
    value_template: ‘{{ value_json.batt }}’
  • platform: template
    sensors:
    mukund_batt:
    friendly_name: “Mukund Phone”
    unit_of_measurement: “%”
    value_template: “{{states.sensor.mukund_phone_battery.state}}”
    icon_template: “{%- if states.sensor.mukund_phone_battery.state >= ‘95’ %}mdi:battery{%- elif states.sensor.mukund_phone_battery.state == ‘100’ %}mdi:battery{%- elif states.sensor.mukund_phone_battery.state <= ‘15’ %}mdi:battery-outline{% else %}mdi:battery-{{(states.sensor.mukund_phone_battery.state|float / 10)|round*10}}{%- endif %}”

I have an issue that just started on my ipad and ipad mini but works fine on my iphone. I have the same config on all 3. If I test templates in the Developer tools and put the iphone template in it works but when I change the devicetracker name to my ipad it gets the error,

here is my sensor config:

- platform: template
  sensors:
    battery_iphone:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_iphone.attributes.battery %}
              {{ states.device_tracker.tylers_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.tylers_iphone", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_iphone.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_iphone.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_iphone.attributes.battery|float / 10)|round*10}}{%- endif %}'


- platform: template
  sensors:
    battery_ipad:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_ipad.attributes.battery %}
              {{ states.device_tracker.tylers_ipad.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.tylers_ipad", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_ipad.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_ipad.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_ipad.attributes.battery|float / 10)|round*10}}{%- endif %}'

- platform: template
  sensors:
    battery_ipad_mini:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_ipad_mini.attributes.battery %}
              {{ states.device_tracker.tylers_ipad_mini.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.tylers_ipad_mini", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_ipad_mini.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_ipad_mini.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_ipad_mini.attributes.battery|float / 10)|round*10}}{%- endif %}'      

and here are the errors:

[homeassistant.components.sensor.template] Could not render icon template battery_ipad: UndefinedError: ‘mappingproxy object’ has no attribute ‘battery’

[homeassistant.components.sensor.template] Could not render icon template battery_ipad_mini: UndefinedError: ‘mappingproxy object’ has no attribute ‘battery’

This appears to have been fixed by opening my ipads and relaunching home assistant.

Here’s my version for MQTT and Owntracks. This combines the battery and charging symbols into one. It also allows for the strange gaps in the charging icons in MDI…

sensor:
  - platform: mqtt
    state_topic: "owntracks/bertbert/bertbert"
    name: "Bertbert Battery Charging"
    value_template: '{{ value_json.charging }}'
  - platform: template
    sensors:
      bertbert_battery:
        unit_of_measurement: "%"
        value_template: '{{ states.device_tracker.bertbert_bertbert.attributes.battery }}'
        icon_template: >
          {% set battery = [('-outline',''),('-10',''),('-20','-20'),('-30','-20'),('-40','-40'),('-50','-40'),('-60','-60'),('-70','-60'),('-80','-80'),('-90','-90'),('','-100')] %}
          {% set charging = states.sensor.bertbert_battery_charging.state|int %}
          {% set charge = (states.device_tracker.bertbert_bertbert.attributes.battery|float / 10)|round %}
          mdi:battery{% if charging == 1 %}-charging{% endif %}{{ battery[charge][charging] }}
1 Like

Hi all,

I tried one of the examples above but so far it did not change the mdi icon, I also followed the expample that is given here:

https://bonani.tech/track-battery-levels-with-home-assistant-and-custom-ui/

And this is my code in sensor.yaml

 - platform: template
   sensors:
     schuifraam_battery:
      unit_of_measurement: '%'
      value_template: '{{ states.schuifraam_battery.attributes|int }}'
      icon_template: >-
        {% set battery_level = states.sensor.schuifraam_battery.state|int * 10 %}
        {% set battery_round = (battery_level / 10) |int * 10 %}
        {% if battery_level >= 100 %}
          mdi:battery
        {% elif battery_level > 0 %}
          mdi:battery-{{ battery_level }}
        {% else %}
          mdi:battery-alert
        {% endif %}

My sensor is a Xiaomi and it communicates with mqtt via SmartThings to my Synology.

Kind regards,

Glenn

why don’t you use device_class: battery?
This will automatically set the icon

3 Likes

@ludeeus
Hi!

I am trying to make something like this but than with color based icon, is that possible?

Example:

The small icons i can change by state = 1 2 3 … its only till 10.
But the ‘Luchtkwaliteit’ goes from 0 - 250

type: custom:button-card
entity: sensor.acc_air_tomorrow
aspect_ratio: 0
tap_action:
  action: fire-dom-event
  browser_mod:
    command: popup
    title: Luchtkwaliteit Morgen
    style:
      hui-vertical-stack-card:
        $hui-entities-card$: |
          #states {
            padding-top: 0.5em;
            padding-bottom: 1.2em;
          }
    card:
      type: vertical-stack
      cards:
        - type: entity
          entity: sensor.acc_air_tomorrow
          name: Luchtkwaliteit Morgen
        - type: entities
          card_mod:
            class: content
          entities:
            - type: custom:gap-card
              height: 10
            - entity: sensor.acc_air_tomorrow_info
              name: Info
            - type: custom:gap-card
              height: 10
      style: |
        ha-card {
          box-shadow: none;
          background: transparent !important;
         }
name: Luchtkwaliteit
color_type: icon
show_state: true
show_entity_picture: true
styles:
  card:
    - width: 680px
    - height: 130px
    - margin: 0px
  name:
    - align-self: start
    - position: absolute
    - top: 10px
    - font-size: 16px
    - font-weight: bold
    - font-family: Sans-serif
  img_cell:
    - justify-content: start
    - align-items: start
    - padding-left: 619px
    - padding-top: 1px
    - margin: 0px
  state:
    - justify-self: start
    - font-size: 16px
    - font-family: Sans-serif
    - font-color: rgba(255, 0, 0, 0.8)
    - text-transform: capitalize
    - padding-left: 331px
    - margin-bottom: '-25px'
  icon:
    - width: 5%
state:
  - value: 0
    color: rgba(103,251,135,0.8)
  - value: 1
    color: rgba(192,243,106,0.9)
  - value: 2
    color: rgba(255,191,61,0.7)
  - value: 3
    color: rgba(255,129,44,0.7)
  - value: 4
    color: rgba(255,122,42,0.8)
  - value: 5
    color: rgba(255,101,37,0.8)
  - value: 6
    color: rgba(252,99,79,0.9)
  - value: 7
    color: rgba(254,59,59,0.8)
  - value: 8
    color: rgba(254,44,44,0.8)
  - value: 9
    color: rgba(253,12,69,0.8)
  - value: 10
    color: rgba(253,12,93,1.0)

I tried your example with air_quality_today, but it does change nothing for me…