Weird energy statistics

Hi all!
Some days ago installed PZEM-004t current sensor on my electricity input and another power sensor(SONOFF POW R2) on electric water heater.
Then, configured energy dashboard with PZEM as “Grid consumption” and SONOFF POW R2 as " Individual devices".
The third thing that i’ve made was a template energy sensor also added as individual device. The sensor is configured to substract the value consumed by a boiler from the total consumed energy:

template:
  - sensor:
      - name: "Other consumption (Wh)"
        unit_of_measurement: 'Wh'
        device_class: energy
        state_class: total_increasing
        state:  >-
          {{ (states('sensor.mains_energy') | float) - (states('sensor.boiler_cutoff_energy_wh') | float) }}

The weird thing is the values that is displayed in energy dashboard:


(it is displayed over 90 kWh for “Other consumption (Wh)”, while the sensor value is around 35 kWh).
What is the problem with my custom sensor?

This might be due to communication failures of one or both of the underlying sensors.
If they are unavailable your template becomes unknown and this is interpreted as a reset.
To avoid this add the availability attribute to your template-sensor and check, if the underlying sensors actually report a number.

1 Like

Thank you! Truncating statistic table in database and adding an availability setting helped me!

1 Like

Could you please show how you added the availability setting?
Thank you

template:
  - trigger:
      - platform: state
        entity_id:
          - sensor.boiler_cutoff_total_daily_energy_kwh
          - sensor.mains_cutoff_total_daily_energy
      - platform: homeassistant
        event: start
    sensor:
      - name: "Other daily consumed energy (kWh)"
        unique_id: other_daily_consumed_energy_kWh
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: 'kWh'
        state:  >-
          {{ [states('sensor.other_daily_consumed_energy_kWh') | float(0), (states('sensor.mains_cutoff_total_daily_energy') | float(0)
            - states('sensor.boiler_cutoff_total_daily_energy_kwh') | float(0)) | round(2)] | max }}
        availability: >-
          {{ states('sensor.mains_cutoff_total_daily_energy') not in ['unknown', 'unavailable', 'none']
            and states('sensor.boiler_cutoff_total_daily_energy_kwh') not in ['unknown', 'unavailable', 'none'] }}
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'
1 Like

That helped for the first two days. On the third day the statistic gone crazy again…

You have three sensors in your state template, but only test for two in your availability template.

Also it iwould be easier to just test for number:

          {{ not false in
             [states('sensor.home_meter_grid_phase1_energy'),
              states('sensor.home_meter_grid_phase2_energy'),
              states('sensor.home_meter_grid_phase3_energy')]|map('is_number')
          }}
  

No, I have exactly two sensors: mains and boiler, by the first instruction I am getting the previous value of the template sensor itself and maxing with the computed value (to get rid of the situations when the computed value is lower than the previous one).
But that is not enough unfortunately(

Not sure what your sensors deliver and what your template does, but know, that if ever a calculated state value is smaller then the one before, this will be interpreted as a reset, which could account for “weird statistics”.

yes, and that is fixed with the following code

[
  # previous value
  states('sensor.other_daily_consumed_energy_kWh') | float(0),
  # compute current value
  (states('sensor.mains_cutoff_total_daily_energy') | float(0) - states('sensor.boiler_cutoff_total_daily_energy_kwh') | float(0)) | round(2) 
] | max # return the greater value: previous or computed

But even that is not enough

So I finally managed to overcome my problem!
The problem was caused by the first value in the given result set

Fixed this by returning zero value at the first second of midnight (21:00:00 on screenshot is local midnight here in Ukraine) with the following config

      - name: "Other daily consumed energy (kWh)"
        unique_id: other_daily_consumed_energy_kWh
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: 'kWh'
        state:  >-
          {% set time = now() %}
          {% if time.minute == 0 and time.hour == 0 and time.second == 0 %}
            {{ 0.00 | float() }}
          {% else %}
            {% set previousValue = states('sensor.other_daily_consumed_energy_kWh') | float(0) %}
            {% set currentValue = (states('sensor.mains_cutoff_total_daily_energy') | float(0)
              - states('sensor.boiler_cutoff_total_daily_energy_kwh') | float(0)) | round(2) %}
            {{ [previousValue, currentValue] | max }}
          {% endif %}
        availability: >-
          {{ states('sensor.mains_cutoff_total_daily_energy') not in ['unknown', 'unavailable', 'none']
            and states('sensor.boiler_cutoff_total_daily_energy_kwh') not in ['unknown', 'unavailable', 'none'] }}
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'