Combining Energy Sensors

Hi All,

I’ve created a template energy sensor to display on the energy dashboard which takes the values of 4 related sensors, and combines them into one. My issue is that the page is reporting way above what the individual sensors are.

The total reported by each of the sensors adds up to 8.415 kWh (which is the total, not todays values) but the energy dashboard shows 21.06kWh for the same sensor. I thought it would reset each night.

Can anyone shed light on what may be happening.

The template sensor is shown below, and I believe is configured correctly.

  - sensor:
    - name: Greenhouse Total Consumption
      unique_id: greenhouse_total_consumption
      device_class: 'energy'
      state_class: 'total_increasing'
      unit_of_measurement: 'kWh'
      state: >
        {% set bucket_pump = states('sensor.tasmota_greenhouse_bucket_pump_energy_total') | float(2) %}
        {% set nft_pump = states('sensor.tasmota_greenhouse_nft_pump_energy_total') | float(2) %}
        {% set exhaust = states('sensor.tasmota_greenhouse_exhaust_energy_total') | float(2) %}
        {% set cooler = states('sensor.tasmota_greenhouse_cooler_energy_total') | float(2) %}
        {{ (bucket_pump + nft_pump + exhaust + cooler) }}

Cheers,
Paul

Only utility meters reset.
With the energy dashboard you don’t need a reset, as it aggregates the data according to the selected range.

Also, shouldn’t the state class be “measurement”?
Are you sure you look at the correct sensor?

Maybe show the attributes of the underlying sensors as well?

Btw, float (2) means default of 2, so if the state cannot be parsed, it will default to 2. Is that intended?

Hi,

No, it wasn’t intentional and has been sorted :-), it was a copy/paste without review.

I’ve heard various ideas on what the state_class and device_class should be and my understanding was as I had them coded.

As for the individual sensors, they have the following attributes and work fine individually.

state_class: total_increasing
unit_of_measurement: kWh
device_class: energy
friendly_name: tasmota_greenhouse_bucket_pump ENERGY Total

And to top it all off, I woke this morning to find that the fridge had actually been generating energy overnight :slight_smile: and the greenhouse sensor (combined) actually aligns with the 4 that I mentioned originally.

1 Like

Did you ever find the problem? I have a similar issue where I combine the energy of 2 phases to see the total and the template sensor is significantly higher than the sum

No, I threw everything into the too-hard basket for a while. Having returned to it the other day, I did notice figures were above what they should be. I deleted the sensor from the view and re-added it and so far appears to be fine.

I don’t really understand as the sensor itself seems to set correctly as per the below config. It would be nice if someone could give it a once over.

- sensor:
    - name: Greenhouse Total Consumption
      unique_id: greenhouse_total_consumption
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: kWh
      state: >
        {% set bucket_pump = states('sensor.tasmota_greenhouse_bucket_pump_energy_total') | float(0) %}
        {% set nft_pump = states('sensor.tasmota_greenhouse_nft_pump_energy_total') | float(0) %}
        {% set exhaust = states('sensor.tasmota_greenhouse_exhaust_energy_total') | float(0) %}
        {% set cooler = states('sensor.tasmota_greenhouse_cooler_energy_total') | float(0) %}
        {{ ((bucket_pump + nft_pump + exhaust + cooler)) }}
      availability: >
        {{ bucket_pump != none and nft_pump != none and exhaust != none and cooler != none }}

So, I found the root cause of the problem (not yet the solution as I’m trying couple of options:) ).

To simplify the explanation, let’s assume you have a sensor named sensortotal which is the sum of two other sensors (sensor1 + sensor2).
Because there are no triggers defined for sensortotal, every change on either sensor1 or sensor2 will trigger the sensortotal to be calculated.

At 12 AM every day, both sensor1 and sensor2 will be reset to zero. But that is not an atomic operation and instead one of the sensors is zeroed before the other.
As soon as one of the sensors is zeroed (i.e. sensor1), the calculation for sensortotal is triggered. And the calculation will be sensor1=0 + sensor2=xx(old value) at 12AM.
So, you have one value at 12:00AM (next day) for the combined sensor.
And because it’s auto-increment, you start the day with some value that breaks the daily calculations.

Yes! The following template code solved the problem.:
Basically if it’s 12:00:00 AM, the sensor will measure 0 instead of one of the two sensors.

- sensor:
    - name: circuit_0911_daily_energy
      unit_of_measurement: Wh
      state_class: total_increasing
      device_class: energy
      state: >
        {% if now().hour == 0 and now().minute == 0 and now().second == 0 -%}
          0
        {%- else -%}
          {{ (states('sensor.circuit_09_daily_energy')|float +
              states('sensor.circuit_11_daily_energy')|float)|int}}
        {%- endif %}

Is this the way to combine energy sensors (kWh)? Or are there other ways to do this? The reset at 12:00:00 is still necessary?