Hi everyone!
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:
- Sensor Availability Handling:
I want my template sensor to handle Zigbee device availability gracefully. Using theavailability
function and tying all sensors withhas_value
andAND
conditions makes the entire sensor unavailable if even one Smart Plug sensor goes offline, which is not the behavior I want. - Eliminating Reboot Spikes:
Not usingavailability
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!