Hi everyone,
I’m facing a puzzling issue in my Home Assistant setup. I have two template sensors with nearly identical logic, but one works flawlessly while the other is always marked as “unavailable.”
To provide context, both sensors depend on a binary sensor (binary_sensor.delayed_ct_gpos_power_a_availability
) for their availability template. Below is the complete configuration for the binary sensor, along with the two template sensors.
Binary Sensor Code (Referenced in Both Template Sensors):
- binary_sensor:
- name: "Delayed CT GPOs Power A Availability"
unique_id: "8459716d-1ee0-43ef-9cf7-184c5871d6c2"
state: >
{% set gpos_state = states('sensor.ct_gpos_power_a') %}
{% set uptime = (now() - (states('sensor.uptime') | as_datetime(default=now()))).total_seconds() %}
{{ gpos_state not in ['unknown', 'unavailable'] and uptime > 60 }}
delay_on:
minutes: 1
Template Sensor Code A (Shows as “Unavailable”):
- sensor:
- name: "GPOs Power A - Excluding Smart Plugs NEW 3"
unique_id: aa8763f7-b818-45f6-a024-898631832f91
unit_of_measurement: "W"
device_class: power
state: >
{% set ct_gpos = states('sensor.ct_gpos_power_a') | float(0) %}
{% set plugs = [
'sensor.livingr_fridge_plug_power',
'sensor.livingr_vacumerobotic_powerboard_plug_power',
'sensor.kitchen_dishwasher_smart_plug_power'
] %}
{% set plugs_total = plugs | map('states') | select('string') | map('float', 0) | sum %}
{{ (ct_gpos - plugs_total) | round(2) }}
availability: >
{% set is_delayed = is_state('binary_sensor.delayed_ct_gpos_power_a_availability', 'on') %}
{% set is_valid = states('sensor.ct_gpos_power_a') not in ['unknown', 'unavailable'] %}
{% set uptime = states('sensor.uptime') | as_datetime %}
{% set current_time = now() %}
{% if uptime is not none %}
{{ is_delayed and is_valid and (current_time - uptime).total_seconds() > 60 }}
{% else %}
false
{% endif %}
Template Sensor Code B (Works Perfectly):
- sensor:
- name: "GPOs Power A - Excluding Smart Plugs NEW 2"
unique_id: bab2b166-c99c-4800-a421-d333ffa9c9ef
unit_of_measurement: "W"
device_class: power
state: >
{% set ct_gpos = states('sensor.ct_gpos_power_a') | float(0) %}
{% set plugs = [
'sensor.livingr_fridge_plug_power',
'sensor.livingr_vacumerobotic_powerboard_plug_power',
'sensor.kitchen_dishwasher_smart_plug_power'
] %}
{% set plugs_total = plugs | map('states') | select('string') | map('float', 0) | sum %}
{{ (ct_gpos - plugs_total) | round(2) }}
availability: >
{% set is_delayed = is_state('binary_sensor.delayed_ct_gpos_power_a_availability', 'on') %}
{% set is_valid = states('sensor.ct_gpos_power_a') not in ['unknown', 'unavailable'] %}
{% set uptime = states('sensor.uptime') | as_datetime %}
{% set current_time = now() %}
{% if uptime is not none %}
{{ is_delayed and is_valid and (current_time - uptime).total_seconds() > 60 }}
{% else %}
false
{% endif %}
What I’ve Verified So Far:
- Entities Exist:
binary_sensor.delayed_ct_gpos_power_a_availability
exists and appears to work correctly (state ison
after startup delay).sensor.ct_gpos_power_a
exists and is notunknown
orunavailable
.sensor.uptime
is valid.
- Template Logic:
- Tested the availability templates for both sensors in Developer Tools > Template, and they both evaluate as
true
.
- Unique ID Conflicts:
- Verified that there are no duplicate
unique_id
values.
- Restart/Reload:
- Reloaded YAML and restarted Home Assistant, but the issue persists.
Questions:
- Could the
binary_sensor.delayed_ct_gpos_power_a_availability
be causing a race condition or initialization timing issue? - Why would one sensor work while the other fails when they share identical logic and dependencies?
- Are there any known quirks with the
delay_on
property in binary sensors that might affect dependent template sensors?
Any help or suggestions would be greatly appreciated! Let me know if you need logs or additional context to assist.
Thank you!