Energy sensor resets / "jumps"

First off all, let me reassure you that I have read countless of posts about this subject without finding an answer to my question. I have a template sensor (blue line) which continuously and apparently without reason resets / “jumps”. At first I thought it was just resetting to zero when unavailable, but no. The sensor usually return to previous value, but not always. The issue has a very unfortunate effect on a utility meter with a daily cycle (purple line). All the “negative” values of the utility meter has been manually applied.

The sensor (sensor.energy_yield_total) is a pretty simple sum of two other energy sensors.

    - name: "Energy Yield Total"
      unique_id: energy_yield_total
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      state: >-
        {{ states('sensor.energy_yield_1') | float(0) + 
        states('sensor.energy_yield_2') | float(0) }}

The input energy sensors are platform sensors calculating the energy production

  - platform: integration
    source: sensor.inverter_input_power_1
    name: "Energy Yield #1"
    unique_id: energy_yield_1
    round: 6
    unit_prefix: k
    unit_time: h
    method: left
  - platform: integration
    source: sensor.inverter_input_power_2
    name: "Energy Yield #2"
    unique_id: energy_yield_2
    round: 6
    unit_prefix: k
    unit_time: h
    method: left

… and these are getting their input from the power sensor provided in the Huawei Solar integration by wlcrs.

Last night I set the sensor up with an attribute to keep the last value if / when the sensor would reset to zero…

    - name: "Energy Yield Total"
      unique_id: energy_yield_total
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      state: >-
        {% if states('sensor.energy_yield_1') | float(0) +
        states('sensor.energy_yield_2') | float(0) > 0 %}
          {{ states('sensor.energy_yield_1') | float(0) +
          states('sensor.energy_yield_2') | float(0) }}
        {% else %}
          {{ state_attr('sensor.energy_yield_total', 'last') }}
        {% endif %}
      attributes:
        last: >-
          {% if states('sensor.energy_yield_total') | float > 0 %}
            {{ states('sensor.energy_yield_total') | float }}
          {% else %}
            {{ last }}
          {% endif %}

… but as you can see in the posted picture the sensor just reset to a value above zero (three times actually), so now I am lost. I am trying the a “availability” aproach, but would really appriciate some help.

    - name: "Energy Yield Total"
      unique_id: energy_yield_total
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      state: >-
        {{ states('sensor.energy_yield_1') | float(0) + 
        states('sensor.energy_yield_2') | float(0) }}
      availability: >-
        {{ states('sensor.energy_yield_1') | is_number or 
        states('sensor.energy_yield_2') | is_number }}

The availability template should solve your issues but both sensors need to be present (AND logic not OR). Also there is a better test now:

      availability: >
        {{ has_value('sensor.energy_yield_1') and 
           has_value('sensor.energy_yield_2') }}

has_value() will return false if the state is unknown or unavailable.

Thanks Tom. Is it possible to set it up so not both sensors have to be present? The case is that I can have one sensor (representing one inverter) with production while the other sensor (inverter) does not have production.

No.

If one of them becomes unavailable the value drops. When the missing sensor connects again the value rises. The utility meter will record that rise as actual energy produced.

So both sensors states need to be valid for your template to be available.

Hmm… Let me clarify the situation / case. I have solar panels in different directions and with different inverters (start up voltage and operating range is not the same). It is often the case that one has production and the other does not.

If I am not mistaken, the setup with the availability template would mean that the “total yield” would not be updated “on time”, and maybe not until the next day. I would often have situations where the yield from #1 and #2 would not be equal to the total yield. Is there really no other way in HA to go about this?

Do your sensors report 0 or unavailable at night?

And do your inverters not have energy sensors, only power?

I beleive the power sensors from the integration (sensor.inverter_input_power_1) return unavailable and the power sensor I have created (sensor.power_inverter_1_input) will return 0 as it is set to “float(0)”.

To my surprise I see that the integration actually do provide energy sensors for the inverter…

I have been working on a project to create energy sensors for all power flows in an install with battery - and honestly I do not recall that I had the energy sensors or why I have chosen not to use them. I’d like to share the project but not until I am sure it works.

I’ll have to get back to you with regards to if I can use the energy sensors… But would this make it possible to ad two energy sensors without the issue I am getting?

I beleive I have chose not to use the energy sensors provided by Huawei / the Huawei Solar integration, because they do in fact not deliver the true value. Notice the “strange” cut off at 11:15. These sensors take into account that you have a loss in your battery and therfore the does not display the “true” yield nor the actual loss you have using your battery.

I have solved this, and with the sensors I have created I get the true production values and I can monitor the actual loss in the battery + much more.

Below you se my two inverters. It is quite clear how the battery loss is effecting the “calculated yield” on inverter #1. Inverter #1 is my master inverter / the one that the battery is connected to. The “Daily Yield 1 (Energy)” (magenta) is my sensor. The “Inverter Daily yield #1” (dark blue/grey) is the sensor provided by Huawei / the integration.

PS: I’d be happy to share a link to my work, but as mentioned not publicly until I am sure it works without this type of quirks. There is a full README.

Why are you duplicating the power sensor states?

    - name: "Power Inverter #1 Input"
      unique_id: power_inverter_1_input
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >-
        {{ states('sensor.inverter_input_power_1') | float(0) }}
    - name: "Power Inverter #2 Input"
      unique_id: power_inverter_2_input
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >-
        {{ states('sensor.inverter_input_power_2') | float(0) }}

If it was to add a state and device class this is not necessary. You can use manual customisation: Customizing entities - Home Assistant

No need for duplicate sensors.

The only reason is to have all the power sensors with the same prefix (“power”). Just adds some order and transparency in the work following. This could be omitted.

You can edit the entity_id from the frontend. Click on the entity, in the pop-up window click on the cog icon. You can set the entity id to what you want there.

OK but I guess that won’t solve the issue with the total energy meter?

Did my answers provide any hope for a solution to my issue - or did the real issue get lost because of my naming convention :slight_smile: ?

You should simplify your setup. Remove unnecessary intermediate steps and see if that helps.

I am using the sensors from the Huawei Solar integration (sensor.inverter_input_power_1 and _2) directly as input to the energy sensors yield # 1 and #2 which are the sensors added in yield total. I don’t think it can be optimized. Sorry for the wrong description above and not being precise about the usage of the two other power sensors. They are irrelevant in any calculations.

  - platform: integration
    source: sensor.inverter_input_power_1
    name: "Energy Yield #1"
    unique_id: energy_yield_1
    round: 6
    unit_prefix: k
    unit_time: h
    method: left
  - platform: integration
    source: sensor.inverter_input_power_2
    name: "Energy Yield #2"
    unique_id: energy_yield_2
    round: 6
    unit_prefix: k
    unit_time: h
    method: left

PS: I’ll edit the first post above, not to confuse others.

Isn’t it a bug that a sensor which has the state_class: total_increasing makes negative jumps? It must be possible to set the sensor only to increase? But then again that might not help if the sensor tries to “jump back”.

The state class is not causing the negative jumps your Rube Goldberg setup is.

The state class just tells things like the energy dashboard how to treat the sensor.

Increasing value → accumulate sum

Decreasing value → the sensor has reset.

All my sensors are simple and so is the one in question. As mentioned in the 3rd post above this the input is directly from the Huawei Solar integration, this input is converted to energy with the platform:integration function in HA and all I am trying to do is to get the sum of two of theses sensors. I hope we can agree that there is no Rube Goldberg about creating a sum of two energy sensors? As said - the rest of my sensors are just as simple. As I have also pointed out the two duplicating power sensor you got caught up in, has no relevance - I don’t use them - I have deleted them yesterday but the issue persists.

I would apricate if you gave this a chance with the above in mind. Thanks.

Your jumps are caused by your template sensor. Tom explained what the cause is and how to fix it. The fix is an availability template on your template sensor that causes the sensor to be unavailable if either one is not available.

This is your only option. Any other option will cause spikes. So you have to ask yourself: Do I want to remove the spikes or do I want to have a 100% persistent uptime sensor. You can’t choose both, you can only choose 1.