Energy Usage showing double the value it actually is

Not sure what the issue is but I have recently noticed an error in which my Energy graph (energy Dashboard) is showing double the value on the selected field.

Stat:

Chart:

It only seems to be recent but im perplexed as to how it has two different figures showing for the same thing?

The template configured is:

  - sensor:
      - name: House Use
        state: "{{ (states('sensor.house')|float (0)- states('sensor.house_total_load')|float (0) )}}"
        unit_of_measurement: kWh
        device_class: energy
        
      - name: House Total Load
        unit_of_measurement: kWh
        device_class: energy
        state: >
          {{ [ states('sensor.air_con'), 
            states('sensor.3d_printer_2'),
            states('sensor.it_equipment_plug'),
            states('sensor.lounge_sonos_2'),
            states('sensor.lounge_television'),
            states('sensor.master_tv'),
            states('sensor.ps5'),
            states('sensor.office_computers'),
            states('sensor.myenergi_zappi_energy_used_today')]
            | map('float',0) | sum }}

I have this error in my logs but im not sure why it has started has nothing has changed beyond the car charger finally getting used.

Logger: homeassistant.components.sensor.recorder
Source: components/sensor/recorder.py:288
Integration: Sensor (documentation, issues)
First occurred: 20:35:10 (1 occurrences)
Last logged: 20:35:10

Entity sensor.house_use from integration template has state class total_increasing, but its state is not strictly increasing. Triggered by state 10.4 (10.41) with last_updated set to 2023-12-07T20:31:50.705621+00:00. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+template%22

This happens because if one of your sensors becomes unavailable it will be replaced with the default from the float filter, zero. This will reduce your total. Then when the sensor becomes available again the total will increase by the the sensor’s value. Messing up your energy dashboard. Fortunately there is an easy fix. An availability template that marks your total sensors as unavailable if any of the source sensors are unavailable. Going from some value → unavailable → some value does not affect the energy dashboard.

- sensor:
      - name: House Use
        state: "{{ states('sensor.house')|float(0) - states('sensor.house_total_load')|float(0) }}"
        availability: "{{ has_value('sensor.house') and has_value('sensor.house_total_load') }}"
        unit_of_measurement: kWh
        device_class: energy
        
      - name: House Total Load
        unit_of_measurement: kWh
        device_class: energy
        state: >
          {{ [ states('sensor.air_con'), 
            states('sensor.3d_printer_2'),
            states('sensor.it_equipment_plug'),
            states('sensor.lounge_sonos_2'),
            states('sensor.lounge_television'),
            states('sensor.master_tv'),
            states('sensor.ps5'),
            states('sensor.office_computers'),
            states('sensor.myenergi_zappi_energy_used_today')]
            | map('float',0) | sum }}
        availability: >
          {{ has_value('sensor.air_con') and
            has_value('sensor.3d_printer_2') and
            has_value('sensor.it_equipment_plug') and
            has_value('sensor.lounge_sonos_2') and
            has_value('sensor.lounge_television') and
            has_value('sensor.master_tv') and
            has_value('sensor.ps5') and
            has_value('sensor.office_computers') and
            has_value('sensor.myenergi_zappi_energy_used_today') }}
1 Like

awesome thank you! I have applied this an hopefully it will sort itself out moving forward… Ill check tomorrow!

1 Like

I had the same issue caused by my sensor returning 0 once in the middle of the day.
History chart is not showing it by default but you would see it if you expanded the view, that’s how I found out.
I’m using Rest sensor where there is no “Availability”.
I might try to set it to text “Unavailable” in my Value Template in case when unexpected JSON is received.

EDIT: That was bad idea. Once sensor state became “Unavailable” instead of 0, as per my Value Template, it remained unavailable even after Rest API worked and returned correct values. I had to manually overwrite state to correct value to get it going. Probably something to do with state_class: total_increasing.

All state classes support the unavailable state, but you are correct in that the rest integration does not have an availability template option. Share your attempted config.

rest:
  - resource_template: https://api.geotogether.com/api/userapi/system/summaries/{{ states('sensor.geo_api_systemid') }}
    method: GET
    headers:
        Content-Type: application/json
        Accept: application/json
        Authorization: Bearer {{ state_attr('sensor.geo_api_access_token', 'accessToken') }}
    scan_interval: 4
    sensor:
      - name: "Geo Imported Today"
        unique_id: geo_api_rest_imported_today
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        value_template: >
            {% if 'IMPORT:IMPORT_T1' in value and 'IMPORT:IMPORT_T2' in value and 'IMPORT:IMPORT_T3' in value %}
            {{ (value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T1'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T2'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T3']) | round(2) }}
            {% elif 'IMPORT:IMPORT_T1' in value and 'IMPORT:IMPORT_T3' in value %}
            {{ (value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T1'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T3']) | round(2) }}
            {% elif 'IMPORT:IMPORT_T1' in value and 'IMPORT:IMPORT_T2' in value %}
            {{ (value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T1'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T2']) | round(2) }}
            {% elif 'IMPORT:IMPORT_T2' in value and 'IMPORT:IMPORT_T3' in value %}
            {{ (value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T2'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T3']) | round(2) }}
            {% else %}
            0
            {% endif %}

That 0 rarely happens, but when it does, it doubles on energy board. Setting it to Unavailable made it worse, so I’m back to 0.

Try this:

rest:
  - resource_template: https://api.geotogether.com/api/userapi/system/summaries/{{ states('sensor.geo_api_systemid') }}
    method: GET
    headers:
        Content-Type: application/json
        Accept: application/json
        Authorization: Bearer {{ state_attr('sensor.geo_api_access_token', 'accessToken') }}
    scan_interval: 4
    sensor:
      - name: "Geo Imported Today"
        unique_id: geo_api_rest_imported_today
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        value_template: >
            {% if 'IMPORT:IMPORT_T1' in value and 'IMPORT:IMPORT_T2' in value and 'IMPORT:IMPORT_T3' in value %}
              {{ (value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T1'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T2'] + value_json['values'][0].energyKwhTotals['IMPORT:IMPORT_T3']) | round(2) }}
            {% else %}
              {{ this.state }}
            {% endif %}

It should retain the last value instead of replacing it with 0.

You can’t use two of the three values. That too will cause the value to decrease and then massively increase when all three values becomes available again.

Great, if this retains the value, it should fix the problem.

This particular API sometimes skips some fields (one of the 3 energy tariffs) instead of returning them as 0 in case when that tariff did not have any usage on given day yet, so it is safe to always add them all up, I just had to account for fields sometimes missing, in which case I know they meant to be 0.
The issue is that sometimes I get nothing at all from that API, but your suggestion might work around it.
It might be because token is expiring sooner than expected, I will find out.
Thanks again.

No it really isn’t. Assume your three sensors are 10kW, 20kW and 30kW respectively.

You get all three:

10+20+30 = 60kW

You then only get the first two:

10+20 = 30kW, a reduction of 30kW.

You then get all three again:

10+20+30 = 60kW, an increase of 30kW over the last value, which is erroneously added to your energy dashboard as used energy.

Only update the total when you have all three sensors.

EDIT: I really should read all the post before replying,

If that is the case then you should actually be ok with only the two values. As long as once all three have values they are always returned (or none are returned, with the new template using this.state).

1 Like

So looking into this further after getting some time, I still seem to have an issue.

It looks like the house_use figure is including the Car Charger Energy Day figure within it, when it should be deducting it based on the sensor.house_total_load sensor.

My template file is this:

- sensor:
      - name: House Use
        state: "{{ states('sensor.house')|float(0) - states('sensor.house_total_load')|float(0) }}"
        availability: "{{ has_value('sensor.house') and has_value('sensor.house_total_load') }}"
        unit_of_measurement: kWh
        device_class: energy
        
      - name: House Total Load
        unit_of_measurement: kWh
        device_class: energy
        state: >
          {{ [ states('sensor.air_con'), 
            states('sensor.3d_printer_2'),
            states('sensor.it_equipment_plug'),
            states('sensor.lounge_sonos_2'),
            states('sensor.lounge_television'),
            states('sensor.master_tv'),
            states('sensor.ps5'),
            states('sensor.office_computers'),
            states('sensor.myenergi_zappi_energy_used_today')]
            | map('float',0) | sum }}
        availability: >
          {{ has_value('sensor.air_con') and
            has_value('sensor.3d_printer_2') and
            has_value('sensor.it_equipment_plug') and
            has_value('sensor.lounge_sonos_2') and
            has_value('sensor.lounge_television') and
            has_value('sensor.master_tv') and
            has_value('sensor.ps5') and
            has_value('sensor.office_computers') and
            has_value('sensor.myenergi_zappi_energy_used_today') }}

I would assume the fact the sensor sensor.myenergi_zappi_energy_used_today is incorporated as part of the House Total Load it should deduct it from the Home Use total, but it doesn’t seem to be?