Collecting attributes data from entity

I want to display charging status information about my devices in a view.

I use this code to get battery data, but I am not sure how to do the same and only get charging status.

sensors:
  iphone_battery:
    friendly_name: iPhone X
    unit_of_measurement: "%"
    value_template: '{{ states.device_tracker.iphonex.attributes.battery|int }}'
    device_class: battery

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.

1 Like

Thank you so much, very helpful information :smile:

Is there a way to change the icon on the new sensor? And also change the text “charging” and “not charging” to something else.

…and yes I know about the typo in “Ihpone X lading” :wink:

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 %}

You can choose your icon from this site:
http://materialdesignicons.com

When done, restart HA and the new sensor you will have to use will be macbookbat_template, or whatever you named it.

1 Like

Thank you!! :slight_smile:

1 Like

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/

You have no endif statement under here

2 Likes

You need to close the template with

{% endif %}
1 Like

Thank you @argykaraz and @keithh666.

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.

Here is the code I use:

  - platform: template
    sensors:
      iphone_battery:
        friendly_name: iPhone X
        unit_of_measurement: "%"
        value_template: '{{ states.device_tracker.iphonex.attributes.battery|int }}'
        device_class: battery

  - platform: template
    sensors:
      ipnone_ladestatus: 
        friendly_name: "iPhone X status"
        value_template: >-
          {% if ('device_tracker.iphonex.attributes.battery_status', 'Charging') %}
            Lader
          {% else %}
            Lader ikke  
          {% endif %}
        icon_template: >-
          {% if ('device_tracker.iphonex.attributes.battery_status', 'Charging') %}
            mdi:power-plug
          {% else %}
            mdi:power-plug-off
          {% endif %}  


Ok, here is what I would like you to do:
Go to the template editor and type this:
{{states.device_tracker.iphonex.attributes.battery_status}}

and then show us the result on the right, when charging and when NOT charging.

1

2

Just tried @argykaraz.

This is what I got:
When not charging: “NotCharging”
When charging: “Charging”

OK, this may be my bad templating skills. I am no expert :smile:
Try this one please:

   - platform: template
    sensors:
      ipnone_ladestatus: 
        friendly_name: 'iPhone X status'
        value_template: >-
          {% if states.device_tracker.iphonex.attributes.battery_status == Charging %}
            Lader
          {% else %}
            Lader ikke  
          {% endif %}
        icon_template: >-
          {% if states.device_tracker.iphonex.attributes.battery_status == Charging %}
            mdi:power-plug
          {% else %}
            mdi:power-plug-off
          {% endif %}  

Thank you so much @argykaraz :smile: Well I am very new at this, so you are a expert in my eyes :wink:

New code did not solve my problem. Still says “Lader ikke” (NotCharging) when it is charging.

Code:

- platform: template
    sensors:
      ipnone_ladestatus: 
        friendly_name: 'iPhone X status'
        value_template: >-
          {% if states.device_tracker.iphonex.attributes.battery_status == Charging %}
            Lader
          {% else %}
            Lader ikke  
          {% endif %}
        icon_template: >-
          {% if states.device_tracker.iphonex.attributes.battery_status == Charging %}
            mdi:power-plug
          {% else %}
            mdi:power-plug-off
          {% endif %}

This one should work… Weird…
Let me ask this, though it is silly… But I have to ask…

You should be looking at sensor.ipnone_ladestatus right?

I am out of ideas now… maybe someone else figures what is wrong.

Yes, I checked sensor.ipnone_ladestatus. Strange, it looks right. Been searching for similar code and I can not find anything that is wrong.

this is a string it needs to be in single quotes

2 Likes

Thanks @keithh666! That solved the problem :slight_smile:

And thanks @argykaraz for all you help, much appreciated.

2 Likes

Did you manage to get this working? When my iPhone battery level is 100% AND plugged in (e.g. charging), the sensor says it’s NotCharging…

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.