Utility meter readings jump unexpectedly and rise at a greater rate than expected

Hello. I'm new here. I've been using HA for a couple of months and have learnt lots. I'm trying to track the money I am saving from using solar energy, rather than purchasing from the grid. Everything is working pretty well, but I'm having a problem with the way my daily values are summing using utility meters. I have various template sensors, including one called Daily Solar Savings defined in template.yaml, which calculates the baseline cost of the energy generated and not exported:

- sensor:
    - name: "Daily Solar Savings"
      unique_id: daily_solar_savings
      unit_of_measurement: "£"
      state_class: total
      device_class: monetary
      state: >
        {% set std = state_attr('sensor.flux_import_rates', 'standard') | float(0) %}
        {% set solar = states('sensor.daily_solar_kwh') | float(0) %}
        {% set export = (
          states('sensor.daily_export_off_peak_kwh') | float(0) +
          states('sensor.daily_export_standard_kwh') | float(0) +
          states('sensor.daily_export_peak_kwh') | float(0)
        ) %}
        {% set self = [solar - export, 0] | max %}
        {{ (self * std) | round(2) }}

I then have 2 Utility Meters in configuration.yaml, created from the template sensor, to track daily and all-time accumulated savings:

utility_meter:
  daily_solar_savings:
    source: sensor.daily_solar_savings
    cycle: daily
    all_time_solar_savings:
    source: sensor.daily_solar_savings

The problem is that I am getting small glitches and down-ward movements in my daily savings template (some unexplained, some when I restart HA). These make minimal impact to my daily readings, but they causing significant jumps in my accumulative utilty meter readings.


I can't work out how to fix this. I've tried setting state_class to measurement, rather than total, but that didn't help. Any pointers would be really appreciated.

Add an availability template to your template sensor.

Thanks very much - I wasn't aware of this requirement. I've added the availability templates, and will monitor things over the next few days. Fingers crossed.