Estimation of electric car consumption (E.g. Renault Zoe)

Hello,
I have a Renault Zoe (electric car).
The garage where I park/charge the car doesn’t allow to receive “cells phone” : thus the car cannot communicate its state (in particular “in charge” or “not in charge”).
My idea to estimate the consumption is to consider my remaining battery state : if it increase (with a threshold) it means that I have charged the car. (by knowing the capacity of my car, I can estimate the charge I’ve made)
Any idea how to perform this computation ? Does any has create a sensor for this ? or to put together several integrations (I need something that give the previous state : after it’s straight forward with a sensor template)
Thanks in advance
Philippe

Here a solution based on SQL.
Please note, the idea is to compute the difference between current battery value and former one .
Then you can evaluate when you charge (the battery value increase) thanks to the capacity of the car (e.g. My Zoe is 41kWh)

Let’s assumed that sensor.battery_level is the current battery value (in %)
In configuration.yaml, you can declare :

  1. the current value not needed, just to understand
  - platform: sql
    db_url: !secret database_url
    queries:
      - name: last_battery_level
        query: "SELECT * FROM states WHERE entity_id = 'sensor.battery_level' ORDER BY state_id DESC LIMIT 1;"
        column: "state"
        unit_of_measurement: '%'

  1. the former value not needed, just to understand
  - platform: sql
    db_url: !secret database_url
    queries:
      - name: before_last_battery_level
        query: "SELECT * FROM (SELECT * FROM states WHERE entity_id = 'sensor.battery_level' ORDER BY state_id DESC LIMIT 2) two_entity  ORDER BY state_id ASC LIMIT 1;"
        column: "state"
        unit_of_measurement: '%'

  1. the difference between current and former battery value (NEEDED)
  - platform: sql
    db_url: !secret database_url
    queries:
      - name: diff_last_and_before_last_battery_level
        query: "SELECT SUM(state) AS query_result FROM
                (
                   (SELECT state FROM states WHERE entity_id = 'sensor.battery_level' ORDER BY state_id DESC LIMIT 1)
                   UNION ALL
                   (
                      SELECT (-1*state) FROM
                      (
                        SELECT * FROM states WHERE entity_id = 'sensor.battery_level' ORDER BY state_id DESC LIMIT 2
                      ) two_entity  ORDER BY state_id ASC LIMIT 1
                   )
                ) last_battery_levels
               ;"
        column: "query_result"
        unit_of_measurement: '%'

  1. Then based on battery capacity of your car (e.g. 41kWh) , you can evaluate the charge you perform :
  - platform: template
    sensors:
      charge_battery_level_sql:
        unit_of_measurement: 'kWh'
        value_template: >-
           {% if ((states('sensor.diff_last_and_before_last_battery_level') | float) < 0 ) %}
              0
           {% else %}
              {{ (states('sensor.diff_last_and_before_last_battery_level') | float) * 41 / 100  }}
           {% endif %}

Hope it can help

Hello,
In order to have a robust sensor, and a simplier way, I have written a custom_component :
https://github.com/jugla/battery_consumption

Hi,
unfortunately your solution GitHub - jugla/battery_consumption: Home Assistant Component to compute battery consumption is not working for me. I could create the folder battery_consumption in custom_components and added the code as described into configuration.yaml.
The error message I receive is that the new created sensor zoe_batterie_total_discharge has no unique ID. (…total_charge as well)
Maybe this is related to the new created sensor battery_level+name of sensor to monitor, because its not really clear how to combine it in the code.
Any idea, what could have happened? Thanks,
Holger

Hello,
Please can you cut and paste, the corresponding part of configuration.yaml.
Thanks