Question About Energy Usage in HA

I’m just getting things configured in HA to track energy usage by getting all of my devices linked to power consumption. To that end, instead of having every device in the “Monitor Individual Devices” and in the Energy usage, I simply wrote a script that goes through all sensors classified as “Energy” and add them together, which should serve the same purpose since all of the devices included are “Running Totals”.

The issue, and I’ve been seeing this somewhat regularly, is these really bizarre and massive spikes for no reason. In this picture you can see a huge spike of nearly 4kWh between 1am and 2am when most things in the house that report energy are turned off.

My question is if there is what I might be able to do to track this down. As I stated above, every device is being totaled for the grid portion, but those same devices are also in the “Monitor Individual Devices”, so I see this big spike in the “grid” but none of the individual devices show this energy usage.

Screen Shot 2021-12-01 at 7.20.46 AM

I don’t think it’s a problem with my script that totals everything for the “grid” but since I figure that might be assumed, here it is:

total_grid_consumed_kwh:
  device_class: energy
  friendly_name: Total Grid Consumed kWh
  unit_of_measurement: kWh
  value_template: >-
    {% set ns = namespace(states="", val = 0.0) %}

    {% for s in states.sensor | selectattr('attributes.device_class', 'equalto', 'energy') | list %}
      {% if int(s.state, 0) > -1 %}
          {% if not 'total grid consumed' in s.name.lower() %}
            {% if not 'current in use kilowatts' in s.name.lower() %}
              {% set ns.val = ns.val + float(s.state, 0)  %}
            {% endif %}
        {% endif %}
      {% endif %}

    {% endfor %}

    {{ ns.val | round(2) }}

Another thing to note is that I also force my Z-Wave energy devices to update more frequently since I know that ZwaveJS won’t request it, so every device’s energy sensor should be accurate to within 5 minutes.