Binary Sensor for device charging, why is not working?

I have the following binary_sensor defined:

- platform: template
  sensors:
    iphone_charge_state:
      device_class: battery_charging
      friendly_name: iPhone Charge State
      value_template: >
        {{ state_attr('sensor.iphone_battery_state', 'charging') }}

but it does not seem to be working and I am not sure why or what I am missing :thinking:

Developer Tools > States shows it as off:

However, if I go to devices and check the Device Info I am able to see the Battery State (sensor.iphone_battery_state) charging:

Developer Tools > Templates displays no icons for the following code:

{% if is_state('sensor.iphone_battery_state', 'charging') %}
mdi:lightning-bolt
{% elif states('sensor.iphone_battery_level') | int < 10 %}
mdi:exclamation-thick
{% endif %}

and last Developer Tools > States shows the state as Charging:

At this point, I am completely lost and not sure what I am missing or what I miss understood.

Can I get some help?

Check the history of the iphone_battery_state sensor. Since your charge % was 97%, it may have been “full” not “charging”

For your icon:

When structuring if/then you need to look out for cases where none of your if’s are true and assign an else.

{% if is_state('sensor.iphone_battery_state', 'charging') %}
mdi:lightning-bolt
{% elif states('sensor.iphone_battery_level') | int < 10 %}
mdi:exclamation-thick
{% else %}
mdi:battery
{% endif %}
1 Like

This:

      value_template: >
        {{ is_state_attr('sensor.iphone_battery_state', 'charging') }}

Not this:

1 Like

For some reason this does not work for me:

The only way it works is as follow:

      value_template: >
        {{ is_state('sensor.iphone_battery_state', 'Charging') }}

Not sure where is the trick

Because “Charging” is the state not an attribute, look at the column headings in the image you posted:

image

And I was half asleep when I wrote my reply or I would have picked that up, and that is_state_attr requires three variables.

1 Like

First of all make sure that you have the right case of the letters.
charging is not always equal to Charging.

Secondly be aware that nearly all modern mobile phone use smart charging where the battery will be charged to 100% and then the charger will be disconnected programmatically and not be activated again until the battery level have dropped some %, like to 97% or 93%. This is to prevent the battery from being topped up on the last % where the most damage to the battery life occur.
The most modern mobile phones even use AI learning to predict your usage pattern and then might wait even longer to make the charger to 100% hit just before the time where you would normally disconnect the phone.

These two features can make a mess of many sensors and automations. Sometimes you can be lucky to have a “charger connected” sensor, but other times you are just forced to make some assumptions.

They are using Developer Tools → States to check, which does not translate states. So what is shown there is correct. It’s the only place you can be sure the state is correct.

1 Like

Still there is a difference in one of the examples and the developer tools, and the text indicating confusion. :slight_smile:

does this mean the state I am seeing there is what I should use in NodeRED? :thinking:

Yes and make sure that cases are correct too.

1 Like