ludeeus
(Ludeeus)
March 20, 2017, 7:40am
1
Hi,
I added this template to my battery sensor for my iPhone and iPad, and it works really well
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
Bob_NL
(Bob Visser)
March 21, 2017, 9:17pm
2
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
mcfrojd
(Mcfrojd)
March 21, 2017, 9:32pm
3
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…
ludeeus
(Ludeeus)
March 21, 2017, 9:35pm
4
I’m not sure, I think I have only seen it been used in an sensor template
You can try to add it as an FR
1 Like
ludeeus
(Ludeeus)
March 21, 2017, 9:37pm
6
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
hijinx
(James)
March 21, 2017, 11:11pm
7
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
1 Like
ludeeus
(Ludeeus)
March 22, 2017, 6:27am
8
I’m using the icloud platform now, tried Owntracks over MQTT earlier but it was too slow
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
mcfrojd
(Mcfrojd)
March 22, 2017, 8:33am
9
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
ludeeus
(Ludeeus)
March 22, 2017, 2:19pm
10
Nice
Is this MQTT?
Found out now that when battery is 100% i get “mdi:battery-outline” ?
I will look in to that later today
ludeeus
(Ludeeus)
March 22, 2017, 2:45pm
11
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
hijinx
(James)
March 22, 2017, 3:36pm
12
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
ludeeus
(Ludeeus)
March 22, 2017, 4:40pm
13
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.
hijinx
(James)
March 22, 2017, 4:47pm
14
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.
ludeeus
(Ludeeus)
March 22, 2017, 4:49pm
15
Aha, for charging, will check on my end
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.
mcfrojd
(Mcfrojd)
March 22, 2017, 11:16pm
16
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
ludeeus
(Ludeeus)
March 23, 2017, 7:15am
17
beautiful
For the outline part:
I testet your template in my env, and the icon got blank…
When I changed ‘15’ to 15 it worked
mcfrojd
(Mcfrojd)
March 23, 2017, 7:30pm
18
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
Bieniu
(Maciek)
April 2, 2017, 12:06pm
19
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?
metbril
(Robert 🇳🇱🇪🇺)
April 2, 2017, 4:12pm
20
Bieniu:
In HA 0.41.0 is 4 6 6 8.
Can confirm this for 0.41.0