diff --git a/esphome/core/config.py b/esphome/core/config.py index 496fe82934..3835fd3875 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -1,5 +1,6 @@ from __future__ import annotations +from collections import Counter import logging import os from pathlib import Path @@ -515,10 +516,11 @@ async def _add_looping_components() -> None: if not entries: return - # Build constexpr sum for the exact count + # Build constexpr sum for the exact count, deduplicating by type + type_counts = Counter(entries) terms = [ - f"(!std::is_same_v)" - for cpp_type in entries + f"({count} * !std::is_same_v)" + for cpp_type, count in type_counts.items() ] constexpr_expr = " + \\\n ".join(terms) cg.add_global(