Avoiding Spikes in Template Sensor During HA Reboots and Zigbee Device Reconnection

Hi everyone! :wave:
I’m new to Home Assistant templates but have read through several topics before creating this post. Despite my efforts, I’m facing some challenges with my setup and would greatly appreciate your guidance.

The Problem:

When Home Assistant reboots, I’m experiencing unwanted spikes in my template sensor output while Zigbee devices reconnect. My goal is twofold:

  1. Sensor Availability Handling:
    I want my template sensor to handle Zigbee device availability gracefully. Using the availability function and tying all sensors with has_value and AND conditions makes the entire sensor unavailable if even one Smart Plug sensor goes offline, which is not the behavior I want.
  2. Eliminating Reboot Spikes:
    Not using availability avoids the complete unavailability of the sensor but causes spikes when HA restarts. This affects the outcome of my sensor, “GPOs First Floor - Excluding Smart Plugs”, and results in the unwanted spikes shown in the second image.

My Goal:

I want to achieve a smooth, spike-free sensor output that remains stable regardless of the availability of individual Zigbee devices or HA restarts. Ideally, the output should look like the first image (steady and clean), not like the second image (spikey and erratic).

The Code:

Here’s the code I’m working with:

    # Data Information are coming from CT Zigbee Energy Monitor Clamps and Smart Plugs sensors
    # CT Zigbee Energy Monitor Clamps - https://www.aliexpress.com/item/1005004948381800.html
    # GPO stands for General Power Outlet or General Purpose Outlet

    - name: "GPOs First Floor - Excluding Smart Plugs"
      unique_id: 1c363f56-b964-40a7-8657-1da40c872cda
      unit_of_measurement: "kWh"
      state_class: total_increasing
      device_class: energy
      state: >
        {% set GPOs_First_Floor_Exc_Smart_plug = states ('sensor.ct_gpos_energy_a') | round(2) - (
                                                      states ('sensor.tv_sonos_appletv_plug_energy') | float(0) + 
                                                      states ('sensor.livingr_fridge_plug_energy') | float(0) + 
                                                      states ('sensor.bed1_allinone_poweroutlet_plug_energy') | float(0) +
                                                      states ('sensor.rba_laptop_monitors_plug_energy') | float(0) +
                                                      states ('sensor.server_homelab_plug_energy') | float(0) +
                                                      states ('sensor.bed2_leftbedside_plug_energy') | float(0) + 
                                                      states ('sensor.livingr_vacumerobotic_powerboard_plug_energy') | float(0) +
                                                      states ('sensor.kitchen_dishwasher_smart_plug_energy') | float(0) 
                                                      ) %}
          {{ GPOs_First_Floor_Exc_Smart_plug | float(0) | round(2) }}
      availability: "{{ has_value('sensor.ct_gpos_energy_a') 
                        }}"

Images:

1st image:


2nd image:


If anyone has experience handling similar situations or can offer suggestions to improve my code, I’d be extremely grateful! Let me know how I can achieve the desired behavior without the spikes and while keeping the sensor reliable.

Thanks in advance for your help! :blush:

I tried using ChatGPT to look into the issue from a different perspective; however, the suggested code still doesn’t provide the expected outcome.

# Data Information is coming from CT and Smart Plugs sensors

- name: "GPOs First Floor - Excluding Smart Plugs"
  unique_id: 1c363f56-b964-40a7-8657-1da40c872cda
  unit_of_measurement: "kWh"
  state_class: total_increasing
  device_class: energy
  state: >
    {% set ct_gpos = states('sensor.ct_gpos_energy_a') | float(0) %}
    {% set plugs = [
      'sensor.tv_sonos_appletv_plug_energy',
      'sensor.livingr_fridge_plug_energy',
      'sensor.bed1_allinone_poweroutlet_plug_energy',
      'sensor.rba_laptop_monitors_plug_energy',
      'sensor.server_homelab_plug_energy',
      'sensor.bed2_leftbedside_plug_energy',
      'sensor.livingr_vacumerobotic_powerboard_plug_energy',
      'sensor.kitchen_dishwasher_smart_plug_energy'
    ] %}
    {% set plugs_total = plugs
      | map('states', 0)
      | map('float', 0)
      | sum %}

    {# Check for missing or unavailable values #}
    {% if states('sensor.ct_gpos_energy_a') in ['unknown', 'unavailable'] or
          plugs | select('in', ['unknown', 'unavailable']) | list | length > 0 %}
      unavailable
    {% else %}
      {{ (ct_gpos - plugs_total) | round(2) }}
    {% endif %}
  availability: >
    {{ states('sensor.ct_gpos_energy_a') not in ['unknown', 'unavailable'] }}