Energy Consumption template sensor causes issues in Energy Dashboard when device becomes unavailable

Hello,

I have some IKEA INSPELNING smart plugs to measure energy usage. Unfortunately my energy meter is too far away for me to measure that, so I have to use my smart plugs and reading measurements directly from my energy meter is not an option.

To include them properly inside my energy dashboard, I created the following template sensor with some help from some older posts:

- sensor:
    - name: "Haushalt Stromverbrauch"
      unique_id: "home_energy_usage"
      unit_of_measurement: "kWh"
      state_class: "total_increasing"
      device_class: "energy"
      state: >
        {{ states.sensor 
            | selectattr('attributes.device_type', 'equalto', 'Electric Metering')
            | map(attribute='state')
            | select('is_number')
            | map('float')
            | reject('==', 0.0)
            | sum }}

This works fine as long as all energy meters properly report a value. Though if a device becomes unavailable, and for some reason they all become unavailable at the same time at night for 3 seconds, the sensor reports 0. This messes with my energy dashboard, as it creates those spikes:

Obviously this is not correct, but I figured that this happens due to the way the energy dashboard works: By using the sensor difference from the last measurement. In this case, it jumps from 0 (because all devices are unavailable) to ~250kWh.

Somewhere in the forum I read, that the energy dashboard will not use the measurement if it reports as unavailable. So I figured that I should make the accumulation template sensor unavailable when at least one device doesn’t report a proper value.

This is where I stuck now. I have literally no idea on how I should do that. Is this even the right way to do that or is there another, maybe better way?

Any help is appreciated, thanks!

Let’s say you have 3 sensors of which the devicy_type is Electric Metering,:

- name: "Meter 1"
  state: 70
- name: "Meter 2"
  state: 10
- name: "Meter 3"
  state: 20

So that means the result of your template sensor is 100.

Case 1: An entity temporary becomes unavailable

Let’s say Meter 1 becomes unavailable for a short moment, this means the result of your template sensor is then 30. As that result is lower compared to the previous result, and the state_class of your template sensor is total_increasing that will cause a conflict. Home Assistant expects your sensor to always increase, but in this case it didn’t, it decreased. The way Home Assistant manages that is to assume the sensor had a reset, and started back at 0. As it now sees a new state of 30 it assumes after this reset there has already been a usage of 30 kWh. So it will add the 30 to the original state of 100 to bring it to a total of 130.

Case 2: You restart Home Assistant

After you restart Home Assistant, the template will most probably already be rendered before the integration for your meters is loaded. So the entities will all be unavailable. The way your template is built, that will result in a value of 0. After the integration is loaded, the value will be 100 again. At that moment the same interpretation of that change as described above will kick in, and the new value of 100 will be added to the previous value of 100 resulting in a value of 200.

How to prevent this

Essentially you want the template to only return a value when all those meters have a valid state. That can be achieved by use of an availability template. Doing that, you can also simplify your state template a bit, as you will already know all of them have a valid state
(BTW not sure why you reject 0 states in your template, it doesn’t really have any use if you use sum after it, as adding 0 to a value will do nothing.)

- sensor:
    - name: "Haushalt Stromverbrauch"
      unique_id: "home_energy_usage"
      unit_of_measurement: "kWh"
      state_class: "total_increasing"
      device_class: "energy"
      state: >
        {{ states.sensor 
            | selectattr('attributes.device_type', 'equalto', 'Electric Metering')
            | map(attribute='state')
            | map('float')
            | sum }}
      availability: >
        {% set sensors = states.sensor 
            | selectattr('attributes.device_type', 'equalto', 'Electric Metering')
            | map(attribute='entity_id')
            | list %}
        {{ sensors | count == sensors | select('has_value') | list | count }}

NOTE: The template above can still give issues with wrong values in the Energy Dashboard (or with Long Term Statistics in general) when you completely remove or replace a device marked as Energy Meter from Home Assistant. In that case the availability template will still render true but the result of the template will be lower as the previous value.
So in case one of your INSPELNING plugs breaks, and you replace it with a new one, that will cause a decrease, and therefor a spike in your Energy Dashboard.

Question?

Why don’t you just add all your Energy Meters as sources for the Energy Dashboard instead of a combined template sensor?

Thank you very much for your reply, @TheFes! I’m sorry that I couldn’t get back to you any sooner.

Case 1 is what I already discovered on my own. Case 2 was something I didn’t even think about, but it makes sense.

In the meantime, I also tried something like you suggested, though after reading your question, I figured I’ll probably be on the safe side with just adding all energy metering devices to my energy dashboard manually.

I wanted to use a dynamic template sensor so that all devices I have are automatically inside my energy dashboard without the need to add them manually. This isn’t a huge benefit though, as I don’t add or remove devices on a regular basis.