Wrong value in energy dashboard after restart HA

Hello,
I have a problem with wrong value in energy dashboard after restart HA. Value is increased by 100%. It is probably due to sensor report zero value after restart. Data is read from wattrouter via modbus. All sensors have state_class total_increasing. I will be very grateful for advice on how to treat the sensor to show no zero value. There are several topics with similar problem but without clear solutions

#..Statistiky den..........................................................................................................
      - name: "wattrouter1 statistic"                             #statistiky za aktuální den
        address: 92
        input_type: input
        scan_interval: 10
        slave: 1
        data_type: int32
        slave_count: 2
        scale: 10
        unique_id: wattrouter1_statistic
- unique_id: wattrouter_grid_consumption_nt   #spotřeva energie ze sítě NT aktuální den
        name: "Wattrouter spotřeba ze sítě NT"
        unit_of_measurement: "kWh"
        state_class: total_increasing
        device_class: energy
        state: "{{ (states('sensor.wattrouter1_statistic_1')) | int(0) / 1000 | round(2, default=0) }}"

Thank you very much for help
Tomyc

I too come across this same issue from time to time. You can manually adjust the statistics values for specific entities.
Go to Developer Tools > Statistics, then search for your specific entity that is showing the incorrect value, then on the far right side of the row look for
image
Click that icon to open the ‘adjust a statistic’ dialog, then you can find the offending value for the specific date/time that you see in the energy dashboard.

Hi @Tomyc147, can you show history graph of your energy import sensor? Looks like it’s proly an issue w/ the sensor.

Do you also use a sensor w/o any periodical reset outside of HA?

Hi, thank you for your reply. I know this feature. I use it after each restart. But it is very annoying :-/

Hi David, I use more sensor from same device (wattrouter) via modbus. All sensors have same problem. Problem was appear itself several weeks ago. I guess after some update HA. Not sure. Sensors are reset by device at the midnight. I guess problem is due to sensors have wrong value before modbus connection is created. I have inserted delay for modbus communication but no success. I attached graph of two sensors in the range of 3 days. You can see several wrong values (drop to zero)


I guess problem is due to sensors have wrong value before modbus connection is created.

Yes. The problem is right here. Integration should set the state as unknown or unavailable instead of 0 before succesfull connection.

What integration are you using?

I am using default integration od HA for modbus

Hmm, I was looking through the code but it looks the native value is set initially to none (which means Unknown). I guess you’ll need to raise a github issue so someone more familiar with the integration code can take a look.

I guess when modbus start to communicate sensor has state undefined, unavailable etc. But I do not have this state treated and change it direct to zero by round function (default=0)

state: “{{ (states(‘sensor.wattrouter1_andi_7’)) | int(0) / 1000 | round(2, default=0) }}”

But it worked before without problem

Ah I see, so that showed sensor is not directly from the integration but yours.

So exactly that rounding w/ default to 0 is causing you the issue. :wink:

But it worked before without problem

You could have been just lucky but it doesn’t change the fact that your template is wrong and you shouldn’t have done it that way. Remove that default and you will be good.

It is funny because default settings I have add time ago due to warning in log :slight_smile: I will try to change settings and we will see

Ignore the warning (better to be warned than have incorrect values :wink:).

If you want to get rid of the warning define the sensor using configuration.yaml and add availability: check (e.g. Template - Home Assistant).

I have done some changes in code and it looks good. Problem disappeared.

Before:

- unique_id: wattrouter_grid_consumption_nt   #spotřeva energie ze sítě NT aktuální den
  name: "Wattrouter spotřeba ze sítě NT"
  unit_of_measurement: "kWh"
  state_class: total_increasing
  device_class: energy
  state: "{{ (states('sensor.wattrouter1_statistic_1')) | int(0) / 1000 | round(2, default=0)  }}"

after:

- unique_id: wattrouter_grid_consumption_nt   #spotřeva energie ze sítě NT aktuální den
  name: "Wattrouter spotřeba ze sítě NT"
  unit_of_measurement: "kWh"
  state_class: total_increasing
  device_class: energy
  state: "{{ (states('sensor.wattrouter1_statistic_1')) | int(0) / 1000 | round(2)  }}"
  availability: "{{ is_number(states('sensor.wattrouter1_statistic_1')) }}"

Also I found the problem with wrong value arise immediately before restart. Not after reboot. Probably modbus integration is shutdown earlier than rest of HA. At least I think so because problem disappeared only after second restart of HA.

Thank you very much for help

1 Like

You’re welcome, I’m glad we sorted it out. :wink: