alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
577 stars 99 forks source link

Adding total energy (kWh) sensor for solar related entities #330

Open luisiam opened 1 year ago

luisiam commented 1 year ago

Is it possible to add total energy consumption/production (kWh) sensors for all solar related entities? That will be great for implementing tesla solar into the energy dashboard. Even though it can be done with the existing Riemann sum integration, there are quite a lot of issues using that integration and it will throw off the statistics. It will be nice if this integration will provide the kWh reading directly.

purcell-lab commented 1 year ago

The relevant energy endpoints for implementation of this FR are located here: https://tesla-api.timdorr.com/energy-products/energy/history

nicholashead commented 1 year ago

Came here to post a new bug about this- but this issue is probably the best fit rather than opening a new one. I’ve tried multiple ways to get the kWh matching what the Tesla app shows (per day/month) without success. I’m not sure what I’m doing wrong or maybe it’s just not even possible?

This is what I currently have:

  - platform: integration
    source: sensor.my_home_solar_power
    name: tesla_solarpanels_new_kwh
    unit_prefix: k
    round: 2
luisiam commented 1 year ago

I have tried all three integral method and left seems to be matching the app the closest but it is still off. Hopefully this integration will provide the kWh sensor directly in the future.

nicholashead commented 1 year ago

I have not yet tried this manual kWh method, but was maybe gonna try it tonight: https://community.home-assistant.io/t/convert-w-to-kwh/327007

luisiam commented 1 year ago

I have not yet tried this manual kWh method, but was maybe gonna try it tonight: https://community.home-assistant.io/t/convert-w-to-kwh/327007

That's new to me. I will try and compare my current integral with left method.

nicholashead commented 1 year ago

I gave it a whirl but I must be doing something wrong. I tried to follow the advice on that thread (and since config settings have changed since then I guessed on some parameters)

Sensors I added:

In sensor.yaml:

  - platform: average  
    name: 'Tesla Solar Average Power'
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    entities: sensor.my_home_solar_power
  - platform: average
    name: 'Tesla Solar Five Minute Average Power'
    duration:
      minutes: 5
    entities: sensor.my_home_solar_power

And then in my main config:

template:
  - sensor:
      - name: Tesla New KWH Sensor
        state: "{{ '%0.2f'|format(states('sensor.tesla_solar_average_power')|float / 1000 * (now().hour + now().minute/60)) }} "
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing

But the "Tesla New KWH Sensor" is pretty high- even higher than my original sensor:

  - platform: integration
    source: sensor.my_home_solar_power
    name: tesla_solarpanels_new_kwh
    unit_prefix: k
    round: 2
luisiam commented 1 year ago

My setup as follows and it is very close to the integration sensor. Remember to always use method: left in the integration sensor.

This is my average power sensor setup

sensor:
  # Average Solar Power
  - platform: average
    name: Average Solar Power
    start: "{{ today_at('00:00') }}"
    end: "{{ now() }}"
    entities: sensor.my_home_solar_power

This is my template energy sensor setup

template:
  - sensor:
      # Daily Solar Energy
      - name: Daily Solar Energy
        device_class: energy
        state_class: total_increasing
        state: "{{ (float(states('sensor.average_solar_power'), default = 0.0) * ((now() - today_at('00:00')).total_seconds() / 3600) / 1000) | round(2) }}"
        unit_of_measurement: kWh

This is my original integration energy sensor

sensor:
  - platform: integration
    source: sensor.my_home_solar_power
    name: Solar Energy
    round: 4
    unit_prefix: k
    method: left

Both sensors yield 27.1kWh and the Tesla app shows 27.4kWh of solar production yesterday. However, I was having a perfect sunny day and that was the perfect scenario without any spike in the solar power. I will keep monitoring both sensors and pick the one getting me result closer to the Tesla app.

nicholashead commented 1 year ago

@luisiam interesting. I switched to using your “Daily Solar Energy” sensor but it’s still off for me. Tesla app says 2.8 kWh today, but energy HA says 3.6. This also matches my older integration sensor too. Fun. Maybe this won’t be solvable without the actual kWh data from teslas api?

luisiam commented 1 year ago

@luisiam interesting. I switched to using your “Daily Solar Energy” sensor but it’s still off for me. Tesla app says 2.8 kWh today, but energy HA says 3.6. This also matches my older integration sensor too. Fun. Maybe this won’t be solvable without the actual kWh data from teslas api?

Looks like it. According to the API document, seems like it is very doable. I will be more than happy to help with testing if someone can create the code for it, which will require update to the teslajsonpy library as well.

nicholashead commented 1 year ago

What's interesting is latest HA just dropped, and they have this note: Got an energy sensor in Wh, but you’d like it to be kWh? You can now change the units of energy sensors in the entity settings.

Looking at the sensor supplied by the Tesla integration, it does not show the unit of measurement for me on the entity card - I am also unsure what method they're using to convert between k/kWh - but maybe this new feature in HA could be of assistance for this problem?

luisiam commented 1 year ago

That's totally different. Both Wh and kWh are the unit for energy. Simply 1kWh = 1000Wh. It's like m (meter) vs km (kilometer). What we need is from W/kW to Wh/kWh, which requires the integration of power over time.

nicholashead commented 1 year ago

That's totally different. Both Wh and kWh are the unit for energy. Simply 1kWh = 1000Wh. It's like m (meter) vs km (kilometer). What we need is from W/kW to Wh/kWh, which requires the integration of power over time.

You're right. I missed that part. Sorry.