There you go:
{{ states.device_tracker.iphonex.attributes.battery_status }}
To understand it better, go to the left pane of HA and click on the templates icon under the “developer tools”.
In there you can try different templates and see the result real-time.
Now, if you just put this in there: {{ states.device_tracker.iphonex.attributes }} you will see ALL the attributes for that sensor.
Sure. You will need to create a template sensor:
Open your configuration.yaml and add this under the sensor section:
sensor:
- platform: template
sensors:
macbookbatt_template: // you can change this to whatever you like
friendly_name: "Macbook Pro lading"
value_template: >-
{% if ('device_tracker.iphonex.attributes.battery_status', 'charging') %}
Charging // change this to whatever you want to say when charging
{% else %}
Not Charging // change this to whatever when NOT charging
{% endif %}
icon_template: >-
{% if ('device_tracker.iphonex.attributes.battery_status', 'charging') %}
mdi:power-plug // change this for when CHARGING
{% else %}
mdi:power-plug-off // change this for when NOT charging
{% endif %}
I did a test now, got an error. Can you see what I did wrong?
- platform: template
sensors:
macbookbatt_template:
friendly_name: "Macbook Pro lading2"
value_template: >-
{% if ('device_tracker.iphonex.attributes.battery_status', 'charging') %}
Lader
{% else %}
Lader ikke
icon_template: >-
{% if ('device_tracker.iphonex.attributes.battery_status', 'charging') %}
mdi:power-plug
{% else %}
mdi:power-plug-off
{% endif %}
Error log:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endif'. The innermost block that needs to be closed is 'if'.) for dictionary value @ data['sensors']['macbookbatt_template']['value_template']. Got "{% if ('device_tracker.iphonex.attributes.battery_status', 'charging') %}\n Lader \n{% else %}\n Lader ikke ". (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Added the code, but it does still not work for me. No errors, but it does not give the correct output, it says “lader” (charging) when it is not charging.
Yes it’s working fine now. I have the same thing, when I have devices that are charged 100% but still connected, it will say “NotCharging” (Frakoblet). Not sure how to fix this.