Calculate power consumption for charging Tesla

I use Norwegian company Tibber as my electricity provider, which gives me realtime information about power usage, but also the current electricity price.

I want to make a template sensor that shows me the following:

  • Current charging expenses while my Tesla is charging
  • Amount of electricity used during current current charge
  • Accumulated charging expenses for all charges
  • Accumulated electricity used for all charges

I want to have a dashboard that shows me the price of last charge as well as amount of electricity (kWh) that was consumed.

The counters for charge price and consumed electricity should reset each time the car charges. (There is a switch for this).

How do I go about to make these template sensors?

Hi @ronnyandre,

did you solve this? I have the same setup.

Thanks,
Peter

Hi @peter.alvmo

I didn’t manage to solve it in Home Assistant directly, as I discovered a system called TeslaMate.

TeslaMate runs as a docker container, connected to a PostgreSQL database and Grafana to show the statistics. It also has MQTT client implemented, so it can send MQTT messages to Home Assistant, letting me show the data in Home Assistant.

I’ve put together something similar. I want to track how much I’m paying for charging at home. I use Tesla Custom Integration and the Energy tracker in the HASS. The kwh data comes from …

  1. Get the charger rate in W.
  2. Convert to kWh.
  3. Use the HASS energy tracker, where you can reference a cost
sensor:
  - platform: template
    sensors:
      tesla_charging_power_home:
        device_class: power
        friendly_name: Tesla Charging Power at Home
        unit_of_measurement: 'W'
        value_template: >
          {% if is_state('device_tracker.<car_name>_location_tracker', 'home') and is_state('binary_sensor.<car_name>_ischarging', 'on') %}
          {{ state_attr('sensor.<car_name>_charging_rate_sensor', 'charger_power') |float *1000}}
          {% else %} 
          0
          {% endif %}
    #Tesla Charging Energy kWh (added at Home)
  - platform: integration
    source: sensor.tesla_charging_power_home
    name: tesla_charging_home_kWh
    unit_prefix: k
    unit_time: h
    method: left
    round: 2

4 Likes

Shouldn’t “binary_sensor.<car_name>_ischarging” be “binary_sensor.<car_name>_charging”?

1 Like

You’re correct, if you’re using the binary_sensor entity directly from the integration.

I found that it becomes true even, if car is just plugged in - no power or scheduled to charge later, etc. Doesn’t affect it. So I templated the “ischarging” binary_sensor, reading the “charging” state from the attripute of the charger…

template:
    - binary_sensor:
        - name: "<car_name> isCharging"
          device_class: battery_charging
          unique_id: tesla_ischarging
          state: >
              {%if state_attr('binary_sensor.<car_name>_charger', 'charging_state') == 'Charging' and is_state('binary_sensor.<car_name>_online','on')%}
              True
              {% else %} 
              False 
              {% endif %}
1 Like

Hmm it still doesn’t work with me.

statistics_not_defined

  • sensor.tesla_charging_home_kwh
    Entity unavailable

The state of these configured entities are currently not available:

  • sensor.tesla_charging_home_kwh (unknown)

Check Developer Tools\Statistics for any issues

i think the issue is that the charging rate sensor : state_attr(‘sensor.<car_name>_charging_rate_sensor’, ‘charger_power’) do not have a attribute charger_power.’

I use ‘sensor.hamster_charger_power’ isstead. But not yet testet.