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? 
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