Template Sensors based on Utility Meters resetting at different times causing spikes in graph

I’m hoping I can explain this satisfactorily. Refer to this graph
image

The value of Electricity Total Cost Daily spikes from the value of $1.60 to $3.10 at midnight. I believe this is because the values that are used in calculating these values are resetting at slightly different times. Here are the template sensor configurations:

      - unique_id: "total_daily_import_cost"
        name: Total Daily Import Cost
        icon: mdi:currency-usd
        device_class: monetary
        state_class: total
        unit_of_measurement: $
        attributes:
          last_reset: >
            {{ state_attr("sensor.daily_energy_offpeak", "last_reset") }}
        state: >
            {% set supply = states('input_number.electricity_supply_charge') | float(0) / 100 %}
            {% set offpeak = states.sensor.daily_energy_offpeak.state | float(0) / 1000 * states.input_number.daily_energy_offpeak.state | float(0) / 100 %}
            {% set peak = states.sensor.daily_energy_peak.state | float(0) / 1000 * states.input_number.daily_energy_peak.state | float(0) / 100 %}
            {% if states('sensor.daily_energy_offpeak') | is_number and states('sensor.daily_energy_peak') | is_number %} 
            {% set demand = (states('input_number.electricity_demand_max') | float (0) / 1000) * 2 / 100 * (states('input_number.electricity_import_rate_high_demand') | float(0)) %}
              {{ (supply + offpeak + peak + demand) | round(2) }}
            {% endif %}
            
      - name: "Electricity Total Cost Daily"
        unique_id: electricity_total_cost_daily
        icon: mdi:currency-usd
        unit_of_measurement: $
        state_class: total
        device_class: monetary
        attributes:
          last_reset: >
            {{ state_attr("sensor.electricity_exported_energy_daily", "last_reset") }}
        state: >
          {% if states('sensor.electricity_exported_energy_daily') | is_number %}
            {% set feedintariff = (states('sensor.electricity_exported_energy_daily') | float(0) / 1000) * (states('sensor.solar_feed_in_tariff_aud_kwh') | float(0)) %}
            {{ ((states('sensor.total_daily_import_cost') | float(0)) - feedintariff) | round(2) }}            
          {% endif %}

So what I assume is happening is that at midnight sensor.electricity_exported_energy_daily (which is a utility meter on a daily cycle) is resetting to zero thus the value of sensor.electricity_total_cost_daily becomes the same as sensor.total_daily_import_cost for the previous day before that then resets and everythng returns to normal. Is there a way to ensure these various utility meters all reset at the same time? Or do I need to do something different in the template sensors to fix this?

Utility meters update when their source sensors update. Daily meters will reset pretty damn close to simultaneously.

You are using the new template sensor format so you could use time triggered template sensors to ensure everything is calculated when you want, say at 23:59:59. Missing one second of data isn’t going to introduce any appreciable error.

Ok let me look into that tomorrow as I haven’t used that before

Edit: but they will still need to update continuously throughout the day….

I’m only cautiously optimistic but setting the time trigger to every second has resulted in a good changeover at midnight but I have seen that on the odd occasion so will know after a couple of days. :crossed_fingers: