How to best integrate the battery usage/power drain of your phone into Home Assistant's energy dashboard?

Aim

My “simple” aim is to integrate the power usage of my phone into Home Assistant’s energy dashboard. Aka, so you can see how much power it is charging.
All, without a power plug, which would obviously solve this, but come on… the smartphone has this data and could “just report” it to HA. So having a dedicated plug is really unnecessary and even more complicated, as you would always have to attach your phone to exactly this one plug, which removes the flexibility to attach it anywhere in your flat/house/to any plug.

Problems

  • First, the setup problem, which I’ve partially solved. (see below)
  • Then, obviously, a systemic problem: I obviously only want to measure the power drain, when it is attached to my home aka my Home Assistant “instance”.

Tries

I can then also select it in the energy dashboard:

However, this is only a power sensor (in W) apparently, not an energy sensor.

Questions

  • Could not the app also be made to also measure “energy” (in Wh)?
  • Or how can the dashboard best be setup?

Another obvious problem is you need to filter out negative values (power drain) from the sensor, because you only want to count charging (not discharging).

Okay, with some AI help, I found a solution (text with AI help too):

The Home Assistant companion app gives you a battery_power sensor in watts. Since the Energy dashboard wants energy, not power, the usual solution is to convert that sensor with an Integral helper.

If you want to count only charging while the phone is actually “home”, use a template sensor first:

template:
  - sensor:
      - name: "Phone power charging only while at home"
        unique_id: phone_charging_power
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {% set p = states('sensor.phone_battery_power') | float(0) %}
          {% if is_state('device_tracker.phone', 'home') and p > 0 %}
            {{ p }}
          {% else %}
            0
          {% endif %}

Then create an Integral helper from sensor.phone_charging_power:

  • method: left
  • unit prefix: k
  • unit time: h

That gives you a kWh sensor you can add to the Energy dashboard.

If you want a simpler version without the “home” filter, just do:

template:
  - sensor:
      - name: "Phone power charging only"
        unique_id: phone_charging_power
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {{ min(0, states('sensor.phone_battery_power') | float(0)) }}

(Note by myself: You currently need to create that in the GUI, but you can map the YAML values accordingly.)

Pipeline:

battery_power (W) -> template filter -> Integral helper -> energy (kWh) -> Energy dashboard

That’s the cleanest way to do it without a smart plug.


As you can see, the AI suggested the “left integral method” because:

For battery-like or mostly stable readings, left is usually the right method.

I slightly doubt that, it refers to the HA docs, likely the part:

extremely accurate at estimating rectangular functions which are very stable for long periods of time and change very rapidly (e.g. such as the power function of a resistive load can jump instantly to a given value and stay at the same value for hours). If your source keeps its state for long periods of time, this method is preferable to the trapezoidal.

I am unsure if that is the case for a phone charging. Maybe depends on your usage? :sweat_smile:

Also my notes:

  • Read the info box for battery_power sensor in the Home Assistant Companion app guide. Some phone models may provide the battery power values wrongly (e.g. a negative value for charging or the wrong unit). You can correct that in the app.
  • It’s useful to use the “available when” advanced setting in the custom templates and then e.g. add there is_state('device_tracker.phone', 'home'), so you can see the power value as “unavailable” when

The companion app also exposes a sensor.your_mobile_battery_state with the following states: charging, discharging, full, not_charging. You could probably add the charging & full states in the template so it only reports the values when charging.

Yeah, that’s surely a working alternative. Based on my experience the watt number however anyway is positive for charging and negative for battery drain aka not_charging, so well… I guess it correlates anyway.

Ah or yeah, you mean maybe for the availability template part in the template sensors? Sure, like that it may be done:

{{ is_state('device_tracker.phone', 'home') and is_state('sensor.phone_battery_state', 'charging') }}

respectively if only for charging:

{{ is_state('sensor.phone_battery_state', 'charging') }}

Not to be “nitpicky” …but i am sometimes :joy:

Your Chargers actually draw about 10-15% more Amps than it Charge the battery with ( heat-loss & conversion ) , i would probably add that “assumption” to the equation
in the template
That is if it’s not only your Battery-health/usage you are after

( Thou then why have it in the Energy Dashboards :thinking: )

Yeah that is a good point. Also actually the usage og the phone, respectively using the phone while charging of course increases the amount somewhat. As I was made aware in the PowerCalc discussion.

In any case, the PowerCalc sounded quite interested in the feature, so maybe it will one day be added to PowerCalc.